1+ const parseAnn = ( function ( ) {
2+ class TextBoundMention {
3+ constructor ( id , label , charStart , charEnd ) {
4+ this . id = id ;
5+ this . label = label ;
6+ this . charStart = charStart ;
7+ this . charEnd = charEnd ;
8+ }
9+ }
10+
11+ class EventMention {
12+ constructor ( id , label , trigger , args ) {
13+ this . id = id ;
14+ this . label = label ;
15+ this . trigger = trigger ;
16+ this . arguments = args ;
17+ }
18+ }
19+
20+ class RelationMention {
21+ constructor ( id , label , arg1 , arg2 ) {
22+ this . id = id ;
23+ this . label = label ;
24+ this . arguments = [ arg1 , arg2 ] ;
25+ }
26+ }
27+
28+ class Attribute {
29+ constructor ( id , target , attribute , value ) {
30+ this . id = id ;
31+ this . target = target ;
32+ this . attribute = attribute ;
33+ this . value = value ;
34+ }
35+ }
36+
37+ const re = / : + (? = [ T E R ] \d + $ ) / ;
38+
39+ function parseTextBoundMention ( tokens , text ) {
40+ const id = + tokens [ 0 ] . slice ( 1 ) ,
41+ label = tokens [ 1 ] ,
42+ charStart = + tokens [ 2 ] ,
43+ charEnd = + tokens [ 3 ] ;
44+
45+ if ( id > 0 && charStart >= 0 && charStart < charEnd && charEnd < text . length ) {
46+ return new TextBoundMention ( 'T' + id , label , charStart , charEnd ) ;
47+ }
48+ }
49+
50+ function parseEventMention ( tokens , mentions ) {
51+ const id = + tokens [ 0 ] . slice ( 1 ) ,
52+ trigger = tokens [ 1 ] ,
53+ arguments = tokens . slice ( 2 ) ;
54+
55+ if ( id > 0 && trigger ) {
56+ let split = trigger . split ( re ) ;
57+ if ( split [ 0 ] . length > 0 && mentions [ split [ 1 ] ] ) {
58+
59+ const em = new EventMention ( 'E' + id , split [ 0 ] , split [ 1 ] , [ ] ) ;
60+
61+ arguments . forEach ( argument => {
62+ let splitArgument = argument . split ( re ) ;
63+ if ( splitArgument [ 0 ] . length > 0 && mentions [ splitArgument [ 1 ] ] ) {
64+ em . arguments . push ( {
65+ type : splitArgument [ 0 ] ,
66+ id : splitArgument [ 1 ]
67+ } ) ;
68+ }
69+ } ) ;
70+
71+ return em ;
72+ }
73+ }
74+ }
75+
76+ function parseRelationMention ( tokens , mentions ) {
77+ const id = + tokens [ 0 ] . slice ( 1 ) ,
78+ label = tokens [ 1 ] ,
79+ arg1 = tokens [ 2 ] ,
80+ arg2 = tokens [ 3 ] ;
81+
82+ if ( id > 0 && arg2 ) {
83+ const split1 = arg1 . split ( re ) ,
84+ split2 = arg2 . split ( re ) ;
85+
86+ if ( mentions [ split1 [ 1 ] ] && mentions [ split2 [ 1 ] ] ) {
87+ return new RelationMention ( 'R' + id , label , {
88+ type : split1 [ 0 ] ,
89+ id : split1 [ 1 ]
90+ } , {
91+ type : split2 [ 0 ] ,
92+ id : split2 [ 1 ]
93+ } ) ;
94+ }
95+ }
96+ }
97+
98+ function parseAttributes ( tokens , mentions ) {
99+ const id = + tokens [ 0 ] . slice ( 1 ) ,
100+ attr = tokens [ 1 ] ,
101+ target = tokens [ 2 ] ;
102+
103+ if ( id > 0 && mentions [ target ] ) {
104+ return new Attribute ( id , target , attr , tokens . slice ( 3 ) . join ( ' ' ) ) ;
105+ }
106+ }
107+
108+ function parse ( input ) {
109+
110+ var output = {
111+ texts : [ ] ,
112+ events : [ ] ,
113+ relations : [ ] ,
114+ attributes : [ ] ,
115+ unparsedLines : [ ] ,
116+ mentions : { }
117+ }
118+
119+ let lines = input . split ( '\n' ) ;
120+
121+ let text = lines [ 0 ] ;
122+ if ( ! text ) {
123+ output . unparsedLines = lines ;
124+ return output ;
125+ }
126+
127+ let unparsedLines = [ ] ;
128+ let mentions = { } ;
129+
130+ for ( let i = 1 ; i < lines . length ; ++ i ) {
131+ const line = lines [ i ] . trim ( ) ;
132+ if ( ! line ) { continue ; }
133+
134+ let tokens = line . split ( / \s + / ) ;
135+
136+ let parseIsSuccessful = false ;
137+
138+ /** The following IDs are currently supported:
139+
140+ T: text-bound annotation
141+ E: event
142+ R: relation
143+ A: attribute
144+
145+ Normalizations, notes, and equivalence relations are not currently supported
146+ */
147+
148+ switch ( tokens [ 0 ] . charAt ( 0 ) ) {
149+ case 'T' :
150+ let tbm = parseTextBoundMention ( tokens , text ) ;
151+ if ( tbm ) {
152+ output . texts . push ( tbm ) ;
153+ mentions [ tbm . id ] = tbm ;
154+ }
155+ break ;
156+ case 'E' :
157+ let em = parseEventMention ( tokens , mentions ) ;
158+ if ( em ) {
159+ output . events . push ( em ) ;
160+ mentions [ em . id ] = em ;
161+ }
162+ break ;
163+ case 'R' :
164+ let rm = parseRelationMention ( tokens , mentions ) ;
165+ if ( rm ) {
166+ output . relations . push ( rm ) ;
167+ mentions [ rm . id ] = rm ;
168+ }
169+ break ;
170+ case 'A' :
171+ let a = parseAttribute ( tokens , mentions ) ;
172+ if ( a ) {
173+ output . attributes . push ( a ) ;
174+ mentions [ a . id ] = a ;
175+ }
176+ break ;
177+ }
178+
179+ if ( ! parseIsSuccessful ) {
180+ unparsedLines . push ( line ) ;
181+ }
182+ }
183+
184+ output . mentions = mentions ;
185+ output . unparsedLines = unparsedLines ;
186+
187+ return output ;
188+ }
189+
190+ return parse ;
191+ } ) ( ) ;
0 commit comments