-
Notifications
You must be signed in to change notification settings - Fork 17
/
test_amrsemparser.py
228 lines (197 loc) · 8.59 KB
/
test_amrsemparser.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
import os
import re
from amr_parser import get_verbnet_preds_from_obslist
def protected_tokenizer(sentence_string, simple=False):
if simple:
# simplest possible tokenizer
# split by these symbols
sep_re = re.compile(r'[\.,;:?!"\' \(\)\[\]\{\}]')
return simple_tokenizer(sentence_string, sep_re)
else:
# imitates JAMR (97% sentece acc on AMR2.0)
# split by these symbols
# TODO: Do we really need to split by - ?
sep_re = re.compile(r'[/~\*%\.,;:?!"\' \(\)\[\]\{\}-]')
return jamr_like_tokenizer(sentence_string, sep_re)
def simple_tokenizer(sentence_string, separator_re):
tokens = []
positions = []
start = 0
for point in separator_re.finditer(sentence_string):
end = point.start()
token = sentence_string[start:end]
separator = sentence_string[end:point.end()]
# Add token if not empty
if token.strip():
tokens.append(token)
positions.append((start, end))
# Add separator
if separator.strip():
tokens.append(separator)
positions.append((end, point.end()))
# move cursor
start = point.end()
# Termination
end = len(sentence_string)
if start < end:
token = sentence_string[start:end]
if token.strip():
tokens.append(token)
positions.append((start, end))
return tokens, positions
def jamr_like_tokenizer(sentence_string, sep_re):
# quote normalization
sentence_string = sentence_string.replace('``', '"')
sentence_string = sentence_string.replace("''", '"')
sentence_string = sentence_string.replace("“", '"')
# currency normalization
#sentence_string = sentence_string.replace("£", 'GBP')
# Do not split these strings
protected_re = re.compile("|".join([
# URLs (this conflicts with many other cases, we should normalize URLs
# a priri both on text and AMR)
# r'((http|https)\:\/\/)?[a-zA-Z0-9\.\/\?\:@\-_=#]+\.([a-zA-Z]){2,6}([a-zA-Z0-9\.\&\/\?\:@\-_=#])*',
#
r'[0-9][0-9,\.:/-]+[0-9]', # quantities, time, dates
r'^[0-9][\.](?!\w)', # enumerate
r'\b[A-Za-z][\.](?!\w)', # itemize
r'\b([A-Z]\.)+[A-Z]?', # acronym with periods (e.g. U.S.)
r'!+|\?+|-+|\.+', # emphatic
r'etc\.|i\.e\.|e\.g\.|v\.s\.|p\.s\.|ex\.', # latin abbreviations
r'\b[Nn]o\.|\bUS\$|\b[Mm]r\.', # ...
r'\b[Mm]s\.|\bSt\.|\bsr\.|a\.m\.', # other abbreviations
r':\)|:\(', # basic emoticons
# contractions
r'[A-Za-z]+\'[A-Za-z]{3,}', # quotes inside words
r'n\'t(?!\w)', # negative contraction (needed?)
r'\'m(?!\w)', # other contractions
r'\'ve(?!\w)', # other contractions
r'\'ll(?!\w)', # other contractions
r'\'d(?!\w)', # other contractions
# r'\'t(?!\w)' # other contractions
r'\'re(?!\w)', # other contractions
r'\'s(?!\w)', # saxon genitive
#
r'<<|>>', # weird symbols
#
r'Al-[a-zA-z]+|al-[a-zA-z]+', # Arabic article
# months
r'Jan\.|Feb\.|Mar\.|Apr\.|Jun\.|Jul\.|Aug\.|Sep\.|Oct\.|Nov\.|Dec\.'
]))
# iterate over protected sequences, tokenize unprotected and append
# protected strings
tokens = []
positions = []
start = 0
for point in protected_re.finditer(sentence_string):
# extract preceeding and protected strings
end = point.start()
preceeding_str = sentence_string[start:end]
protected_str = sentence_string[end:point.end()]
if preceeding_str:
# tokenize preceeding string keep protected string as is
for token, (start2, end2) in zip(
*simple_tokenizer(preceeding_str, sep_re)
):
tokens.append(token)
positions.append((start + start2, start + end2))
tokens.append(protected_str)
positions.append((end, point.end()))
# move cursor
start = point.end()
# Termination
end = len(sentence_string)
if start < end:
ending_str = sentence_string[start:end]
if ending_str.strip():
for token, (start2, end2) in zip(
*simple_tokenizer(ending_str, sep_re)
):
tokens.append(token)
positions.append((start + start2, start + end2))
return tokens, positions
amr_server_ip = os.getenv('LOA_AMR_SERVER_IP', 'localhost')
amr_server_port = int(os.getenv('LOA_AMR_SERVER_PORT', '0'))
obs = """
-= Laundry Room =-
You find yourself in a laundry room. An usual one.
Okay, just remember what you're here to do,
and everything will go great.
You make out a washing machine. Empty!
What kind of nightmare TextWorld is this?
You can make out an opened laundry basket. Empty!
What kind of nightmare TextWorld is this?
You make out a clothes drier. The clothes drier is empty!
This is the worst thing that could possibly happen, ever!
You scan the room, seeing a suspended shelf.
Unfortunately, there isn't a thing on it. You see a work table.
On the work table you can see a pair of dirty gray underpants.
You make out a bench.
Looks like someone's already been here and taken everything off it,
though. Aw, here you were,
all excited for there to be things on it!'
"""
# tokens, positions = protected_tokenizer(obs)
#
# print(tokens)
# print(positions)
#
# exit()
all_preds, pred_count_dict, verbnet_facts_logs = \
get_verbnet_preds_from_obslist(
# obslist=['You pick up the wet hoodie from the ground. ' +
# 'You pick up an red apple from the black big table.'],
# obslist=['You pick up the wet hoodie from the ground and ' +
# 'you make out coat hanger.'],
obslist=['you are carrying peanut oil, flour, and sugar.'],
# obslist=['I see the cake.'],
# obslist=['I saw her bake the cake.'],
# obslist=[
# '-= Backyard =- ' +
# 'I just think it\'s great you\'ve just entered a backyard. ' +
# 'I guess you better just go and list everything you see here. ' +
# 'You can make out a BBQ. The BBQ is recent. ' +
# 'But the thing hasn\'t got anything on it. ' +
# 'What you think everything in TextWorld should have stuff? ' +
# 'You make out a clothesline. The clothesline is typical. ' +
# 'But the thing is empty, unfortunately. ' +
# 'Aw, here you were, all excited for there to be things on it! ' +
# 'As if things weren\'t amazing enough already, ' +
# 'you can even see a patio chair. The patio chair is stylish. ' +
# 'However, the patio chair, like an empty patio chair, ' +
# 'has nothing on it. You make out a patio table. ' +
# 'The patio table is stylish. But there isn\'t a thing on it. ' +
# 'You bend down to tie your shoe. When you stand up, ' +
# 'you notice a workbench. ' +
# 'On the workbench you see a wet cardigan. ' +
# 'There is an open screen door leading west.'
# ],
# obslist=[
# """
# -= Laundry Room =-
# You find yourself in a laundry room. An usual one.
# Okay, just remember what you're here to do,
# and everything will go great.
# You make out a washing machine. Empty!
# What kind of nightmare TextWorld is this?
# You can make out an opened laundry basket. Empty!
# What kind of nightmare TextWorld is this?
# You make out a clothes drier. The clothes drier is empty!
# This is the worst thing that could possibly happen, ever!
# You scan the room, seeing a suspended shelf.
# Unfortunately, there isn't a thing on it. You see a work table.
# On the work table you can see a pair of dirty gray underpants.
# You make out a bench.
# Looks like someone's already been here and taken everything off it,
# though. Aw, here you were,
# all excited for there to be things on it!'
# """
# ],
amr_server_ip=amr_server_ip,
amr_server_port=amr_server_port,
mincount=0, verbose=True,
sem_parser_mode='propbank',
)
print(all_preds)
print(pred_count_dict)
print(verbnet_facts_logs)