File size: 8,284 Bytes
cdf8937
 
 
 
 
 
f2ae9f0
cdf8937
 
 
 
f2ae9f0
cdf8937
f2ae9f0
cdf8937
 
 
 
f2ae9f0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cdf8937
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f2ae9f0
 
 
 
cdf8937
 
 
 
 
 
 
 
f2ae9f0
 
 
 
 
 
 
cdf8937
 
 
 
 
 
 
 
 
 
 
2d4d710
65ce669
 
cdf8937
 
 
f2ae9f0
cdf8937
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3670dbb
cdf8937
de6996a
bfa4fe6
cdf8937
bfa4fe6
 
 
cdf8937
 
 
 
 
 
274baec
cdf8937
 
 
 
 
 
 
 
 
 
 
 
 
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
import streamlit as st
from annotated_text import annotated_text

import torch
from transformers import pipeline
from transformers import AutoModelForTokenClassification, AutoTokenizer
import spacy
import json

st.set_page_config(layout="wide")

model = AutoModelForTokenClassification.from_pretrained("./models/lusa_prepo", use_safetensors=True)

tokenizer = AutoTokenizer.from_pretrained("./models/lusa_prepo", model_max_length=512)
tagger = pipeline("ner", model=model, tokenizer=tokenizer, aggregation_strategy='first') #aggregation_strategy='max'
  


from spacy.matcher import PhraseMatcher
nlp = spacy.load("en_core_web_sm")


tokenization_contractions = {
    "no": ["n", "o"],
    "na": ["n", "a"],
    "nos": ["n", "os"],
    "nas": ["n", "as"],
    "ao": ["a", "o"],
#    "à": ["a", "a"],
    "aos": ["a", "os"],
 #   "às": ["a", "as"],
    "do": ["d", "o"],
    "da": ["d", "a"],
    "dos": ["d", "os"],
    "das": ["d", "as"],
    "pelo": ["pel", "o"],
    "pela": ["pel", "a"],
    "pelos": ["pel", "os"],
    "pelas": ["pel", "as"],
    "dum": ["d", "um"],
    "duma": ["d", "uma"],
    "duns": ["d", "uns"],
    "dumas": ["d", "umas"],
    "num": ["n", "um"],
    "numa": ["n", "uma"],
    "nuns": ["n", "uns"],
    "numas": ["n", "umas"],
    "dele": ["d", "ele"],
    "dela": ["d", "ela"],
    "deles": ["d", "eles"],
    "delas": ["d", "elas"],
    "deste": ["d", "este"],
    "desta": ["d", "esta"],
    "destes": ["d", "estes"],
    "destas": ["d", "estas"],
    "desse": ["d", "esse"],
    "dessa": ["d", "essa"],
    "desses": ["d", "esses"],
    "dessas": ["d", "essas"],
    "daquele": ["d", "aquele"],
    "daquela": ["d", "aquela"],
    "daqueles": ["d", "aqueles"],
    "daquelas": ["d", "aquelas"],
}


def tokenize_contractions(doc, tokenization_contractions):
    words = tokenization_contractions.keys() # Example: words to be split
    splits = tokenization_contractions
    matcher = PhraseMatcher(nlp.vocab)
    patterns = [nlp.make_doc(text) for text in words]
    matcher.add("Terminology", None, *patterns)
    matches = matcher(doc)

    with doc.retokenize() as retokenizer:
        for match_id, start, end in matches:
            heads = [(doc[start],1), doc[start]]
            attrs = {"POS": ["ADP", "DET"], "DEP": ["pobj", "compound"]}
            orths= splits[doc[start:end].text]           
            retokenizer.split(doc[start], orths=orths, heads=heads, attrs=attrs)
    return doc



def aggregate_subwords(input_tokens, labels):
    new_inputs = []
    new_labels = []
    current_word = ""
    current_label = ""
    for i, token in enumerate(input_tokens):     
        label = labels[i]
        # Handle subwords
        if token.startswith('##'):
            current_word += token[2:]
        else:
            # Finish previous word
            if current_word:
                new_inputs.append(current_word)
                new_labels.append(current_label)
            # Start new word
            current_word = token
            current_label = label
    new_inputs.append(current_word)
    new_labels.append(current_label)
    return new_inputs, new_labels

def annotateTriggers(line):
    line = line.strip()
    doc = nlp(line)
    doc = tokenize_contractions(doc, tokenization_contractions)
    tokens = [token.text for token in doc]
    inputs = tokenizer(tokens, is_split_into_words=True, return_tensors="pt")
    input_tokens = tokenizer.convert_ids_to_tokens(inputs['input_ids'][0])

    with torch.no_grad():
        logits = model(**inputs).logits

    predictions = torch.argmax(logits, dim=2)
    predicted_token_class = [model.config.id2label[t.item()] for t in predictions[0]]
    input_tokens, predicted_token_class =  aggregate_subwords(input_tokens,predicted_token_class)


    input_tokens = input_tokens[1:-1]
    predicted_token_class = predicted_token_class[1:-1]
    print(input_tokens)
    print(predicted_token_class)
    print(len(input_tokens), len(predicted_token_class))
    token_labels = []
    current_entity = ''
    for i, label in enumerate(predicted_token_class):
        token = input_tokens[i]
        if label == 'O':
            token_labels.append((token, 'O', ''))
            current_entity = ''
        elif label.startswith('B-'):
            current_entity = label[2:]
            token_labels.append((token, 'B', current_entity))
        elif label.startswith('I-'):
            if current_entity == '': 
                #raise ValueError(f"Invalid label sequence: {predicted_token_class}")
                continue
            token_labels[-1] = (token_labels[-1][0] + f" {token}", 'I', current_entity)
        else:
            raise ValueError(f"Invalid label: {label}")
    return token_labels





def joinEntities(entities):

    joined_entities = []
    i = 0
    while i < len(entities):
        curr_entity = entities[i]
        if curr_entity['entity'][0] == 'B':
            label = curr_entity['entity'][2:]
            j = i + 1
            while j < len(entities) and entities[j]['entity'][0] == 'I':
                j += 1
            joined_entity = {
                 'entity': label,
                'score': max(e['score'] for e in entities[i:j]),
                'index': min(e['index'] for e in entities[i:j]),
                'word': ' '.join(e['word'] for e in entities[i:j]),
                'start': entities[i]['start'],
                'end': entities[j-1]['end']
            }
            joined_entities.append(joined_entity)
            i = j - 1
        i += 1
    return joined_entities



import pysbd
seg = pysbd.Segmenter(language="es", clean=False)

def sent_tokenize(text):
    return seg.segment(text)

def getSentenceIndex(lines,span):
    i = 1
    sum = len(lines[0])
    while sum < span:
        sum += len(lines[i])
        i = i + 1
    return i - 1

def generateContext(text, window,span):
    lines = sent_tokenize(text)
    index = getSentenceIndex(lines,span)
    text = " ".join(lines[max(0,index-window):index+window +1])
    return text


def annotateEvents(text,squad,window):
    text = text.strip()
    ner_results = tagger(text)
    #print(ner_results)
    #ner_results = joinEntities(ner_results)
    i = 0
    #exit()
    while i < len(ner_results):
        ner_results[i]["entity"] = ner_results[i]["entity_group"].lstrip("B-")
        ner_results[i]["entity"] = ner_results[i]["entity_group"].lstrip("I-")
        i = i + 1

    events = [] 
    for trigger in ner_results:
        tipo = trigger["entity_group"]
        context = generateContext(text,window,trigger["start"])
        event = {
            "trigger":trigger["word"],
            "type": tipo,
            "score": trigger["score"],
            "context": context,
        }
        events.append(event)
    return events


#"A Joana foi atacada pelo João nas ruas do Porto, com uma faca."

st.title('Identify Events')

options = ["Naquele ano o rei morreu na batalha em Almograve. A rainha casou com o irmão dele.","O presidente da Federação Haitiana de Futebol, Yves Jean-Bart, foi banido para sempre de toda a atividade ligada ao futebol, por ter sido considerado culpado de abuso sexual sistemático de jogadoras, anunciou hoje a FIFA.",
           "O navio 'Figaro', no qual viajavam 30 tripulantes - 16 angolanos, cinco espanhóis, cinco senegaleses, três peruanos e um do Gana - acionou por telefone o alarme de incêndio a bordo.", "A Polícia Judiciária (PJ) está a investigar o aparecimento de ossadas que foram hoje avistadas pelo proprietário de um terreno na freguesia de Meadela, em Viana do Castelo, disse à Lusa fonte daquela força policial."]

option = st.selectbox(
        'Select examples',
        options)
#option = options [index]
line = st.text_area("Insert Text",option)

st.button('Run')



window = 1
if line != "":
    st.header("Triggers:")
    triggerss = annotateTriggers(line)   
    annotated_text(*[word[0]+" " if word[1] == 'O' else (word[0]+" ",word[2]) for word in triggerss ])

    eventos_1 = annotateEvents(line,1,window)
    eventos_2 = annotateEvents(line,2,window)

    for mention1, mention2 in zip(eventos_1,eventos_2):
        st.text(f"| Trigger: {mention1['trigger']:20} | Type: {mention1['type']:10} | Score: {str(round(mention1['score'],3)):5} |") 
        st.markdown("""---""")