-
Notifications
You must be signed in to change notification settings - Fork 1
/
elaborate.py
334 lines (296 loc) · 10.5 KB
/
elaborate.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
import json
import sys
import urllib
import random
import time
import requests
import requests_cache
from pattern.en import wordnet, NOUN, VERB, ADJECTIVE, ADVERB
from pattern.en import referenced, INDEFINITE
from jinja2 import Template
cn_baseurl = 'http://conceptnet5.media.mit.edu/data/5.2/'
nn_killfile = [u'organisation', u'album', u'single', u'television show',
u'film', u'band', 'song', 'video game', 'software', 'other', 'there']
requests_cache.install_cache('elaborate_cache')
cached_requests = 0
fresh_requests = 0
backoff = 1.0
def canonical(query):
# sort query params in canonical order. requests_cache probably does this
# on its own, but I was experiencing weirdnesses while developing so I
# made this just to be sure
components = list()
for key in sorted(query.keys()):
components.append("%s=%s" % (key, urllib.quote_plus(str(query[key]))))
return '&'.join(components)
def cn_search_get_text(query, lemma):
global cached_requests, fresh_requests, backoff
url = cn_baseurl + 'search?' + canonical(query)
while True:
resp = requests.get(url)
obj = resp.json()
text = list()
if resp.status_code == 429:
sys.stderr.write("received 429, backing off: %0.2f\n" % backoff)
time.sleep(backoff)
backoff = backoff * 1.5
continue
break
if 'edges' not in obj:
sys.stderr.write(str(resp.status_code) + " " + str(obj) + "\n")
raise KeyError
for edge in obj['edges']:
text.extend([t for t in edge['text'] if len(t) > 0 and t != lemma and t not in nn_killfile])
if resp.from_cache:
sys.stderr.write(resp.url + ' is from cache\n')
cached_requests += 1
else:
sys.stderr.write("!!! " + resp.url + ' not cached, sleeping (%d, %d)\n' % (cached_requests, fresh_requests))
time.sleep(1)
fresh_requests += 1
return text
def underscore(s):
return s.replace(' ', '_')
def cn_start_isa(lemma):
query = {'start': '/c/en/' + underscore(lemma), 'rel': '/r/IsA',
'filter': 'core', 'limit': 10}
return cn_search_get_text(query, lemma)
def cn_end_isa(lemma):
query = {'end': '/c/en/' + underscore(lemma), 'rel': '/r/IsA',
'filter': 'core', 'limit': 10}
return cn_search_get_text(query, lemma)
def cn_start_has_property(lemma):
query = {'start': '/c/en/' + underscore(lemma), 'rel': '/r/HasProperty',
'filter': 'core', 'limit': 10}
return cn_search_get_text(query, lemma)
def cn_start_at_location(lemma):
query = {'start': '/c/en/' + underscore(lemma), 'rel': '/r/AtLocation',
'filter': 'core', 'limit': 10}
return cn_search_get_text(query, lemma)
def cn_end_at_location(lemma):
query = {'end': '/c/en/' + underscore(lemma), 'rel': '/r/AtLocation',
'filter': 'core', 'limit': 10}
return cn_search_get_text(query, lemma)
def wn_filter_pos(text, pos):
synsets = wordnet.synsets(text, pos=pos)
for s in synsets:
for synonym in s.synonyms:
if synonym[0].isupper():
return False
if len(synsets) > 0:
return True
class ElaborationImpossible(Exception): pass
class Elaboration(object):
def __init__(self, text, further):
self.text = text
self.further = further
def is_animate(lemma):
# this "works" but is very eager to grant animacy even for words that have
# one synset with a person hypernym (things like "rock" count as "animate"
# because a "rock" can be a person that you depend on)
hypernyms = list()
for synset in wordnet.synsets(lemma, pos=NOUN):
# skip synsets that are proper nouns, as these are always animate!
if any([s[0].isupper() for s in synset.synonyms]):
continue
synonyms = list()
for s in synset.hypernyms(recursive=True):
synonyms.extend(s.synonyms)
if 'person' in synonyms:
return True
return False
def synonyms(lemma, pos=NOUN):
synonyms = list()
for synset in wordnet.synsets(lemma, pos):
for synonym in synset.synonyms:
if synonym != lemma:
synonyms.append(synonym)
return synonyms
def antonyms(lemma, pos=NOUN):
antonyms = list()
for synset in wordnet.synsets(lemma, pos):
if synset.antonym is not None:
antonyms.extend(synset.antonym.synonyms)
return antonyms
def indef(lemma):
if lemma in ('someone', 'something'): return lemma
return referenced(lemma, article=INDEFINITE)
def subj_pronoun(lemma):
if is_animate(lemma): return "they"
else: return "it"
def obj_pronoun(lemma):
if is_animate(lemma): return "them"
else: return "it"
def copula(lemma):
if is_animate(lemma): return "were"
else: return "was"
def ucfirst(s):
return s[0].upper() + s[1:]
def render(tmpl, **args):
return Template(tmpl).render(
subj_pronoun=subj_pronoun, copula=copula, indef=indef, ucfirst=ucfirst,
choice=random.choice, obj_pronoun=obj_pronoun, synonyms=synonyms,
antonyms=antonyms, ADJECTIVE=ADJECTIVE, **args)
def elaborate_on_start_isa(lemma):
"""Check to for things that lemma is (i.e., hypernyms). Then grab a
property of that."""
nns = filter(lambda x: wn_filter_pos(x, NOUN), cn_start_isa(lemma))
if len(nns) == 0:
raise ElaborationImpossible
adjs = list()
for nn in nns[:2]:
adjs.extend(filter(lambda x: wn_filter_pos(x, ADJECTIVE), cn_start_has_property(nn)))
if len(adjs) == 0:
raise ElaborationImpossible
patterns = [
"{{adj}}",
"LEMMA-SUBJ LEMMA-COPULA{{choice([' not', ''])}} {{adj}}",
"LEMMA-SUBJ {{choice(['seemed', 'appeared'])}} {{adj}}",
"LEMMA-SUBJ did not {{choice(['seem', 'appear'])}} {{adj}}",
"you said that LEMMA-SUBJ LEMMA-COPULA{{choice([' not', ''])}} {{adj}}",
"LEMMA-SUBJ LEMMA-COPULA {{adj}} but not {{choice(synonyms(adj,pos=ADJECTIVE))}}"
]
adj = random.choice(adjs)
if len(antonyms(adj, pos=ADJECTIVE)) > 0:
patterns.append(
"LEMMA-SUBJ LEMMA-COPULA both {{adj}} and {{choice(antonyms(adj,pos=ADJECTIVE))}}")
if len(synonyms(adj, pos=ADJECTIVE)) > 0:
patterns.append(
"LEMMA-SUBJ LEMMA-COPULA {{adj}} and {{choice(synonyms(adj,pos=ADJECTIVE))}}")
return Elaboration(
render(random.choice(patterns), adj=adj, lemma=lemma),
{'adj': adj})
def elaborate_on_end_isa(lemma):
"""Check for things that are lemma (i.e., hyponyms)"""
nns = filter(lambda x: wn_filter_pos(x, NOUN), cn_end_isa(lemma))
if len(nns) == 0:
raise ElaborationImpossible
patterns = [
"LEMMA-SUBJ LEMMA-COPULA{{choice([' not', ''])}} {{indef(nn)}}",
"{{indef(nn)}}",
"you said LEMMA-SUBJ LEMMA-COPULA{{choice([' not', ''])}} {{indef(nn)}}",
]
nn = random.choice(nns)
if len(synonyms(nn)) > 0:
patterns.append(
"LEMMA-SUBJ LEMMA-COPULA {{indef(nn)}} but not {{indef(choice(synonyms(nn)))}}")
return Elaboration(
render(random.choice(patterns), nn=nn, lemma=lemma),
{'nn': nn})
def elaborate_on_start_has_property(lemma):
adjs = filter(lambda x: wn_filter_pos(x, ADJECTIVE),
cn_start_has_property(lemma))
if len(adjs) == 0:
raise ElaborationImpossible
patterns = [
"{{adj}}",
"LEMMA-SUBJ LEMMA-COPULA {{adj}}",
"LEMMA-SUBJ {{choice(['looked', 'seemed', 'appeared'])}} {{adj}}",
"you told me LEMMA-SUBJ LEMMA-COPULA {{adj}}",
]
adj = random.choice(adjs)
if len(antonyms(adj, pos=ADJECTIVE)) > 0:
patterns.extend([
"LEMMA-SUBJ {{choice(['looked', 'seemed', 'appeared'])}} {{adj}} and {{choice(antonyms(adj, pos=ADJECTIVE))}}",
"you told me LEMMA-SUBJ LEMMA-COPULA {{choice(antonyms(adj,pos=ADJECTIVE))}}"
])
return Elaboration(
render(random.choice(patterns), adj=adj, lemma=lemma),
{'adj': adj})
def elaborate_on_start_at_location(lemma):
"""lemma->at_location->something, i.e., 'nn is in lemma'"""
# either I'm getting this wrong, or some of the entries on conceptnet are
# getting it wrong
nns = filter(lambda x: wn_filter_pos(x, NOUN), cn_start_at_location(lemma))
if len(nns) == 0:
raise ElaborationImpossible
patterns = [
"LEMMA-SUBJ LEMMA-COPULA in {{indef(nn)}}",
"{{choice(['we', 'I', 'you'])}} {{choice(['found', 'saw'])}} LEMMA-OBJ in {{indef(nn)}}",
"LEMMA-SUBJ LEMMA-COPULA {{choice(['found', 'seen', 'discovered'])}} in {{indef(nn)}}"
]
nn = random.choice(nns)
return Elaboration(
render(random.choice(patterns), nn=nn, lemma=lemma),
{'nn': nn})
def elaborate_on_end_at_location(lemma):
"""something->at_location->lemma, i.e. 'lemma is in nn'"""
# either I'm getting this wrong, or some of the entries on conceptnet are
# getting it wrong
nns = filter(lambda x: wn_filter_pos(x, NOUN), cn_end_at_location(lemma))
if len(nns) == 0:
raise ElaborationImpossible
patterns = [
"{{indef(nn)}} was{{choice([' not', ''])}} in LEMMA-OBJ",
"{{indef(nn)}} was within LEMMA-OBJ",
"{{choice(['we', 'I', 'you'])}} found {{indef(nn)}} in LEMMA-OBJ",
"{{choice(['we', 'I', 'you'])}} found {{indef(nn)}} {{choice(['within', 'there'])}}"
]
nn = random.choice(nns)
return Elaboration(
render(random.choice(patterns), nn=nn, lemma=lemma),
{'nn': nn})
def random_conjoin(strs):
out = list()
i = 0
while i < len(strs)-1:
if (len(strs[i].split()) >= 3 and len(strs[i+1].split()) >= 3) \
and random.randrange(2) == 0:
out.append(', and '.join([strs[i], strs[i+1]]))
i += 2
else:
out.append(strs[i])
i += 1
if i < len(strs):
out.append(strs[-1])
return [ucfirst(s) + "." for s in out]
def parenthesized(strs):
out = list()
if len(strs) <= 1: return strs
count = 0
for i, s in enumerate(strs):
if i > 0 and random.randrange(5) == 0 and count == 0:
out.append("(" + s + ")")
count += 1
else:
out.append(s)
return out
def elaborate_on(lemma):
elaborations = [
elaborate_on_start_isa,
elaborate_on_end_isa,
elaborate_on_start_has_property,
elaborate_on_start_at_location,
elaborate_on_end_at_location]
to_elaborate = lemma.decode('utf8')
elaboration_strs = []
repeat_lemma = True
random.shuffle(elaborations)
for elab_func in elaborations:
try:
elab = elab_func(to_elaborate)
except ElaborationImpossible:
continue
# hack cohesion things. this is really primitive but it does the job and
# sometimes makes the text pleasantly weird
elab_text = ""
if repeat_lemma:
article = random.choice(['the', 'my', 'your', 'our', 'this'])
elab_text = elab.text.replace('LEMMA-SUBJ', article + ' ' + to_elaborate)
elab_text = elab_text.replace('LEMMA-COPULA', 'was')
elab_text = elab_text.replace('LEMMA-OBJ', article + ' ' + to_elaborate)
repeat_lemma = False
else:
elab_text = elab.text.replace('LEMMA-SUBJ', subj_pronoun(to_elaborate))
elab_text = elab_text.replace('LEMMA-COPULA', copula(to_elaborate))
elab_text = elab_text.replace('LEMMA-OBJ', obj_pronoun(to_elaborate))
elaboration_strs.append(elab_text)
if 'nn' in elab.further and random.randrange(2) == 0:
to_elaborate = elab.further['nn']
repeat_lemma = True
conjoined = random_conjoin(elaboration_strs)
parens = parenthesized(conjoined)
return ' '.join(parens)
if __name__ == '__main__':
from pprint import pprint
print elaborate_on(sys.argv[1])