-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmongo_new.py
163 lines (144 loc) · 6.83 KB
/
mongo_new.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
from pymongo import MongoClient
import numpy as np
from nltk.corpus import stopwords
def dump_glove_matrix(lang_origin,lang_target):
client = MongoClient('localhost', 27017)
db = client['nlprokz']
words = sorted(db.hinglish.find({"lang":lang_target}).distinct("words"))
count = 0
count_update = 0
for i in db.hinglish.find({"lang":lang_origin}):
try:
target_sentence = db.hinglish.find_one({"identifier":i['identifier'],"lang":lang_target})
bulk_vec = []
for j in i["words"]:
vec_count = db.bilingual_glove_vec.count({"word":j.lower()})
if vec_count > 0:
doc = db.bilingual_glove_vec.find_one({"word":j.lower()})
vec = np.array(doc["vec"])
norm = doc['norm_count']
flag = False
for k in xrange(len(words)):
if words[k] in target_sentence["words"]:
glove = db.glove.find_one({"gram":words[k]})
if glove is not None:
glove_vec = [ float(t) for t in glove['glove_vector']]
vec += np.array(glove_vec)
norm += 1
flag = True
if flag:
count_update += 1
db.bilingual_glove_vec.update({"word":j.lower()},{"$set":{"vec":vec.tolist(),"norm_count":norm,"lang_origin":lang_origin,"lang_target":lang_target}})
else:
vec = np.zeros(300)
norm = 0
flag = False
for k in words:
if k in target_sentence["words"]:
glove = db.glove.find_one({"gram":k})
if glove is not None:
glove_vec = [ float(t) for t in glove['glove_vector']]
vec += np.array(glove_vec)
norm += 1
flag = True
if flag:
count += 1
db.bilingual_glove_vec.insert({"word":j.lower(),"norm_count":norm,"vec":vec.tolist(),"lang_origin":lang_origin,"lang_target":lang_target})
#print "NEW INSERTS:"+str(count)
except Exception as e:
print e
def get_vectors():
vector = {}
with open('eng_vectors.txt', 'r') as f:
full_data = [line.rstrip().split(' ') for line in f]
for i in full_data:
vector[i[0]] = i[1:]
# with open('hin_vectors.txt', 'r') as f:
# full_data = [line.rstrip().split(' ') for line in f]
# for i in full_data:
# vector[i[0]] = i[1:]
return vector
def dump_err_glove_matrix(lang_origin,lang_target):
client = MongoClient('localhost', 27017)
db = client['nlprokz']
words = sorted(db.hinglish.find({"lang":lang_target}).distinct("words"))
count = 0
vector = get_vectors()
stop = set(stopwords.words('english'))
count_update = 0
for i in db.hinglish.find({"lang":lang_origin}):
try:
target_sentence = db.hinglish.find_one({"identifier":i['identifier'],"lang":lang_target})
bulk_vec = []
for j in i["words"]:
vec_count = db.bilingual_new_glove_vec.count({"word":j.lower()})
if vec_count > 0:
doc = db.bilingual_new_glove_vec.find_one({"word":j.lower()})
vec = np.array(doc["vec"])
norm = doc['norm_count']
flag = False
for k in xrange(len(words)):
if words[k] in target_sentence["words"]:
glove = vector[words[k]]
if glove is not None and words[k].lower() not in stop:
glove_vec = [ float(t) for t in glove]
vec += np.array(glove_vec)
norm += 1
flag = True
if flag:
count_update += 1
db.bilingual_new_glove_vec.update({"word":j.lower()},{"$set":{"vec":vec.tolist(),"norm_count":norm,"lang_origin":lang_origin,"lang_target":lang_target}})
else:
vec = np.zeros(100)
norm = 0
flag = False
for k in words:
if k in target_sentence["words"]:
glove = vector[k]
if glove is not None and k.lower() not in stop:
glove_vec = [ float(t) for t in glove]
vec += np.array(glove_vec)
norm += 1
flag = True
if flag:
count += 1
db.bilingual_new_glove_vec.insert({"word":j.lower(),"norm_count":norm,"vec":vec.tolist(),"lang_origin":lang_origin,"lang_target":lang_target})
print "NEW INSERTS:"+str(count)
except Exception as e:
print e
def find_closest(lang_origin,lang_target):
client = MongoClient("localhost", 27017)
db = client["nlprokz"]
target_words = []
target_words_strings = []
for i in db.bilingual_new_glove_vec.find({"lang_origin":lang_target}):
target_words.append(i['vec'])
target_words_strings.append(i['word'])
words = sorted(db.hinglish.find({"lang":lang_target}).distinct("words"))
target_words = np.transpose(np.array(target_words))
target_words = target_words/ np.linalg.norm(target_words,axis = 0)
count = 0
for i in db.bilingual_new_glove_vec.find({"lang_origin":lang_origin}):
origin_word = 1.0*np.array([i['vec']])
origin_word = origin_word/np.linalg.norm([i['vec']])
# dist = []
# for word_vec in target_words:
# word_vec = np.transpose(word_vec)
# dist.append(euclidean(origin_word, word_vec))
# max_similarity = np.argmax(dist[0])
# #print "STRING:"+i['word']+":"+target_words_strings[max_similarity]
# count += 1
# if count > 10:
# break
dot_product = np.dot(origin_word,target_words)
max_index = dot_product.argsort()[-1:][::-1]
for j in max_index[0][-10:]:
print "STRING %s: %s : %s" % (i['word'],target_words_strings[j],str(dot_product[0][j]))
# max_similarity = np.argmax(dot_product[0])
#print "STRING:"+i['word']+":"+target_words_strings[max_similarity]+":"+str(dot_product[0][max_similarity])
count += 1
if count > 100:
break
if __name__ == '__main__':
dump_err_glove_matrix("eng","eng")
# find_closest("hin","eng")