Views
No views yet
| Language | Number of text samples | Number of tokens |
|---|---|---|
| Portuguese | 2,974,058 | 49,720,263 |
1from transformers import pipeline, AutoModelForTokenClassification, AutoTokenizer
2import torch
3
4def get_result_text_es_pt (list_entity, text, lang):
5 result_words = []
6 tmp_word = ""
7 if lang == "es":
8 punc_tags = ['¿', '?', '¡', '!', ',', '.', ':']
9 else:
10 punc_tags = ['?', '!', ',', '.', ':']
11
12 for idx, entity in enumerate(list_entity):
13 tag = entity["entity"]
14 word = entity["word"]
15 start = entity["start"]
16 end = entity["end"]
17
18 # check punctuation
19 punc_in = next((p for p in punc_tags if p in tag), "")
20
21 subword = False
22 # check subwords
23 if word[0] == "#":
24 subword = True
25 if tmp_word == "":
26 p_s = list_entity[idx-1]["start"]
27 p_e = list_entity[idx-1]["end"]
28 tmp_word = text[p_s:p_e] + text[start:end]
29 else:
30 tmp_word = tmp_word + text[start:end]
31 word = tmp_word
32 else:
33 tmp_word = ""
34 word = text[start:end]
35
36 if tag == "l":
37 word = word
38 elif tag == "u":
39 word = word.capitalize()
40 # case with punctuation
41 else:
42 if tag[-1] == "l":
43 word = (punc_in + word) if punc_in in ["¿", "¡"] else (word + punc_in)
44 elif tag[-1] == "u":
45 word = (punc_in + word.capitalize()) if punc_in in ["¿", "¡"] else (word.capitalize() + punc_in)
46
47 if subword == True:
48 result_words[-1] = word
49 else:
50 result_words.append(word)
51
52 return " ".join(result_words)
53
54lang = "pt"
55model_path = "VOCALINLP/portuguese_capitalization_punctuation_restoration_sanivert"
56
57model = AutoModelForTokenClassification.from_pretrained(model_path)
58tokenizer = AutoTokenizer.from_pretrained(model_path)
59
60pipe = pipeline("token-classification", model=model, tokenizer=tokenizer)
61text = "é preciso fazer análises ao sangue à urina e aos ouvidos"
62result = pipe(text)
63
64print("Source text: "+ text)
65result_text = get_result_text_es_pt(result, text, lang)
66print("Restored text: " +result_text)Created by VOCALI SISSTEMAS INTELIGENTES S.L.