freethenation's picture
Initial upload: CoNLL 2003 + CrossNER test sets with document context
4d3b2b2 verified
Raw History Blame Contribute Delete
3.79 kB
# BigBio KB schema (Hugging Face bigbio/*, bigbiohub.py kb_features), used for NER with document context.
# All offsets are character offsets into the document text, [start, end).
# Document text = all passage texts joined with one space, in order.
import json
from pathlib import Path
from pydantic import BaseModel, model_validator
class Passage(BaseModel):
id: str # unique within the document, e.g. "p0"
type: str # "labeled" = a benchmark sentence that is scored;
# "context" = unlabeled surrounding text, never scored
text: list[str] # the passage text; one string per piece (always one piece here)
offsets: list[tuple[int, int]] # [start, end) of each piece in the document text (always one pair here)
class Normalized(BaseModel): # link to a knowledge-base entry; required by BigBio, never used here
db_name: str # knowledge-base name, e.g. "UMLS"
db_id: str # entry id in that knowledge base
class Entity(BaseModel):
id: str # unique within the document, e.g. "e0"
type: str # entity label, one of the dataset's labels.json
text: list[str] # surface text of each piece; BigBio allows split ("discontinuous")
# entities, ours always have one piece
offsets: list[tuple[int, int]] # [start, end) of each piece in the document text; one pair here
normalized: list[Normalized] = [] # knowledge-base links; always empty here
class Document(BaseModel): # one line of test.jsonl
id: str # record id, e.g. "conll2003-test-0007"
document_id: str # source document id: CoNLL = DOCSTART index (RCV1 file in
# files.eng.testb); CrossNER = article index in *_domainlevel.txt
passages: list[Passage] # the whole document in order, labeled and context passages
entities: list[Entity] # gold entities; all inside "labeled" passages
events: list = [] # BigBio slot for events (trigger + participants); always empty here
coreferences: list = [] # BigBio slot for coreference groups; always empty here
relations: list = [] # BigBio slot for entity-to-entity relations; always empty here
@property
def text(self):
return " ".join(t for p in self.passages for t in p.text)
@model_validator(mode="after")
def check(self):
# passage offsets tile the joined document text; every entity lies inside a "labeled" passage
# and its text equals the document slice at its offsets
doc, pos = self.text, 0
for p in self.passages:
for t, (s, e) in zip(p.text, p.offsets, strict=True):
assert (s, e) == (pos, pos + len(t)), f"{self.id} passage {p.id}: offsets {s, e} != {pos, pos + len(t)}"
pos = e + 1
labeled = [o for p in self.passages if p.type == "labeled" for o in p.offsets]
for ent in self.entities:
for t, (s, e) in zip(ent.text, ent.offsets, strict=True):
assert doc[s:e] == t, f"{self.id} entity {ent.id}: {doc[s:e]!r} != {t!r}"
assert any(ls <= s and e <= le for ls, le in labeled), f"{self.id} entity {ent.id} outside labeled passages"
return self
def load(dataset_dir) -> tuple[list[Document], list[str]]:
"""Read test.jsonl (validated Documents) and labels.json."""
d = Path(dataset_dir)
docs = [Document.model_validate_json(line) for line in open(d / "test.jsonl", encoding="utf-8")]
return docs, json.load(open(d / "labels.json", encoding="utf-8"))