anamargarida commited on
Commit
7538da0
·
verified ·
1 Parent(s): 2cbd71b

Rename app_25.py to app_26.py

Browse files
Files changed (1) hide show
  1. app_25.py → app_26.py +39 -2
app_25.py → app_26.py RENAMED
@@ -294,12 +294,49 @@ def extract_arguments(text, tokenizer, model, beam_search=True):
294
 
295
  return annotated_text
296
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
297
 
298
 
299
 
300
  # Apply the tags to the sentence tokens
301
- tagged_sentence1 = add_tags_offset(input_text, start_cause1, end_cause1, start_effect1, end_effect1, start_signal, end_signal)
302
- tagged_sentence2 = add_tags_offset(input_text, start_cause2, end_cause2, start_effect2, end_effect2, start_signal, end_signal)
303
  return tagged_sentence1, tagged_sentence2
304
 
305
 
 
294
 
295
  return annotated_text
296
 
297
+ def add_tags_offset_2(text, start_cause, end_cause, start_effect, end_effect, start_signal, end_signal):
298
+ """
299
+ Inserts tags into the original text based on token offsets.
300
+
301
+ Args:
302
+ text (str): The original input text.
303
+ offset_mapping (list of tuples): Maps token indices to character spans.
304
+ start_cause (int): Start token index of the cause span.
305
+ end_cause (int): End token index of the cause span.
306
+ start_effect (int): Start token index of the effect span.
307
+ end_effect (int): End token index of the effect span.
308
+ start_signal (int, optional): Start token index of the signal span.
309
+ end_signal (int, optional): End token index of the signal span.
310
+
311
+ Returns:
312
+ str: The modified text with annotated spans.
313
+ """
314
+
315
+ # Convert token indices to character indices
316
+ spans = [
317
+ (offset_mapping[start_cause][0], offset_mapping[end_cause][1], "<ARG0>", "</ARG0>"),
318
+ (offset_mapping[start_effect][0], offset_mapping[end_effect][1], "<ARG1>", "</ARG1>")
319
+ ]
320
+
321
+ # Include signal tags if available
322
+ if start_signal is not None and end_signal is not None:
323
+ spans.append((offset_mapping[start_signal][0], offset_mapping[end_signal][1], "<SIG0>", "</SIG0>"))
324
+
325
+ # Sort spans in reverse order based on start index (to avoid shifting issues)
326
+ spans.sort(reverse=True, key=lambda x: x[0])
327
+
328
+ # Insert tags
329
+ for start, end, open_tag, close_tag in spans:
330
+ text = text[:start] + open_tag + text[start:end] + close_tag + text[end:]
331
+
332
+ return text
333
+
334
 
335
 
336
 
337
  # Apply the tags to the sentence tokens
338
+ tagged_sentence1 = add_tags_offset_2(input_text, start_cause1, end_cause1, start_effect1, end_effect1, start_signal, end_signal)
339
+ tagged_sentence2 = add_tags_offset_2(input_text, start_cause2, end_cause2, start_effect2, end_effect2, start_signal, end_signal)
340
  return tagged_sentence1, tagged_sentence2
341
 
342