naamapadam / naamapadam.py
AnanthZeke's picture
Update naamapadam.py
21fec25
raw
history blame
2.96 kB
import json
import os
import datasets
logger = datasets.logging.get_logger(__name__)
_CITATION = """\
"""
_DESCRIPTION = """\
"""
_HOMEPAGE = "https://indicnlp.ai4bharat.org/"
_LICENSE = "Creative Commons Attribution-NonCommercial 4.0 International Public License"
_URL = "https://huggingface.co/datasets/ai4bharat/naamapadam/resolve/main/data/{}_IndicNER_v{}.zip"
_LANGUAGES = ["as", "bn", "gu", "hi", "kn", "ml", "mr", "or", "pa", "ta", "te"]
class Naamapadam(datasets.GeneratorBasedBuilder):
VERSION = datasets.Version("1.0.0")
BUILDER_CONFIGS = [
datasets.BuilderConfig(
name="{}".format(lang), version=datasets.Version("1.0.0")
)
for lang in _LANGUAGES
]
def _info(self):
return datasets.DatasetInfo(
description=_DESCRIPTION,
features=datasets.Features(
{
"tokens": datasets.Sequence(datasets.Value("string")),
"tags": datasets.Sequence(
datasets.features.ClassLabel(
names=[
"O",
"B-PER",
"I-PER",
"B-ORG",
"I-ORG",
"B-LOC",
"I-LOC",
]
)
),
}
),
supervised_keys=None,
homepage=_HOMEPAGE,
citation=_CITATION,
license=_LICENSE,
version=self.VERSION,
)
def _split_generators(self, dl_manager):
"""Returns SplitGenerators."""
lang = str(self.config.name)
url = _URL.format(lang, self.VERSION.version_str[:-2])
data_dir = dl_manager.download_and_extract(url)
return [
datasets.SplitGenerator(
name=datasets.Split.TRAIN,
gen_kwargs={
"filepath": os.path.join(data_dir, lang + "_train.json"),
},
),
datasets.SplitGenerator(
name=datasets.Split.TEST,
gen_kwargs={
"filepath": os.path.join(data_dir, lang + "_test.json"),
},
),
datasets.SplitGenerator(
name=datasets.Split.VALIDATION,
gen_kwargs={
"filepath": os.path.join(data_dir, lang + "_val.json"),
},
),
]
def _generate_examples(self, filepath):
logger.info(f"generating examples from = {filepath}")
"""Yields examples as (key, example) tuples."""
with open(filepath, encoding="utf-8") as f:
for idx_, row in enumerate(f):
data = json.loads(row)
yield idx_, {"tokens": data["words"], "tags": data["ner"]}