-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathai.py
151 lines (131 loc) · 3.93 KB
/
ai.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
import awstools
import gensim
import pandas as pd
import string
import nltk
from nltk.corpus import stopwords
from nltk.corpus import wordnet
from nltk.stem import WordNetLemmatizer
import enchant
import math
import random
def pos_tagger(nltk_tag):
if nltk_tag.startswith('J'):
return wordnet.ADJ
elif nltk_tag.startswith('V'):
return wordnet.VERB
elif nltk_tag.startswith('N'):
return wordnet.NOUN
elif nltk_tag.startswith('R'):
return wordnet.ADV
else:
return None
def clean_text(text):
d1 = enchant.Dict("en_US")
d2 = enchant.Dict("en_GB")
words = stopwords.words('english')
lemmatizer = WordNetLemmatizer()
text = text.lower().translate(str.maketrans(string.punctuation, ' ' * len(string.punctuation)))
arr = text.split()
new_arr = []
for k in range(len(arr)):
if arr[k] not in words and len(arr[k])>1 and(d1.check(arr[k]) or d2.check(arr[k])):
p = pos_tagger(nltk.pos_tag([arr[k]])[0][1])
if(p==None):
new_arr.append(lemmatizer.lemmatize(arr[k]))
else:
new_arr.append(lemmatizer.lemmatize(arr[k], p))
text = ' '.join(new_arr)
return ''.join([c for c in text if ord(c)<128])
def tokenize_text(text):
tokens = []
for sent in nltk.sent_tokenize(text):
for word in nltk.word_tokenize(sent):
if len(word) < 2:
continue
tokens.append(word.lower())
return tokens
def get_events():
events = awstools.getAllEvents()
arr = []
model = gensim.models.KeyedVectors.load_word2vec_format('model/GoogleNews-vectors-negative300.bin', binary=True, limit=50000)
for event in events:
text = clean_text(event['description'])
words = tokenize_text(text)
vector = [0] * 300
sze = 0
for word in words:
if word in model:
tmp = model[word]
sze += 1
for i in range(len(tmp)):
vector[i] += tmp[i]
if sze > 0:
for i in range(300):
vector[i] /= sze
arr.append([event['eventid'], event['description'], vector])
df = pd.DataFrame(arr, columns=['id', 'description', 'vector'])
df = df.set_index('id')
return df
def suggest_similar(eventid):
df = get_events()
lst = []
for index, row in df.iterrows():
if index == eventid:
continue
dist = math.dist(df.loc[eventid, 'vector'], row['vector'])
lst.append((dist,index))
lst.sort()
names = []
for x in lst:
names.append(x[1])
return names
"""
def suggest_user(userid):
arr = [x['eventid'] for x in awstools.getEventsFromVolunteer(userid)]
df = get_events()
lst = []
for index, row in df.iterrows():
if index in arr:
continue;
mn = 1000000005
for event in arr:
dist = math.dist(row['vector'], df.loc[event, 'vector'])
print(index, event, dist)
mn = min(mn, dist)
print(index, mn)
lst.append((mn, index))
lst.sort()
names = []
for x in lst:
names.append(x[1])
return names
"""
def suggest_user(userid):
arr=[x['eventid'] for x in awstools.getEventsFromVolunteer(userid)]
df=get_events()
vector = [0]*300
names=[]
for index,row in df.iterrows():
if index in arr:
for i in range(300):
vector[i]+=row['vector'][i]
else:
names.append(index)
if len(arr)==0:
random.shuffle(names)
return names
for i in range(300):
vector[i]/=len(arr)
lst=[]
for i in names:
dist=math.dist(df.loc[i,'vector'],vector)
lst.append((dist,i))
lst.sort()
names=[]
for x in lst:
names.append(x[1])
return names
if __name__ == '__main__':
username = input('Username: ')
print(suggest_user(username))