-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path2.py
298 lines (184 loc) · 6.15 KB
/
2.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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
# coding: utf-8
# In[307]:
import numpy as np
import nltk
from nltk.corpus import wordnet as wn
import pandas as pd
from nltk import sent_tokenize
from nltk import word_tokenize
def convert_tag(tag):
"""Convert the tag given by nltk.pos_tag to the tag used by wordnet.synsets"""
tag_dict = {'N': 'n', 'J': 'a', 'R': 'r', 'V': 'v'}
try:
return tag_dict[tag[0]]
except KeyError:
return None
def doc_to_synsets(doc):
synonyms = []
doc_comp=word_tokenize(doc)
poss=nltk.pos_tag(doc_comp)
#print(poss)
for doc in poss:
#print(doc)
#poss=nltk.pos_tag(doc)
#print(poss)
wordnet_tag=convert_tag(doc[1][0])
#print(wordnet_tag)
#print(poss)
synon=wn.synsets(doc[0],wordnet_tag)
#print(synon)
if len(synon)!=0:
synonyms.append(synon[0])
#for syn in wn.synsets(doc[0],wordnet_tag):
#for i in syn.lemmas():
#synonyms.append(i.name())
#print(synonyms)
# Your Code Here
#print(synonyms)
return synonyms# Your Answer Here
def similarity_score(s1, s2):
get_score=[]
max_score=[]
for syn1 in s1:
for syn2 in s2:
#print(syn1,syn2)
score=wn.path_similarity(syn1,syn2)
#print('score is',score)
if score is not None:
#print('true score',score)
get_score.append(score)
#print(score)
#print('hi')
#print(get_score.append(score))
#print(get_score)
if len(get_score)>=1:
#print('hi')
max_score.append(max(get_score))
#print(nltk.pos_tag(s1))
# Your Code Here
return (sum(max_score)/len(max_score)) # Your Answer Here
def document_path_similarity(doc1, doc2):
synsets1 = doc_to_synsets(doc1)
synsets2 = doc_to_synsets(doc2)
print(similarity_score(synsets1, synsets2))
return (similarity_score(synsets1, synsets2) + similarity_score(synsets2, synsets1)) / 2
# In[308]:
dog = wn.synset('dog.n.01')
cat = wn.synset('cat.n.01')
wn.path_similarity(dog,cat)
#doc='function'
#tok=word_tokenize(doc)
#poss=nltk.pos_tag(tok)
#print(poss[0][1])
#word=poss[0]
#print(type(word))
#wn.synsets(word+'.n')
#poss
get_score=[]
a=wn.synset('function.n.01')
b=wn.synset('see.v.01')
r=wn.path_similarity(a,b)
print(r)
if r is not None:
print('hi')
#print(get_score)
#print(wn.synsets('dog','n')[0])
#synonyms=[]
#for syn in wn.synsets('dog','n'):
#for i in syn.lemmas():
#synonyms.append(i.name())
#synonyms[0]
# In[309]:
def test_document_path_similarity():
#doc1='function'
#doc2='see'
doc1 = 'what is your name'
doc2 = 'May I know your name'
return document_path_similarity(doc1, doc2)
# In[310]:
result=test_document_path_similarity()
print('result is:',result)
# In[334]:
# Use this dataframe for questions most_similar_docs and label_accuracy
paraphrases = pd.read_csv('paraphrases.csv')
paraphrases.info()
# In[337]:
def most_similar_docs():
result_list=[]
que1=paraphrases['D1'].tolist()
que2=paraphrases['D2'].tolist()
#result=document_path_similarity(que1[1],que2[2])
for i in range(len(que1)):
result=document_path_similarity(que1[i],que2[i])
result_list.append(result)
#result=document_path_similarity(paraphrases['D1'].astype(str), paraphrases['D2'].astype(str))
#print(que1)
# Your Code Here
return result_list
# In[1]:
result=most_similar_docs()
paraphrases['result']=result
paraphrases
# In[ ]:
def label_accuracy():
from sklearn.metrics import accuracy_score
# Your Code Here
return # Your Answer Here
# In[354]:
import pickle
import gensim
from sklearn.feature_extraction.text import CountVectorizer
# Load the list of documents
with open('newsgroups', 'rb') as f:
newsgroup_data = pickle.load(f)
# Use CountVectorizor to find three letter tokens, remove stop_words,
# remove tokens that don't appear in at least 20 documents,
# remove tokens that appear in more than 20% of the documents
vect = CountVectorizer(min_df=20, max_df=0.2, stop_words='english')
#token_pattern='(?u)\\b\\w\\w\\w+\\b'
#print(newsgroup_data)
# Fit and transform
X = vect.fit_transform(newsgroup_data)
# Convert sparse matrix to gensim corpus.
corpus = gensim.matutils.Sparse2Corpus(X, documents_columns=False)
# Mapping from word IDs to words (To be used in LdaModel's id2word parameter)
id_map = dict((v, k) for k, v in vect.vocabulary_.items())
#print(id_map)
# In[359]:
# Use the gensim.models.ldamodel.LdaModel constructor to estimate
# LDA model parameters on the corpus, and save to the variable `ldamodel`
# Your code here:
#ldamodel =
Lda = gensim.models.ldamodel.LdaModel
# Running and Trainign LDA model on the document term matrix.
ldamodel = Lda(corpus, num_topics=30, id2word = id_map, passes=25)
ldamodel
# In[360]:
def lda_topics():
top=ldamodel.print_topics(4)
# Your Code Here
return top# Your Answer Here
# In[363]:
arr=lda_topics()
arr[1]
# In[366]:
new_doc = ["\n\nIt's my understanding that the freezing will start to occur because of the\ngrowing distance of Pluto and Charon from the Sun, due to it's\nelliptical orbit. It is not due to shadowing effects. \n\n\nPluto can shadow Charon, and vice-versa.\n\nGeorge Krumins\n-- "]
# In[391]:
def topic_distribution():
vect_new = CountVectorizer(stop_words='english')
X_new = vect_new.fit_transform(new_doc)
# Convert sparse matrix to gensim corpus.
corpus_new = gensim.matutils.Sparse2Corpus(X_new, documents_columns=False)
#ldamodel(corpus_new, num_topics=5)
#arr1=ldamodel.print_topics(4)(corpus_new)
arr1=ldamodel.get_document_topics(corpus_new)
# Your Code Here
return arr1 # Your Answer Here
# In[394]:
arr2=topic_distribution()
for ar in range(len(arr2)):
print(arr2[ar])
# In[ ]:
def topic_names():
# Your Code Here
return # Your Answer Here