-
Notifications
You must be signed in to change notification settings - Fork 15
/
nlg.py
437 lines (354 loc) · 14.2 KB
/
nlg.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
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
# nlg.py
import random
import datetime
from py4j_server import launch_py4j_server
from py4j.java_gateway import java_import
gateway = launch_py4j_server()
# Import the SimpleNLG classes
java_import(gateway.jvm, "simplenlg.features.*")
java_import(gateway.jvm, "simplenlg.realiser.*")
# Define aliases so that we don't have to use the gateway.jvm prefix.
NPPhraseSpec = gateway.jvm.NPPhraseSpec
PPPhraseSpec = gateway.jvm.PPPhraseSpec
SPhraseSpec = gateway.jvm.SPhraseSpec
InterrogativeType = gateway.jvm.InterrogativeType
Realiser = gateway.jvm.Realiser
TextSpec = gateway.jvm.TextSpec
Tense = gateway.jvm.Tense
Form = gateway.jvm.Form
class NLG(object):
"""
Used to generate natural language. Most of these sections are hard coded. However, some use simpleNLG which is
used to string together verbs and nouns.
"""
def __init__(self, user_name=None):
self.user_name = user_name
# make random more random by seeding with time
random.seed(datetime.datetime.now())
def acknowledge(self):
user_name = self.user_name
if user_name is None:
user_name = ""
simple_acknoledgement = [
"Yes?",
"What can I do for you?",
"How can I help?"
]
personal_acknowledgement = [
"How can I help you today, %s" % user_name,
"How can I help you, %s" % user_name,
"What can I do for you, %s" % user_name,
"Hi %s, what can I do for you?" % user_name,
"Hey %s, what can I do for you?" % user_name
]
choice = 0
if self.user_name is not None:
choice = random.randint(0, 2)
else:
choice = random.randint(0,1)
ret_phrase = ""
if choice == 0:
ret_phrase = random.choice(simple_acknoledgement)
elif choice == 1:
date = datetime.datetime.now()
ret_phrase = "Good %s. What can I do for you?" % self.time_of_day(date)
else:
ret_phrase = random.choice(personal_acknowledgement)
return ret_phrase
def searching(self):
searching_phrases = [
"I'll see what I can find"
]
return random.choice(searching_phrases)
def snow_white(self):
phrases = [
"You are",
"You",
"You are, of course"
]
return random.choice(phrases)
def user_status(self, type='positive', attribute=None):
ret_phrase = ""
positive_complements = [
"good",
"nice",
"great",
"perfect",
"Beautiful"
]
negative_complements = [
"bad",
"terrible"
]
moderate_complements = [
"alright",
"okay"
]
complement_choice = positive_complements
if type == 'negative':
complement_choice = negative_complements
elif type == 'moderate':
complement_choice = moderate_complements
if attribute is None:
ret_phrase = "You look %s" % random.choice(complement_choice)
else:
ret_phrase = self.generate('none', {'subject': "Your %s" % attribute, 'verb': 'look %s' % random.choice(complement_choice)}, "present")
return ret_phrase
def personal_status(self, status_type=None):
positive_status=[
"I'm doing well",
"Great, thanks for asking",
"I'm doing great"
]
negative_status = [
"I'm not doing well",
"I'm feeling terrible",
"I'm not doing well today",
"I could be much better"
]
moderate_status = [
"I'm doing alright",
"I'm okay",
"I could be better",
"I'm alright"
]
if status_type == 'negative':
return random.choice(negative_status)
elif status_type == 'moderate':
return random.choice(moderate_status)
return random.choice(positive_status)
def joke(self):
jokes = [
"Artificial intelligence is no match for natural stupidity.",
"This morning I made a mistake and poured milk over my breakfast instead of oil, and it rusted before I could eat it.",
"An Englishman, an Irishman and a Scotsman walk into a bar. The bartender turns to them, takes one look, and says, \"What is this - some kind of joke?\"",
"What's an onomatopoeia? Just what it sounds like!",
"Why did the elephant cross the road? Because the chicken retired.",
"Today a man knocked on my door and asked for a small donation towards the local swimming pool. I gave him a glass of water.",
"A recent study has found that women who carry a little extra weight live longer than the men who mention it.",
"I can totally keep secrets. It's the people I tell them to that can't.",
"My therapist says I have a preoccupation with vengeance. We'll see about that.",
"Money talks ...but all mine ever says is good-bye.",
"I started out with nothing, and I still have most of it.",
"I used to think I was indecisive, but now I'm not too sure.",
"I named my hard drive dat ass so once a month my computer asks if I want to 'back dat ass up'.",
"A clean house is the sign of a broken computer.",
"My favorite mythical creature? The honest politician.",
"Regular naps prevent old age, especially if you take them while driving.",
"For maximum attention, nothing beats a good mistake.",
"Take my advice. I'm not using it."
]
return random.choice(jokes)
def news(self, tense):
headlines_nouns = [
"stories",
"articles",
"headlines",
]
headlines_adjectives = [
["these"],
["some"],
["a", "few"],
["a", "couple"]
]
headlines_prepmodifiers = [
"you"
]
choice = random.randint(0, 1)
if choice == 1:
ret_phrase = self.generate('none', {'subject': "I", 'object': random.choice(headlines_nouns), 'verb': 'find', 'objmodifiers': random.choice(headlines_adjectives), 'preposition': 'for', 'prepmodifiers': [random.choice(headlines_prepmodifiers)]}, tense)
else:
ret_phrase = self.generate('none', {'subject': "I", 'object': random.choice(headlines_nouns), 'verb': 'find', 'objmodifiers': random.choice(headlines_adjectives)}, tense)
return ret_phrase
def article_interest(self, article_titles):
ret_phrase = None
if random.randint(0,2) == 0: # 1 in 3 chance the bot will express interest in a random article
if article_titles is not None:
article = random.choice(article_titles)
article = article.rsplit('-', 1)[0]
ret_phrase = "%s sounds particularly interesting" % article
return ret_phrase
def insult(self):
return "That's not very nice. Talk to me again when you have fixed your attitude"
def greet(self):
"""
Creates a greeting phrase.
:return:
"""
greeting_words = [
"Hi",
"Hey",
"Hello"
]
goofy_greetings = [
"what's up?",
"howdy",
"what's crackin'?",
"top of the morning to ya"
]
choice = random.randint(0,4)
ret_phrase = ""
if (choice == 0) or (choice == 3): # time related
ret_phrase = "Good %s" % self.time_of_day(datetime.datetime.now())
if self.user_name is not None:
if random.randint(0, 1) == 0:
ret_phrase = "%s %s" % (ret_phrase, self.user_name)
elif (choice == 1) or (choice == 4): # standard greeting
ret_phrase = random.choice(greeting_words)
if self.user_name is not None:
if random.randint(0, 1) == 0:
ret_phrase = "%s %s" % (ret_phrase, self.user_name)
elif choice == 2: # goofy greeting
ret_phrase = random.choice(goofy_greetings)
return ret_phrase
def weather(self, temperature, date, tense):
"""
Generates a statement about the current weather.
:param temperature:
:param date:
:param tense:
:return:
"""
ret_phrase = self.generate('none', {'subject':"the temperature", 'object': "%d degrees" % temperature, 'verb': 'is', 'adverbs': ["%s" % self.time_of_day(date, with_adjective=True)]}, tense)
return ret_phrase
def forecast(self, forecast_obj):
ret_phrase = ""
forecast = ""
if forecast_obj.get("forecast") is None:
return ret_phrase
else:
forecast = forecast_obj.get("forecast")
forecast_current = [
"Currently, it's",
"Right now, it's",
"At the moment, it's",
"It's",
"It is"
]
forecast_hourly = [
"It's",
"It will be",
"Looks like it will be"
]
forecast_daily = [
""
]
if forecast_obj.get('forecast_type') == "current":
ret_phrase = "%s %s" % (random.choice(forecast_current), forecast)
elif forecast_obj.get('forecast_type') == "hourly":
ret_phrase = "%s %s" % (random.choice(forecast_hourly), forecast)
elif forecast_obj.get('forecast_type') == "daily":
ret_phrase = "%s %s" % (random.choice(forecast_daily), forecast)
return ret_phrase
def appreciation(self):
phrases = [
"No problem!",
"Any time",
"You are welcome",
"You're welcome",
"Sure, no problem",
"Of course",
"Don't mention it",
"Don't worry about it"
]
return random.choice(phrases)
def holiday(self, holiday_name):
phrases = [
"",
"Looks like the next holiday is ",
"The next important day is "
]
return "%s%s" % (random.choice(phrases), holiday_name)
def meaning_of_life(self):
phrases = [
"42",
"The meaning of life, the universe, and everything else is 42"
]
return random.choice(phrases)
def name(self):
return self.user_name
def time_of_day(self, date, with_adjective=False):
ret_phrase = ""
if date.hour < 10:
ret_phrase = "morning"
if with_adjective:
ret_phrase = "%s %s" % ("this", ret_phrase)
elif (date.hour >= 10) and (date.hour < 18):
ret_phrase = "afternoon"
if with_adjective:
ret_phrase = "%s %s" % ("this", ret_phrase)
elif date.hour >= 18:
ret_phrase = "evening"
if with_adjective:
ret_phrase = "%s %s" % ("this", ret_phrase)
return ret_phrase
def generate(self, utter_type, keywords, tense=None):
"""
Input: a type of inquiry to create and a dictionary of keywords.
Types of inquiries include 'what', 'who', 'where', 'why', 'how',
and 'yes/no' questions. Alternatively, 'none' can be specified to
generate a declarative statement.
The dictionary is essentially divided into three core parts: the
subject, the verb, and the object. Modifiers can be specified to these
parts (adverbs, adjectives, etc). Additionally, an optional
prepositional phrase can be specified.
Example:
nlg = NaturalLanguageGenerator(logging.getLogger())
words = {'subject': 'you',
'verb': 'prefer',
'object': 'recipes',
'preposition': 'that contains',
'objmodifiers': ['Thai'],
'prepmodifiers': ['potatoes', 'celery', 'carrots'],
'adverbs': ['confidently'],
}
nlg.generate('yes_no', words)
u'Do you confidently prefer Thai recipes that contains potatoes, celery and carrots?'
nlg.generate('how', words)
u'How do you confidently prefer Thai recipes that contains potatoes, celery and carrots?'
"""
utterance = SPhraseSpec()
subject = NPPhraseSpec(keywords['subject'])
target = None
if 'object' in keywords:
target = NPPhraseSpec(keywords['object'])
preposition = PPPhraseSpec()
if 'preposition' in keywords:
preposition.setPreposition(keywords['preposition'])
if 'prepmodifiers' in keywords:
for modifier in keywords['prepmodifiers']:
preposition.addComplement(modifier)
if 'submodifiers' in keywords:
for modifier in keywords['submodifiers']:
subject.addModifier(modifier)
if 'objmodifiers' in keywords:
for modifier in keywords['objmodifiers']:
target.addModifier(modifier)
if utter_type.lower() == 'yes_no':
utterance.setInterrogative(InterrogativeType.YES_NO)
elif utter_type.lower() == 'how':
utterance.setInterrogative(InterrogativeType.HOW)
elif utter_type.lower() == 'what':
utterance.setInterrogative(InterrogativeType.WHAT)
elif utter_type.lower() == 'where':
utterance.setInterrogative(InterrogativeType.WHERE)
elif utter_type.lower() == 'who':
utterance.setInterrogative(InterrogativeType.WHO)
elif utter_type.lower() == 'why':
utterance.setInterrogative(InterrogativeType.WHY)
if target is not None:
target.addModifier(preposition)
utterance.setSubject(subject)
utterance.setVerb(keywords['verb'])
if 'adverbs' in keywords:
for modifier in keywords['adverbs']:
utterance.addModifier(modifier)
if target is not None:
utterance.addComplement(target)
if tense.lower() == 'future':
utterance.setTense(Tense.FUTURE)
elif tense.lower() == 'past':
utterance.setTense(Tense.PAST)
realiser = Realiser()
output = realiser.realiseDocument(utterance).strip()
return output