bratyacc.py
4.18 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
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
#!/usr/bin/env python
'''
Grammar for the brat stand-off format.
Example, test grammar on a collection:
find . -name '*.ann' | parallel cat | ./bratyacc.py
Author: Pontus Stenetorp <pontus stenetorp se>
Version: 2011-07-11
'''
try:
import ply.yacc as yacc
except ImportError:
# We need to add ply to path
from sys import path as sys_path
from os.path import join as path_join
from os.path import dirname
sys_path.append(path_join(dirname(__file__), '../lib/ply-3.4'))
import ply.yacc as yacc
from bratlex import tokens
# TODO: Recurse all the way to a file
# TODO: Comment annotation
def p_annotation_line(p):
'''
annotation_line : annotation NEWLINE
'''
p[0] = '%s\n' % (p[1], )
return p
# TODO: Ugly newline
def p_annotation(p):
'''
annotation : textbound
| event
| modifier
| equiv
| relation
| comment
'''
p[0] = p[1]
return p
# TODO: What do we really call these?
def p_equiv(p):
'''
equiv : equiv_core SPACE equiv_members
'''
p[0] = '%s %s' % (p[1], p[3], )
return p
def p_equiv_core(p):
'''
equiv_core : WILDCARD TAB TYPE
'''
p[0] = '*\t%s' % (p[3], )
return p
def p_equiv_members(p):
'''
equiv_members : equiv_member SPACE equiv_members
| equiv_member
'''
p[0] = '%s' % (p[1], )
try:
p[0] += ' %s' % (p[3], )
except IndexError:
# We did not have any more members
pass
return p
def p_equiv_member(p):
'''
equiv_member : id
'''
p[0] = '%s' % (p[1], )
return p
def p_textbound(p):
'''
textbound : textbound_freetext
| textbound_core
'''
p[0] = p[1]
return p
def p_textbound_core(p):
'''
textbound_core : TEXT_BOUND_ID TAB TYPE SPACE INTEGER SPACE INTEGER
'''
p[0] = '%s\t%s %d %d' % (p[1], p[3], p[5], p[7], )
return p
def p_textbound_freetext(p):
'''
textbound_freetext : textbound_core TAB FREETEXT
'''
p[0] = '%s\t%s' % (p[1], p[3], )
return p
def p_comment(p):
'''
comment : COMMENT_ID TAB TYPE SPACE id
'''
p[0] = '%s\t%s %s' % (p[1], p[3], p[5])
return p
def p_event(p):
'''
event : event_core SPACE event_arguments
| event_core SPACE
| event_core
'''
p[0] = p[1]
try:
p[0] += p[2]
except IndexError:
pass
try:
p[0] += p[3]
except IndexError:
pass
return p
def p_event_core(p):
'''
event_core : EVENT_ID TAB TYPE COLON id
'''
p[0] = '%s\t%s:%s' % (p[1], p[3], p[5], )
return p
def p_event_arguments(p):
'''
event_arguments : event_argument SPACE event_arguments
| event_argument
'''
p[0] = '%s' % (p[1], )
try:
p[0] += ' ' + p[3]
except IndexError:
pass
return p
def p_event_argument(p):
'''
event_argument : argument COLON id
'''
p[0] = '%s:%s' % (p[1], p[3], )
return p
def p_modifier(p):
'''
modifier : MODIFIER_ID TAB TYPE SPACE id
'''
p[0] = '%s\t%s %s' % (p[1], p[3], p[5], )
return p
def p_relation(p):
'''
relation : RELATION_ID TAB TYPE SPACE argument COLON id SPACE argument COLON id
'''
# TODO: Should probably require only one of each argument type
p[0] = '%s\t%s %s:%s %s:%s' % (p[1], p[3], p[5], p[7], p[9], p[11], )
return p
def p_argument(p):
'''
argument : TYPE
| TYPE INTEGER
'''
p[0] = p[1]
try:
p[0] += str(p[2])
except IndexError:
pass
return p
# Generic id
def p_id(p):
'''
id : TEXT_BOUND_ID
| EVENT_ID
| RELATION_ID
| MODIFIER_ID
| COMMENT_ID
'''
p[0] = p[1]
return p
def p_error(p):
print 'Syntax error in input! "%s"' % (str(p), )
raise Exception
parser = yacc.yacc()
if __name__ == '__main__':
from sys import stdin
for line in stdin:
print 'Input: "%s"' % line.rstrip('\n')
result = parser.parse(line)
assert result == line, ('"%s" != "%s"' % (result, line)
).replace('\n', '\\n')
print result,