-
Notifications
You must be signed in to change notification settings - Fork 0
/
SenToSym.py
134 lines (115 loc) · 3.45 KB
/
SenToSym.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
'''
Anthony Escobar
SenToSym.py
A Simple tool to convert a sentance into it's symbolic logic.
Citation:
Bird, Steven, Edward Loper and Ewan Klein (2009), Natural Language Processing with Python. O’Reilly Media Inc.
'''
from SymTree import SymTree
from SymNode import SymNode
import nltk
def run(s,options=""):
if "d" in options:
demo()
return
t = SymTree(SymNode(nltk.word_tokenize(s)[:len(nltk.word_tokenize(s))-1]))
if not t.poss():
print("The sentence given cannot be identified.")
return
if "r" in options:
print("Input: " + s)
if "s" in options:
t.isoSubject()
for subj in t.getSubj():
print("Subject: " + subj)
t.identify()
t.expand()
print(repr(t))
if "c" in options:
print("Type: " + t.getCond())
print(t)
######################## TESTING FUNCTIONS ####################################
def first():
s = 'I will get an A when I have the time to study.'
print(s)
stt = nltk.pos_tag(nltk.word_tokenize(s))
print(stt)
sttne = nltk.chunk.ne_chunk(stt)
print(sttne)
def theProcess1():
sentence = "If I have the time to study then I will get an A."
print("Input: " + sentence)
tokens = nltk.word_tokenize(sentence)
t = SymTree(SymNode(tokens[:len(tokens)-1]))
# print(t)
t.isoSubject()
for subj in t.getSubj():
print("Subject: " + subj)
t.identify()
t.expand()
print(repr(t))
print("Type: " + t.getCond())
print(t)
def theProcess2():
sentence = "Forneybots were found to malfumction if and only if they suffer water damage or overheard a logical paradox."
print("Input: " + sentence)
tokens = nltk.word_tokenize(sentence)
t = SymTree(SymNode(tokens[:len(tokens)-1]))
# print(t)
t.isoSubject()
for subj in t.getSubj():
print("Subject: " + subj)
t.identify()
t.expand()
print(repr(t))
print("Type: " + t.getCond())
print(t)
def theProcess3():
sentence = "I will get an A if I have time to study."
print(sentence)
tokens = nltk.word_tokenize(sentence)
print(tokens)
t = SymTree(SymNode(tokens[:len(tokens)-1]))
# print(t)
t.identify()
# print(t)
t.expand()
print(repr(t))
print(t)
def playingAround():
print("Testing the NLTK functions:\n")
sentence = """At eight o'clock on Thursday morning Arthur didn't feel very good."""
print(sentence)
print("\nSentence Tokenized")
tokens = nltk.word_tokenize(sentence)
print(tokens)
print("\nTokens Tagged")
tagged = nltk.pos_tag(tokens)
print(tagged)
print("\nIdentify named entities")
entities = nltk.chunk.ne_chunk(tagged)
print(entities)
print("\nMultipule Sentences now")
sentences = '''Good muffins cost $3.88\nin New York. Please buy me two of them.\n\nThanks.'''
print(sentences)
print("\nUse sent_tokenize(s)")
sentTokens = nltk.sent_tokenize(sentences)
print(sentTokens)
print("Use word_tokenize(s) on each sentance in the array")
arrTokens = []
for s in sentTokens:
arrTokens.append(nltk.word_tokenize(s))
print(arrTokens)
def demo():
theProcess1()
print("")
theProcess2()
if __name__ == "__main__":
playingAround()
# print("\n---------------------")
# first()
# theProcess1("")
# print("\n---------------------")
# theProcess2("")
# print("\n---------------------")
# theProcess3("")