Views
No views yet
O, B-SYMPTOM, I-SYMPTOM.is_split_into_words=True). For predictions that match our reported scores, reproduce that tokenization at inference (split into word/non-word tokens, run, then re-align offsets). A plain pipeline(raw_text) still gives correct character offsets, but its subword tokenization differs from training, so boundary predictions near punctuation may differ.ner_nlp4bia (handles pre-tokenization + offset re-alignment)1# pip install git+https://github.com/nlp4bia-bsc/ner-nlp4bia.git # not on PyPI; install from git
2from ner_nlp4bia.data.corpus import Document
3from ner_nlp4bia.inference.pipeline import PipelineInferencer
4
5inf = PipelineInferencer("BSC-NLP4BIA/multiclinner-it-symptom-CardioBERTa") # stride=128, aggregation="first"
6doc = inf.infer_document(Document(filename="d1", text=open("note.txt").read()))
7for a in doc.annotations:
8 print(a.label, a.start, a.end, repr(a.text)) # offsets in original-text coords1import re
2from transformers import pipeline
3
4TOK = re.compile(r'([0-9A-Za-zÀ-ÖØ-öø-ÿ]+|[^0-9A-Za-zÀ-ÖØ-öø-ÿ])')
5
6def pretokenize(text):
7 toks = [t for t in TOK.split(text) if t]
8 i = 1
9 while i < len(toks):
10 if not toks[i-1].isspace() and not toks[i].isspace():
11 toks.insert(i, ' '); i += 1
12 i += 1
13 pre = ''.join(toks)
14 oi = pj = 0; ins = [] # positions of inserted spaces in `pre`
15 while pj < len(pre):
16 if oi < len(text) and text[oi] == pre[pj]:
17 oi += 1; pj += 1
18 else:
19 ins.append(pj); pj += 1
20 return pre, ins
21
22nlp = pipeline("token-classification", model="BSC-NLP4BIA/multiclinner-it-symptom-CardioBERTa",
23 aggregation_strategy="first")
24# window long docs (RoBERTa/LtgBERT reserve 2 positions -> use mpe-2 for those)
25nlp.tokenizer.model_max_length = nlp.model.config.max_position_embeddings
26
27text = open("note.txt").read()
28pre, ins = pretokenize(text)
29for e in nlp(pre, stride=128):
30 if e["start"] == e["end"]:
31 continue
32 s = e["start"] - sum(1 for x in ins if x < e["start"])
33 t = e["end"] - sum(1 for x in ins if x < e["end"])
34 while s < t and text[s].isspace(): s += 1 # byte-BPE leading-space trim
35 while t > s and text[t-1].isspace(): t -= 1
36 print(e["entity_group"], s, t, repr(text[s:t]))stride=128 matches the training stride; prevents truncation of long documents.model_max_length = max_position_embeddings - 2.| Metric | Score | Definition |
|---|---|---|
| strict F1 | 0.6207 | exact match of both entity span (start/end) and label |
| char F1 (gold) | 0.7671 | character-level F1, restricted to the gold-standard documents |
1
2
3
4