-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathwhat_who.py
40 lines (31 loc) · 1006 Bytes
/
what_who.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
from nltk.tree import Tree as Tree
from parse_sentence import *
from binary import *
from nltk import word_tokenize, pos_tag, ne_chunk
# sNLP = StanfordNLP()
Binary = Binary()
class What_Who:
def is_who(self, text, NE):
for ne in NE:
if ne in text:
return True
tt = ne_chunk(pos_tag(word_tokenize(text)))
for s in tt.subtrees(lambda t: t.label() == "PERSON"):
return True
return False
def main(self, text, NE):
tree = sNLP.parse(text)
tree = Tree.fromstring(str(tree))
(top_level_structure, parse_by_structure) = Binary.get_top_level_structure(tree)
np_index = top_level_structure.index("NP")
if self.is_who(parse_by_structure[np_index], NE):
parse_by_structure[np_index] = "who"
else:
parse_by_structure[np_index] = "what"
parse_by_structure[-1] = "?"
sent = " ".join(parse_by_structure)
print(sent)
return sent
# What_Who = What_Who()
# What_Who.main("Dempsey is the firt place.", ["Dempsey"])
# What_Who.main("Earth orbits the sun.", ["Dempsey"])