-
Notifications
You must be signed in to change notification settings - Fork 0
/
analyzeSlang.py
223 lines (194 loc) · 10.4 KB
/
analyzeSlang.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
from flask import request
from flask.helpers import make_response
import csv
import json
from urbanDictInfo import *
from asyncGoogleTrends import *
class AnalyzeSlang:
def __init__(self, socialMedia):
self.__socialMedia = socialMedia
self.__slangs = None
self.__slangDict = None
self.__data = None #Nested Dictionary of messages, timestamp, username
self.__names = []
self.__wordCount = {}
self.__topTen = {}
self.__totalSent = None
self.__topTenSlangWords = {}
self.__slangCountPerPerson = {}
self.__personalSlangDict = {}
self.__getJSON()
self.__getSlang()
self.__getNames()
self.__getWordCount()
self.__getTopTen()
self.__getTotalSent()
self.__getTopTenSlangWords()
self.__getTopTenSlangCountPerPerson()
# self.__newSlang()
def __getSlang(self):
if (self.__data) != None:
slangList = []
slangDict = commonFilter(self.__data)
updatedSlangList = asyncio.run(get_Slang(slangDict["tempSlang"]))
combinedSlangList = slangDict['finalSlang']+ updatedSlangList
slangDict = asyncio.run(finalUrbanFilter(combinedSlangList))
self.__slangDict = slangDict
for slang in slangDict:
slangList.append(slang)
self.__slangs = slangList
# def __newSlang(self):
# if (self.__slangs) != None:
# with open ("commonSlang.json", "r") as f:
# commonSlangJson = json.load(f)
# for word in self.__slangs:
# punctuatedWord = word.lower().translate(str.maketrans('', '', string.punctuation)).replace('?', '')
# if punctuatedWord not in commonSlangJson:
# commonSlangJson[word.strip()] = ''
# with open ("commonSlang.json", "w") as f:
# json.dump(commonSlangJson, f)
def __getJSON(self):
mydict = (request.files['file']).read()
file = make_response(mydict)
file.mimetype = 'application/json'
file.close()
self.__data = file.json
def __getNames(self):
if self.__socialMedia == "facebook" or self.__socialMedia == "instagram":
for participant in self.__data["participants"]:
self.__names.append(participant["name"])
if self.__socialMedia == "discord":
for message in self.__data["messages"]:
if message["author"]["name"] not in self.__names:
self.__names.append(message["author"]["name"])
def __getWordCount(self):
for message in reversed(self.__data["messages"]):
if "content" in message:
words = message["content"].split()
for word in words:
if word in self.__wordCount:
self.__wordCount[word] += 1
else:
self.__wordCount[word] = 1
def __getTopTen(self):
sortedKeys = sorted(self.__wordCount, key=self.__wordCount.get)
for i in range(1, 11):
self.__topTen[sortedKeys[len(sortedKeys) - i]] = self.__wordCount[sortedKeys[len(sortedKeys) - i]]
def __getTotalSent(self):
self.__totalSent = {"totalMessages": [], "totalWords": []}
for name in self.__names:
msgCount = 0
wordCount = 0
for message in self.__data["messages"]:
if self.__socialMedia == "facebook" or self.__socialMedia == "instagram":
if message["sender_name"] == name:
msgCount += 1
if "content" in message:
words = message["content"].split()
wordCount += len(words)
if self.__socialMedia == "discord":
if message["author"]["name"] == name:
msgCount += 1
if "content" in message:
words = message["content"].split()
wordCount += len(words)
self.__totalSent["totalMessages"].append(msgCount)
self.__totalSent["totalWords"].append(wordCount)
def __getTopTenSlangWords(self):
slangCount = {}
for slang in self.__slangs:
for message in self.__data["messages"]:
if "content" in message:
sentence = message["content"].split()
for word in sentence:
if word == slang or word.lower().translate(str.maketrans('', '', string.punctuation)).replace('?', '') == slang:
if slang in slangCount:
slangCount[slang] += 1
else:
slangCount[slang] = 1
sortedKeys = sorted(slangCount, key=slangCount.get)
if len(sortedKeys) < 10:
for i in range(1, len(sortedKeys)+1):
self.__topTenSlangWords[sortedKeys[len(sortedKeys) - i]] = slangCount[sortedKeys[len(sortedKeys) - i]]
if len(sortedKeys) >= 10:
for i in range(1, 11):
self.__topTenSlangWords[sortedKeys[len(sortedKeys) - i]] = slangCount[sortedKeys[len(sortedKeys) - i]]
def __getTopTenSlangCountPerPerson(self):
for slang in [*self.__topTenSlangWords.keys()]:
for name in self.__names:
slangCount = 0
for message in self.__data["messages"]:
if self.__socialMedia == "facebook" or self.__socialMedia == "instagram":
if "content" in message and message["sender_name"] == name:
sentence = message["content"].split()
for word in sentence:
if word == slang or word.lower().translate(str.maketrans('', '', string.punctuation)).replace('?', '') == slang:
slangCount += 1
if self.__socialMedia == "discord":
if "content" in message and message["author"]["name"] == name:
sentence = message["content"].split()
for word in sentence:
if word == slang or word.lower().translate(str.maketrans('', '', string.punctuation)).replace('?', '') == slang:
slangCount += 1
if slang in self.__slangCountPerPerson:
self.__slangCountPerPerson[slang].append(slangCount)
else:
self.__slangCountPerPerson[slang] = [slangCount]
def __getPersonalSlangDict(self):
for name in self.__names:
personalSlangCount = {}
for slang in [*self.__slangs]:
for message in self.__data["messages"]:
if self.__socialMedia == "facebook" or self.__socialMedia == "instagram":
if "content" in message and message["sender_name"] == name:
sentence = message["content"].split()
for word in sentence:
if word == slang or word.lower().translate(str.maketrans('', '', string.punctuation)).replace('?', '') == slang:
if word in personalSlangCount or word.lower().translate(str.maketrans('', '', string.punctuation)).replace('?', '') in personalSlangCount:
personalSlangCount[slang] += 1
else:
personalSlangCount[slang] = 1
if self.__socialMedia == "discord":
if "content" in message and message["author"]["name"] == name:
sentence = message["content"].split()
for word in sentence:
if word == slang or word.lower().translate(str.maketrans('', '', string.punctuation)).replace('?', '') == slang:
if word in personalSlangCount or word.lower().translate(str.maketrans('', '', string.punctuation)).replace('?', '') in personalSlangCount:
personalSlangCount[slang] += 1
else:
personalSlangCount[slang] = 1
self.__personalSlangDict[name] = personalSlangCount
return self.__personalSlangDict
def createCSV(self, userName):
for name in self.__names:
if userName == name:
self.__userExists = True
csvFile = open("messages.csv", "w", encoding='utf-8', newline='')
csvWriter = csv.writer(csvFile)
csvWriter.writerow(["Content", "Context"])
for message in self.__data["messages"]:
if self.__socialMedia == "facebook" or self.__socialMedia == "instagram":
if message["sender_name"] == userName and "content" in message:
csvWriter.writerow([message["content"]])
if self.__socialMedia == "discord":
if message["author"]["name"] == userName and "content" in message:
csvWriter.writerow([message["content"]])
break
if not self.__userExists:
raise NameError("Invalid Name")
def getTemplateSetup(self):
template = {"topTenWords": list(self.__topTen.keys()),
"topTenWordCount": list(self.__topTen.values()),
"names": self.__names,
"totalMessages": list(self.__totalSent["totalMessages"]),
"totalWords": list(self.__totalSent["totalWords"]),
"topTenSlang": list(self.__topTenSlangWords.keys()),
"topTenSlangCount": list(self.__topTenSlangWords.values()),
"topTenSlangCountPerPerson": list(self.__slangCountPerPerson.values()),
"slangMeaning": self.__slangDict,
"slangListLength": len(self.__topTenSlangWords.keys()),
"personalSlangDict": self.__getPersonalSlangDict(),
}
return(template)
def getParticipantNames(self):
return self.__names