forked from pbarjoueian/persian-tweepy
/
analysis.py
82 lines (60 loc) · 2.04 KB
/
analysis.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import json
import hazm
import os
import os.path
from pathlib import Path
sentimentJSON = json.loads(
open('./dictionaries/dict.json', 'r', encoding='utf-8-sig').read())
sentimentDICT = sentimentJSON['emotions']
def getWordSentiment(word):
for key in sentimentDICT:
if word == key:
return sentimentDICT[key]
return 0 # not important/not in dictionary
def getListSentiment(words):
sentiment = 0
for word in words:
sentiment += getWordSentiment(word)
return sentiment
def prepareText(text):
normalizer = hazm.Normalizer()
text = normalizer.normalize(text)
tokens = hazm.word_tokenize(text)
stemmer = hazm.Stemmer()
words = [stemmer.stem(token) for token in tokens]
return words
def countTextWords(text):
normalizer = hazm.Normalizer()
text = normalizer.normalize(text)
tokens = hazm.word_tokenize(text)
stemmer = hazm.Stemmer()
words = [stemmer.stem(token) for token in tokens]
return len(words)
def analyseText(text):
words = prepareText(text)
return getListSentiment(words)
def analyseTextDetailed(text):
words = prepareText(text)
result = []
for word in words:
s = getWordSentiment(word)
if not s == 0:
result.append((word, s))
if len(result) > 0:
print(result)
if __name__ == '__main__':
dir_files = os.listdir()
output_files = [file for file in dir_files if file.startswith(
'merged') and file.endswith('.txt')]
weekNumber = 19
for file_name in output_files:
with open(file_name, 'r') as file:
with open('sentiments' + str(weekNumber) + '-' + file_name, 'w') as output_file:
output_file.close()
with open('sentiments' + str(weekNumber) + '-' + file_name, 'a') as output_file:
for tweet in file:
sentiment = analyseText(tweet)
output_file.write(tweet)
output_file.write(str(sentiment) + '\n\n')
output_file.close()
file.close()