-
Notifications
You must be signed in to change notification settings - Fork 2
/
regex_demo.py
182 lines (137 loc) · 4.29 KB
/
regex_demo.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
# -*- coding: utf-8 -*-
from quepy.regex import *
class Example_1(RegexTemplate):
test_string = u"What is aluminum"
def make_regex(self):
regex = Token("What") + Lemma("be") + Pos("NN")
return regex
def semantics(self, match):
return match
class Example_2(RegexTemplate):
test_string = u"What is jumped"
def make_regex(self):
regex = Token("What") + Lemma("be") + Pos("NN")
return regex
def semantics(self, match):
return match
class Example_3(RegexTemplate):
test_string = u"What is love"
def make_regex(self):
regex = Pos("WP") + Lemma("be") + Thing()
return regex
def semantics(self, match):
return match
class Example_4(RegexTemplate):
test_string = u"a a b b a a a"
def make_regex(self):
a = Token("a")
b = Token("b")
regex = Star((a + a) | b) + a
return regex
def semantics(self, match):
return match
class Example_8(RegexTemplate):
test_string = u"How long is The Neverending Story?"
def make_regex(self):
A = Pos("WP") + Lemmas("be the duration of")
B = Lemma("how") + Lemma("long") + Lemma("be")
regex = (A | B) + Movie() + Pos(".")
return regex
def semantics(self, match):
return match
class Example_9(RegexTemplate):
test_string = u"The Matrix"
def make_regex(self):
class Movie(Particle):
regex = Question(Pos("DT")) + \
Plus(Pos("NN") | Pos("NNS") | Pos("NNP") | Pos("NNPS"))
def semantics(self, match):
return match.words.tokens
regex = Movie()
return regex
def semantics(self, match):
return match
###
### Begin boilerplate
###
import inspect
from quepy.tagger import get_tagger
from quepy import settings
from refo import Question, Plus
# Put your nltk path here:
settings.NLTK_DATA_PATH = ["/home/rafael/deploy/nltk/data"]
tagger = get_tagger()
class Thing(Particle):
regex = Any()
def semantics(self, match):
return match.words.tokens
class Movie(Particle):
regex = Question(Pos("DT")) + \
Plus(Pos("NN") | Pos("NNS") | Pos("NNP") | Pos("NNPS"))
def semantics(self, match):
return match.words.tokens
HTML_TEMPLATE = """
<html><body>
<table style="border:0" align="center">
<tr>
<td colspan="3"><h2 style="text-align: center"> </h2></td>
</tr>
<tr><td colspan="3"><hr /></td></tr>
{rows}
</table>
</body></html>
"""
QUERY_TEMPLATE = """
<tr>
<td colspan="3"><h2 style="text-align: center">{name}</h2></td>
</tr>
<tr>
<td>
<b style="text-align: center"></b><br /> <pre>{code}</pre> </td>
<td style="padding-left: 30px">
<b style="text-align: center"></b><br /> {string}
<br /> {understood} </td>
<td style="padding-left: 30px">
<b style="text-align: center"></b><br /> <pre>{groups}</pre> </td>
</tr>
<tr>
<td colspan="3"><hr /></td>
</tr>
"""
def templates_to_html(templates):
rows = ""
for template in templates:
try:
string = template.test_string
except:
continue
#name = template.__name__.capitalize()
name = ""
code = inspect.getsource(template.make_regex)
code = code.split("\n")
code = "\n".join(code[1:-2])
words = list(tagger(string))
instance = template()
instance.regex = instance.make_regex()
match, _ = instance.get_semantics(words)
groups = "None"
if match is not None:
groups = {}
for particle in match._particles:
groups[particle] = getattr(match, particle)
rows += QUERY_TEMPLATE.format(name=name,
code=code,
string=string,
understood=" ".join(map(str, words)),
groups=groups)
html = HTML_TEMPLATE.format(rows=rows)
with open("regex_inform.html", "w") as filehandler:
filehandler.write(html)
templates = []
for name, value in sorted(globals().items()):
if isinstance(value, type) and issubclass(value, RegexTemplate):
try:
templates.append(value)
except:
pass
templates_to_html(templates)