-
Notifications
You must be signed in to change notification settings - Fork 13
/
NLG.py
executable file
·66 lines (63 loc) · 2.91 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
#!/usr/bin/env python
import random
#import galbackend_cnn
def GenerateResponsePair(TopicLevel, Candidates, refine_strategy=-1):
if TopicLevel==-1: #off topic
output = 'Ok. Tell me more about yourself.'
else:
select = random.choice(Candidates)
pair = [select[1], select[2]]
output = [" ".join(pair[0]), " ".join(pair[1])]
return output
def FillTemplate(theme, TemplateLib, TopicLib, template, init_id, joke_id,more_id, engaged_input, answer=[],output=[]):
#global topic_id,init_id,joke_id,more_
sent_list = []
for item in template:
for unit in item.split(','):
#print 'this is the unit' +unit
if unit == 'oov' or unit =='name_entity' or unit =='short_answer' or unit =='not_repeat':
sent_list.append(output)
elif unit == 'answer':
output = ' '.join(answer)
punc_list = [".",",","?","'","!"]
for punc in punc_list:
if punc in output:
output = output.replace(' '+punc,punc)
sent_list.append(output)
elif unit == 'template_back' and len(engaged_input)<1:
continue
elif unit == 'topic_back':
if len(engaged_input)>0:
sent_list.append(engaged_input[0])
sent_list.append('do you want to talk more about that?')
engaged_input.pop(0)
else:
unit = random.choice(['joke','init','switch'])
elif unit == 'topic':
#print topic_id
topic_list = [topic for topic in TopicLib if topic != theme]
#print topic_list
#print 'theme' +theme +'\n'
theme = random.choice(topic_list)
sent_list.append(theme)
elif unit == 'template_init':
#print TemplateLib['template_init']
# here we use initiation that is attached to certain topic.
init_index = init_id %len(TemplateLib['template_init'][theme])
sent_list.append(TemplateLib['template_init'][theme][init_index])
init_id = init_id + 1
elif unit == 'template_joke':
# we use joke that is attached to certain topic.
#print 'The theme is ' + theme
#print TemplateLib['template_joke']
joke_index = joke_id%len(TemplateLib['template_joke'][theme])
sent_list.append(TemplateLib['template_joke'][theme][joke_index])
joke_id = joke_id + 1
elif unit == 'template_more':
more_index = more_id%len(TemplateLib['template_more'])
sent_list.append(TemplateLib['template_more'][more_index])
more_id = more_id + 1
else:
sent_list.append(random.choice(TemplateLib[unit]))
#pr
return theme, ' '.join(sent_list), init_id, joke_id, more_id, engaged_input