forked from corinnelhh/chatbot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoutput_filters.py
185 lines (163 loc) · 5.98 KB
/
output_filters.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
import nltk
from nltk import pos_tag
from nltk.tokenize import wordpunct_tokenize
from collections import OrderedDict
import random
funct_dict = OrderedDict({})
grammar1 = nltk.parse_cfg("""
Sent -> NP VP | NP VP END
NP -> Det Nom | PropN | Det NP | N | PR | PR Nom
Nom -> Adj Nom | N
VP -> V Adj | V NP | V S | V NP PP | V Prep NP | V | V CC V
PP -> Prep NP
PropN -> 'NNP' | 'NNPS'
Det -> 'DT'
N -> 'NN' | 'NNS'
Adj -> 'JJ' | 'JJR' | 'JJS'
V -> 'VB' | 'VBD' | 'VBG' | 'VBN' | 'VBP' | 'VBZ'
Prep -> 'TO' | 'IN'
CC -> 'CC'
PR -> 'PRP' | 'PRP$'
RB -> 'RB' | 'RBR' | 'RBS'
END -> '.' | '?' | '!'
""")
def add_func_to_dict(name=None):
def wrapper(func):
function_name = name
if function_name is None:
function_name = func.__name__
funct_dict[function_name] = func, func.__doc__
return func
return wrapper
@add_func_to_dict("No Filter Selected")
def no_o_filter_selected(sentences, bot_dict):
u"""All generated sentences are passed along for
random selection."""
return sentences
@add_func_to_dict("Length Filter")
def filter_length(sentences, bot_dict):
u"""Only sentences with a length <= 8 are passed
along for random selection."""
wordcount = 8
output_sentences = []
for sentence in sentences[:]:
sentence = sentence.split()
if len(sentence) <= wordcount:
output_sentences.append(" ".join(sentence))
return output_sentences
@add_func_to_dict("Part of Speech Filter")
def filter_pos(sentences, bot_dict):
u"""Only sentences containing at least one
verb, noun, or adjective are passed along
for random selection."""
content_pos = ['VB', 'NN', 'JJ']
output_sentences = []
for sentence in sentences:
words = wordpunct_tokenize(sentence)
tagged = pos_tag(words)
for word, pos in tagged:
if pos[:2] in content_pos:
output_sentences.append(sentence)
break
return output_sentences
@add_func_to_dict("Noun-Verb Filter")
def filter_NN_VV(sentences, bot_dict):
u"""Only sentences containing at least one noun followed
somewhere by at least one verb are passed along for
random selection."""
output_sentences = []
for sentence in sentences:
words = wordpunct_tokenize(sentence)
tagged = pos_tag(words)
has_noun = False
for word, tag in tagged:
if tag[:2] == "NN":
has_noun = True
if has_noun and tag[:2] == "VB":
output_sentences.append(sentence)
break
return output_sentences
@add_func_to_dict("Noun-Verb-Noun Filter")
def weak_syntactic_filter(sentences, bot_dict):
u"""Only sentences containing at least one noun followed
somewhere by at least one verb followed by at least one
noun are passed along for random selection."""
output_sentences = []
noms = ["NN", "PR"]
print "first we had {} sentences.".format(len(sentences))
for sentence in sentences:
has_NN = False
has_VV = False
passes = False
tagged_tokens = pos_tag(wordpunct_tokenize(sentence))
print tagged_tokens
for word, tag in tagged_tokens:
if tag[:2] in noms:
has_NN = True
if has_NN and tag[:2] == "VB":
has_VV = True
if has_NN and has_VV and tag[:2] in noms:
passes = True
if passes:
output_sentences.append(sentence)
print "*************"
print sentence
print "Then we had {} sentences".format(len(output_sentences))
return output_sentences
@add_func_to_dict("Syntactic Filter Fast")
def syntactic_filter_fast(sentences, bot_dict):
u"""Only sentences with an underlying structure
matching a given content-free grammar are passed
along for random selection. Filters responses
through looking up the part of speech for input words
in a local lexicon and recursively mapping
phrase structures."""
output_sentences = []
print "Before syntax filter there were " + str(len(sentences)) + " sentences."
for sentence in sentences:
print "=================="
print str(sentence) + "\n"
tokens = nltk.tokenize.wordpunct_tokenize(sentence)
justTags = []
# print self.pos_lexicon_word_pos
for word in tokens[:-1]:
tag = random.choice(bot_dict[word])
justTags.append(tag)
justTags.append(tokens[-1])
print str(justTags) + "\n"
rd_parser = nltk.RecursiveDescentParser(grammar1)
try:
if len(rd_parser.nbest_parse(justTags)) > 0:
output_sentences.append(sentence)
except ValueError:
pass
print "After the syntax filter there were " + str(len(output_sentences)) + " sentences."
print output_sentences
return output_sentences
@add_func_to_dict("Syntactic Filter")
def syntactic_filter(sentences, bot_dict):
u"""Only sentences with an underlying structure
matching a given content-free grammar are passed
along for random selection. Filters responses
through part of speech tagging and
recursive structure lookup."""
output_sentences = []
print "Before syntax filter there were " + str(len(sentences)) + " sentences."
for sentence in sentences:
print "=================="
print str(sentence) + "\n"
tokens = nltk.tokenize.wordpunct_tokenize(sentence)
posTagged = nltk.pos_tag(tokens)
justTags = []
for word, tag in posTagged:
justTags.append(tag)
print str(justTags) + "\n"
rd_parser = nltk.RecursiveDescentParser(grammar1)
try:
if len(rd_parser.nbest_parse(justTags)) > 0:
output_sentences.append(sentence)
except ValueError:
pass
print "After the syntax filter there were " + str(len(output_sentences)) + " sentences."
print output_sentences
return output_sentences