-
Notifications
You must be signed in to change notification settings - Fork 0
/
RoBERTa.py
60 lines (45 loc) · 1.67 KB
/
RoBERTa.py
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
from typing import Any, Dict, List
from tqdm import tqdm
from unml.models.model import Model
from unml.utils.text import TextUtils
class RoBERTa(Model):
"""
Class for RoBERTa (Liu et al., 2019) model
https://arxiv.org/abs/1907.11692
"""
MODEL_NAME = "Jean-Baptiste/roberta-large-ner-english"
def __init__(self, modelName: str = MODEL_NAME) -> None:
super().__init__(modelName=modelName, task="ner", aggregation_strategy="simple")
def recognize(self, text: str) -> List[Dict[str, Any]]:
"""
See doc for `NamedEntityRecognizer` class
"""
return self.recognizeFromChunked(text=text)
def recognizeFromChunked(self, text: str) -> List[Dict[str, Any]]:
"""
Recognize named entities from text, chunking the text into smaller
pieces to avoid hitting the max sequence length limit.
Parameters
----------
`text` : `str`
Text to recognize named entities from.
Returns
-------
`List[Dict[str, Any]]`
List of named entities recognized from the text.
"""
tokenizer = self.model.tokenizer
tokens = tokenizer.tokenize(text)
chunkedTokens = TextUtils.chunkTokens(
tokens=tokens,
tokenizer=tokenizer,
)
results = []
for chunk in tqdm(chunkedTokens):
chunkResults = self.model(chunk)
results.extend(chunkResults)
# Type casting for JSON serialization and cleaning
for result in results:
result["score"] = float(f'{result["score"]:.3f}')
result["word"] = result["word"].strip()
return results