|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
LABELS = [ |
|
"O", |
|
"B-EVN", |
|
"B-GRO", |
|
"B-LOC", |
|
"B-MNT", |
|
"B-PRS", |
|
"B-SMP", |
|
"B-TME", |
|
"B-WRK", |
|
"I-EVN", |
|
"I-GRO", |
|
"I-LOC", |
|
"I-MNT", |
|
"I-PRS", |
|
"I-SMP", |
|
"I-TME", |
|
"I-WRK" |
|
] |
|
|
|
|
|
|
|
import datasets |
|
|
|
|
|
logger = datasets.logging.get_logger(__name__) |
|
|
|
|
|
_CITATION = """\ |
|
@misc{swe-nerc, |
|
title = {Swe-NERC}, |
|
author = {Ahrenberg, Lars ; Frid, Johan and Olsson, Leif-Jöran}, |
|
url = {https://hdl.handle.net/10794/121}, |
|
year = {2020} } |
|
""" |
|
|
|
_DESCRIPTION = """\ |
|
The corpus consists of ca. 150.000 words of text. |
|
""" |
|
|
|
_URL = "https://huggingface.co/datasets/vesteinn/swe-nerc/raw/main/" |
|
_TRAINING_FILE = "swe_nerc_v1.tsv" |
|
|
|
|
|
class SweNERCConfig(datasets.BuilderConfig): |
|
"""BuilderConfig for swe-nerc""" |
|
|
|
def __init__(self, **kwargs): |
|
"""BuilderConfig for swe-nerc. |
|
Args: |
|
**kwargs: keyword arguments forwarded to super. |
|
""" |
|
super(SweNERCConfig, self).__init__(**kwargs) |
|
|
|
|
|
class SweNERC(datasets.GeneratorBasedBuilder): |
|
"""sosialurin-faroese-ner dataset.""" |
|
|
|
BUILDER_CONFIGS = [ |
|
SweNERCConfig(name="swe-nerc", version=datasets.Version("1.0.0"), description="swedish ner corpus"), |
|
] |
|
|
|
def _info(self): |
|
return datasets.DatasetInfo( |
|
description=_DESCRIPTION, |
|
features=datasets.Features( |
|
{ |
|
"id": datasets.Value("string"), |
|
"tokens": datasets.Sequence(datasets.Value("string")), |
|
"ner_tags": datasets.Sequence( |
|
datasets.features.ClassLabel( |
|
names=LABELS |
|
) |
|
), |
|
} |
|
), |
|
supervised_keys=None, |
|
homepage="", |
|
citation=_CITATION, |
|
) |
|
|
|
def _split_generators(self, dl_manager): |
|
"""Returns SplitGenerators.""" |
|
urls_to_download = { |
|
"train": f"{_URL}{_TRAINING_FILE}", |
|
} |
|
downloaded_files = dl_manager.download_and_extract(urls_to_download) |
|
|
|
return [ |
|
datasets.SplitGenerator(name=datasets.Split.TRAIN, gen_kwargs={"filepath": downloaded_files["train"]}), |
|
] |
|
|
|
def _generate_examples(self, filepath): |
|
logger.info("⏳ Generating examples from = %s", filepath) |
|
with open(filepath, encoding="utf-8") as f: |
|
guid = 0 |
|
tokens = [] |
|
ner_tags = [] |
|
last_tag = None |
|
for line in f: |
|
if line.startswith("-DOCSTART-") or line == "" or line == "\n": |
|
if tokens: |
|
yield guid, { |
|
"id": str(guid), |
|
"tokens": tokens, |
|
"ner_tags": ner_tags, |
|
} |
|
guid += 1 |
|
tokens = [] |
|
ner_tags = [] |
|
last_tag = None |
|
else: |
|
|
|
splits = line.split("\t") |
|
tokens.append(splits[0]) |
|
try: |
|
tag = splits[1].rstrip() |
|
if tag == "O": |
|
pass |
|
elif tag == last_tag: |
|
tag = "I-" + tag |
|
else: |
|
tag = "B-" + tag |
|
ner_tags.append(tag) |
|
last_tag = splits[1].rstrip() |
|
except: |
|
print(splits) |
|
raise |
|
|
|
|
|
yield guid, { |
|
"id": str(guid), |
|
"tokens": tokens, |
|
"ner_tags": ner_tags, |
|
} |