-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathldatest.py
More file actions
66 lines (60 loc) · 1.91 KB
/
ldatest.py
File metadata and controls
66 lines (60 loc) · 1.91 KB
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
import json
from operator import itemgetter
import operator
from random import randint
import re
with open('trump.json') as f:
trump = json.load(f)
with open('keywords.json') as f:
kwrds = json.load(f)
def returnTopics(s1):
data = []
for t in trump:
if s1 in t['text'].lower():
data.append(t['text'].lower())
data = [re.sub('\s+', ' ', sent) for sent in data]
data = [re.sub("\'", "", sent) for sent in data]
# print (data)
matchGroup = []
verify = []
for d in data:
splitD = d.split(" ")
matchedData = set(splitD) & set(kwrds)
# print (matchedData)
c = dict.fromkeys(matchedData, 0)
matchedList = list(set(splitD) & set(kwrds))
for m in matchedData:
if m in verify:
index1 = verify.index(m)
matchGroup[index1]['count'] += 1
for li in matchedList:
if li in matchGroup[index1]['matches']:
matchGroup[index1]['matches'][li] += 1
elif m not in verify:
hashArr = {
'word': m,
'count': 1,
'matches': c
}
matchGroup.append(hashArr)
verify.append(m)
newlist = sorted(matchGroup, key=lambda k: k['count'])
newlist.reverse()
slicedList = newlist[:81]
dataArr = []
for index, sort in enumerate(slicedList):
if index <= 80:
sorted_x = sorted(sort['matches'].items(), key=operator.itemgetter(1))
sorted_x.reverse()
wordArr = []
xSlice = sorted_x[:10]
for xS in xSlice:
wordArr.append(xS[0])
hash2 = {
'word': sort['word'],
'data': index/2,
'matches': wordArr
}
dataArr.append(hash2)
print (dataArr)
return dataArr, s1