-
Notifications
You must be signed in to change notification settings - Fork 1
/
runInput.py
84 lines (70 loc) · 2.56 KB
/
runInput.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
from filters.text2parse import client
from filters.parse2predicate import stringParser, treeToString
import sys
sys.path.append('/home/mlc/BobGoesToJail/judge')
from judge.judger import run as judge
def getParseTrees(case):
parsed = nlp.parse(case)
for sent in parsed["sentences"]:
yield sent["parsetree"]
def getListFromTrees(parseTrees):
for pTree in parseTrees:
listOfTuples = treeToString.treeToString(str(pTree))
yield listOfTuples
def getRuledTrees(parseTrees):
for pTree in parseTrees:
treeWithRules = stringParser.stringParser(pTree)
yield treeWithRules
def process_case(case):
global nlp
nlp = client.StanfordNLP()
#Run Stanford NLP
parseTrees = getParseTrees(case)
#Run all the tree rules
ruledTrees = getRuledTrees(parseTrees)
#Convert the tree to lists of tuples
cWords = []
for t in getListFromTrees(ruledTrees):
cWords.append(t)
print t
return cWords
def get_bob(caseWordsList):
bob = []
nonBob = []
for caseWords in caseWordsList:
for i in range(len(caseWords)):
if caseWords[i][1][0].lower() == "bob" or caseWords[i][1][0].lower() == "john":
bob.append(caseWords[i][1][0].strip())
else:
nonBob.append(caseWords[i][1])
bob = list(set(bob))
return (bob, nonBob)
def run(inputCase):
print inputCase
caseWords = process_case(inputCase)
bobs, nonBobs = get_bob(caseWords)
actions = []
for n in nonBobs:
actions.append(" ".join(n).strip())
print "Action: ", actions
case = { "Persons" : bobs,
"Action": { "Bob" : actions},
"Act" : inputCase
}
print case
return judge(case)
#~ case = { "Persons" : ["Bob"],
#~ "Action": { "Bob" : ["induces", "killed a human", "carelessness"]},
#~ "Act" : "Bob causes death of a human through shortcoming"}
if __name__ == "__main__":
print "Welcome to BobGoesToJail?"
print "Example input: Bob causes death of a human through carelessness\nPlease input a case:"
#inputCase = raw_input()
inputCase = raw_input()
#inputCase = "Bob negligently induces physical harms to another human" # works
#inputCase = "Bob killed a human by carelessness" # works
#inputCase = "Bob knowingly interrupts a burial" # works
#inputCase = "Bob unknowingly interrupts a burial" # if it works, it should find nothing
#inputCase = "Bob disparages memory of a dead person" # works
output = run(inputCase)
#~ print "Output: ", output