-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathGenerator.py
402 lines (237 loc) · 9.7 KB
/
Generator.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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
# coding: utf-8
# In[19]:
import nltk
import numpy as np
from nltk.corpus import wordnet as wn
# In[20]:
def indefinite_article(w):
"""Generate an indefinite article for the given phrase: a, an, or empty string"""
if (len(w) == 0):
return ""
if w.lower().startswith("a ") or w.lower().startswith("an ") or w.lower().startswith("the "):
return ""
return "an " if w.lower()[0] in list('aeiou') else "a "
# In[21]:
def camel(s):
"""Camel case the given string"""
return s[0].upper() + s[1:]
# In[22]:
def removeArticle(s):
"""Remove the article in the beginning of the given phrase"""
if s.startswith("a "):
return s[2:]
elif s.startswith("an "):
return s[3:]
elif s.startswith("the "):
return s[4:]
return s
# In[23]:
def NA_joke(d1, d2, w1, w2):
return "What do you call " + indefinite_article(d1) + d1 + "" + " that is " + removeArticle(d2) + "? " + camel(indefinite_article(w1)) + w1 + " " + w2 + "."
# In[24]:
from nltk.stem import WordNetLemmatizer
wordnet_lemmatizer = WordNetLemmatizer()
# In[25]:
import csv
# In[26]:
h1 = []
file = open(r'C:\Users\Aravind\Downloads\J.E.F.F.-master\generator\homophones.csv',"r")
reader = csv.reader(file)
for line in reader:
h1.append(line)
# In[27]:
def get_part_of_speech(word):
synsets = wn.synsets(word)
# Get a collection of synsets (synonym sets) for a word
return (synsets[0].lexname().split(".")[0])
# In[28]:
def get_def(word):
synsets = wn.synsets(word)
# In[29]:
get_part_of_speech("airy")
# In[ ]:
# '
# # NA JOKE - DONE
# In[30]:
for line in h1:
if wn.synsets(line[0]) and get_part_of_speech(line[0]) == 'noun':
for i in range(len(line)) :
if (wn.synsets(line[i])) and get_part_of_speech(line[i]) == 'adj':
s1 = wn.synsets(line[0])
s2 = wn.synsets(line[i])
print(NA_joke(s1[0].definition(),s2[0].definition(),line[i],line[0]))
# # N2A2 DONE
# In[31]:
def N2A2_joke(d1, d2, w1, w2):
return "What do you call " + indefinite_article(d1) + d1 + " " + removeArticle(d2) + "? " + camel(indefinite_article(w1)) + w1 + " " + w2 + "."
# In[ ]:
# In[32]:
harr = np.array(h1)
words = []
for item in harr:
for i in range(len(item)):
words.append(item[i])
words = words[:1000]
# In[33]:
s = wn.synsets("complacent")
xa = []
for sa in s:
xa.append(sa.lemma_names())
print(xa)
stopwords = set(w.rstrip() for w in open(r'C:\Users\Aravind\Downloads\Udemy,NLP\machine_learning_examples-master\machine_learning_examples-master\nlp_class\stopwords.txt'))
# In[67]:
wordnet_lemmatizer.lemmatize("cake")
# In[72]:
w11 = "x"
w22 = "x"
syn1 = []
syn2 = []
for word in words:
if wn.synsets(word) and get_part_of_speech(word) == 'noun' and word not in stopwords:
for i in range(len(words)) :
if (wn.synsets(words[i])) and get_part_of_speech(words[i]) == 'adj' and words[i] not in stopwords:
s1 = wn.synsets(word)
s2 = wn.synsets(words[i])
syn1 = []
syn2 = []
for s in s1:
syn1.append(s.lemma_names())
for s in s2:
syn2.append(s.lemma_names())
for line in syn1:
for part in line:
if (get_part_of_speech(part) == 'noun' and part != word and wordnet_lemmatizer.lemmatize(part) != wordnet_lemmatizer.lemmatize(word)):
w11 = part
break
for line in syn2:
for part in line:
if (get_part_of_speech(part) == 'adj' and part != words[i] and wordnet_lemmatizer.lemmatize(part) != wordnet_lemmatizer.lemmatize(words[i])):
w22 = part
break
if(len(w11) > 2 and len(w22) > 2):
print(N2A2_joke(w22,w11,words[i],wordnet_lemmatizer.lemmatize(word)))
w11 = " "
w22 = " "
# # N2AN DONE
# In[16]:
def N2AN_joke(d1, d2, w1, w2):
return "When is " + indefinite_article(d1) + d1 + " like " + indefinite_article(d2) + d2 + "? " + "When it is " + indefinite_article(w1) + w1 + " " + w2 + "."
# In[44]:
w13 = ""
w23 = ""
for line in h1:
if wn.synsets(line[0]) and get_part_of_speech(line[0]) == 'noun' and line[0] not in stopwords:
for i in range(len(line)) :
if (wn.synsets(line[i])) and get_part_of_speech(line[i]) == 'adj' and line[i] not in stopwords:
s1 = wn.synsets(line[0])
s2 = wn.synsets(line[i])
syn1 = []
syn2 = []
for s in s1:
syn1.append(s.lemma_names())
for s in s2:
syn2.append(s.lemma_names())
for lin in syn1:
for part in lin:
if (get_part_of_speech(part) == 'noun' and part != line[0] and wordnet_lemmatizer.lemmatize(part) != wordnet_lemmatizer.lemmatize(line[0])):
w13 = part
break
for lin in syn2:
for part in lin:
if (get_part_of_speech(part) == 'adj' and part != line[i] and wordnet_lemmatizer.lemmatize(part) != wordnet_lemmatizer.lemmatize(line[i])):
w23 = part
break
if(len(w13) > 2 and len(w23) > 2):
print(N2A2_joke(w23,w13,line[i],line[0]))
w13 = " "
w23 = " "
# In[ ]:
# for line in h1:
# for i in range(len(line)) :
# s2 = wn.synsets(line[i])
# for s in s2:
# if (s.lexname().split(".")[0] == 'noun' and s.name().split(".")[0] != line[i] ):
# w13 = s.name().split(".")[0]
# print(w13 + " " + line[i])
# break
# # N2V2 DONE
# In[61]:
def N2V2_joke(d1, d2, w1, w2):
return "Why did someone " + d2 + " " + indefinite_article(d1) + d1 + "? " + "So they could " + w1 + " " + indefinite_article(w1) + w2 + "."
# In[71]:
for line in h1:
if wn.synsets(line[0]) and get_part_of_speech(line[0]) == 'noun' and line[0] not in stopwords:
for i in range(len(line)) :
if (wn.synsets(line[i])) and get_part_of_speech(line[i]) == 'verb' and line[i] not in stopwords:
s1 = wn.synsets(line[0])
s2 = wn.synsets(line[i])
syn1 = []
syn2 = []
for s in s1:
syn1.append(s.lemma_names())
for s in s2:
syn2.append(s.lemma_names())
for lin in syn1:
for part in lin:
if (get_part_of_speech(part) == 'noun' and part != line[0] and wordnet_lemmatizer.lemmatize(part) != wordnet_lemmatizer.lemmatize(line[0])):
w11 = part
break
for lin in syn2:
for part in lin:
if (get_part_of_speech(part) == 'verb' and part != line[i] and wordnet_lemmatizer.lemmatize(part) != wordnet_lemmatizer.lemmatize(line[i])):
w22 = part
break
if(len(w11) > 2 and len(w22) > 2 ):
print(N2V2_joke(wordnet_lemmatizer.lemmatize(w11, 'v'),w22,wordnet_lemmatizer.lemmatize(line[i],'v'),line[0]))
w11 = " "
w22 = " "
# # N4 DONE
# In[47]:
def N4_joke(d1, d2, w1, w2):
return "When is " + indefinite_article(d1) + d1 + " like " + indefinite_article(d2) + d2 + "? " + "When it is " + indefinite_article(w2) + w2 + "."
# In[87]:
def ishomophoneof(x,y):
linex = []
liney = []
for line in h1:
if x in line:
linex = line
if y in line:
liney = line
if linex == liney:
return "true"
# In[89]:
w2 = ""
w1 = ""
for line in h1:
if wn.synsets(line[0]) and get_part_of_speech(line[0]) == 'noun' and line[0] not in stopwords:
for i in range(len(line)) :
if (wn.synsets(line[i])) and get_part_of_speech(line[i]) == 'noun' and line[i] not in stopwords:
s1 = wn.synsets(line[0])
s2 = wn.synsets(line[i])
syn1 = []
syn2 = []
for s in s1:
syn1.append(s.lemma_names())
for s in s2:
syn2.append(s.lemma_names())
for lin in syn1:
for part in lin:
if (get_part_of_speech(part) == 'noun' and part != line[0] and wordnet_lemmatizer.lemmatize(part) != wordnet_lemmatizer.lemmatize(line[0])):
w1 = part
break
for lin in syn2:
for part in lin:
if (get_part_of_speech(part) == 'noun' and part != line[i] and part != line[0] and wordnet_lemmatizer.lemmatize(part) != wordnet_lemmatizer.lemmatize(line[i]) and ishomophoneof(part,line[0]) == "true"):
w2 = part
break
if(len(w1) > 2 and len(w2) > 2 and w1!= w2):
print(N4_joke(w2,w1,line[i],line[0]))
w1 = " "
w2 = " "
#w2 must be a homophone of line[0]
# # N3A wip
# In[25]:
def N3A_joke(d1, d2, w1, w2):
return "What do you call " + indefinite_article(d1) + d1 + " " + d2 + "? " + camel(indefinite_article(w2)) + w2 + "."
# In[ ]: