-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrepl.py
More file actions
175 lines (153 loc) · 5.58 KB
/
Copy pathrepl.py
File metadata and controls
175 lines (153 loc) · 5.58 KB
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
#!/usr/bin/python
################################################################################
import sys
import cmd
import sqlite3
import traceback
import earley
import logic_to_sql
class SimpleREPL(cmd.Cmd):
def __init__(self, stream):
print repr(stream)
cmd.Cmd.__init__(self, "Tab", stream)
self.prompt = ">> "
self.intro = """
This is a simple Like-Hate query console.
There are a few service commands:
.init Creates all tables
.fini Drops all tables
.dump Dumps all tables
.debug Enables/disables NLP debugging
.trace Enables/disables SQL tracing
"""
self.interactive = (stream == sys.stdin)
self.connection = sqlite3.connect("example.db")
self.grammar = earley.load_grammar(open("repl.txt", "r").readlines())
self.debug = False
self.trace = False
if not self.interactive:
self.use_rawinput = False
def _execute(self, query):
if self.trace:
print "<", query
cursor = self.connection.cursor()
for row in cursor.execute(query):
yield row
cursor.close()
self.connection.commit()
def _execute_sync(self, query):
for row in self._execute(query):
pass
def cmd_init(self):
self._execute_sync("CREATE TABLE my_symm_borders(arg0 TEXT, arg1 TEXT)")
self._execute_sync("CREATE TABLE my_symm_wars(arg0 TEXT, arg1 TEXT)")
self._execute_sync("CREATE TABLE my_symm_ally(arg0 TEXT, arg1 TEXT)")
self._execute_sync("CREATE TABLE my_situated(arg0 TEXT, arg1 TEXT)")
def cmd_fini(self):
self._execute_sync("DROP TABLE my_symm_borders")
self._execute_sync("DROP TABLE my_symm_wars")
self._execute_sync("DROP TABLE my_symm_ally")
self._execute_sync("DROP TABLE my_situated")
def cmd_clear(self):
self._execute_sync("DELETE FROM my_symm_borders")
self._execute_sync("DELETE FROM my_symm_wars")
self._execute_sync("DELETE FROM my_symm_ally")
self._execute_sync("DELETE FROM my_situated")
def cmd_debug(self):
if self.debug:
self.debug = False
print "NLP debugging disabled."
else:
self.debug = True
print "NLP debugging enabled."
def cmd_trace(self):
if self.trace:
self.trace = False
print "SQL tracing disabled."
else:
self.trace = True
print "SQL tracing enabled."
def cmd_dump(self):
print "== Borders =" + "=" * 70
for row in self._execute("SELECT * FROM my_symm_borders"):
print ":", "Borders(%s)" % ", ".join(row)
print "== Wars =" + "=" * 70
for row in self._execute("SELECT * FROM my_symm_wars"):
print ":", "Wars(%s)" % ", ".join(row)
print "== Ally =" + "=" * 70
for row in self._execute("SELECT * FROM my_symm_ally"):
print ":", "Ally(%s)" % ", ".join(row)
print "== Situated =" + '=' * 70
for row in self._execute("SELECT * FROM my_situated"):
print ":", "Situated(%s)" % ", ".join(row)
def cmd_eval(self, semantics):
for query in logic_to_sql.SqlGenerator().make_sql(semantics):
yes_no_select = query.endswith('-- yes_no_select')
count_select = query.endswith('-- count_select')
if yes_no_select:
try:
self._execute(query).next()
print "YES"
except StopIteration:
print "NO"
elif count_select:
count = 0
for row in self._execute(query):
count += 1
print count
else:
for row in self._execute(query):
print ":", " ".join(row)
def emptyline(self):
pass
def default(self, string):
try:
if not self.interactive:
print string
if string == ".init":
self.cmd_init()
elif string == ".fini":
self.cmd_fini()
elif string == ".clear":
self.cmd_clear()
elif string == ".debug":
self.cmd_debug()
elif string == ".trace":
self.cmd_trace()
elif string == ".dump":
self.cmd_dump()
elif string == "what is the meaning of life":
print "42."
else:
variants = earley.parse(self.grammar, string)
if len(variants) == 0:
print '(!) Unable to parse query.'
elif len(variants) > 1:
print '(!) Query is ambiguous.'
for semantics, tree in variants:
print " ", earley.qtree(tree)
else:
semantics, tree = variants[0]
if self.debug:
print
print "T=", string
print "Q=", earley.qtree(tree)
print "S=", semantics
print "S=", semantics.simplify()
print
self.cmd_eval(semantics.simplify())
print
print "Okay."
print
except RuntimeError as e:
traceback.print_exc()
def do_EOF(self, line):
print
print "Ciao!"
return True
if __name__ == "__main__":
if len(sys.argv) > 1:
stream = open(sys.argv[1], "r")
else:
stream = sys.stdin
SimpleREPL(stream).cmdloop()