MCPcopy Create free account
hub / github.com/RingBDStack/GDAP / ETText2Tri

Class ETText2Tri

data_convert/format/text2target.py:171–231  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

169
170# et + src -> ((tri)) 遍历事件类型, 针对每个给定的事件类型生成触发词(如果包含)
171class ETText2Tri(TargetFormat):
172
173 @staticmethod
174 def annotate_span(tokens, predicate_arguments, mark_tree=False, zh=False, isTest = False):
175 """
176 src: et + </s> + source text </s> 为 t5-base 分隔符
177 traget: ((Tri)) 只对tri进行生成, 如果同一个et存在多个Tri, 则生成 ((Tri)(Tri)...)
178 :param tokens:
179 US President George W. Bush told Canadian Prime Minister Jean Chretien by telephone Monday that he looked forward
180 to seeing him at the upcoming summit of major industrialized nations and Russia , the White House said Tuesday .
181 :param predicate_arguments:
182
183 :return:
184 """
185
186 token_separator = '' if zh else ' '
187
188 event_str_rep_list = list()
189
190 source_list = []
191 target_list = []
192
193 # 若出现单句多个相同 type 事件, 则将其合并
194 et_tri_dict = {} # {et + src: tri_list}
195 et_set = set() # 文本已经包含的事件类型
196
197 # 加载 schema文件中的信息, 目前每次都会读取文件, 很低效, 后续将这一步改为传参优化处理
198 et_list = EventSchema.read_from_file("data/raw_data/dyiepp_ace2005/event.schema").type_list
199
200 # 针对事件类型遍历制作训练样本
201 for et in et_list:
202 et_tri_dict[et + " </s> " + token_separator.join(tokens)] = set()
203
204
205 # 遍历event
206 for predicate_argument in predicate_arguments:
207 et = predicate_argument['type']
208 et_set.add(et)
209
210 tri_text = get_str_from_tokens(predicate_argument['tokens'], tokens, separator=token_separator) # 此处的 predicate_argument['tokens'] 为 [start, end](多个单词), 或者[start] (一个单词)
211
212 et_tri_dict[et + " </s> " + token_separator.join(tokens)].add(tri_text)
213
214
215 for src, tri_set in et_tri_dict.items():
216 if not tri_set: continue # 过滤掉不包含触发词的样本
217 source_list.append(src)
218 tmp_list = []
219 for tri_text in tri_set:
220 tmp_list.append(' '.join([type_start, tri_text, type_end]))
221 target_text = f'{type_start} ' + " ".join(tmp_list) + f' {type_end}'
222 target_list.append(target_text)
223
224 # negative sample on tri train data
225 if not isTest:
226 for tmp_et in random.sample(set(et_list) - et_set, 6):
227 source_list.append(tmp_et + " </s> " + token_separator.join(tokens))
228 target_list.append(f'{type_start} ' + " ".join([]) + f' {type_end}')

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected