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 5 |
+
@inproceedings{balasuriya-etal-2009-named,
|
| 6 |
+
title = "Named Entity Recognition in Wikipedia",
|
| 7 |
+
author = "Balasuriya, Dominic and
|
| 8 |
+
Ringland, Nicky and
|
| 9 |
+
Nothman, Joel and
|
| 10 |
+
Murphy, Tara and
|
| 11 |
+
Curran, James R.",
|
| 12 |
+
booktitle = "Proceedings of the 2009 Workshop on The People{'}s Web Meets {NLP}:
|
| 13 |
+
Collaboratively Constructed Semantic Resources (People{'}s Web)",
|
| 14 |
+
month = aug,
|
| 15 |
+
year = "2009",
|
| 16 |
+
address = "Suntec, Singapore",
|
| 17 |
+
publisher = "Association for Computational Linguistics",
|
| 18 |
+
url = "https://aclanthology.org/W09-3302",
|
| 19 |
+
pages = "10--18",
|
| 20 |
+
}
|
| 21 |
+
"""
|
| 22 |
+
|
| 23 |
+
_LICENCE = "CC-BY 4.0"
|
| 24 |
+
|
| 25 |
+
_DESCRIPTION = """
|
| 26 |
+
WikiGold dataset.
|
| 27 |
+
"""
|
| 28 |
+
|
| 29 |
+
_URL = (
|
| 30 |
+
"https://github.com/juand-r/entity-recognition-datasets/raw/master/"
|
| 31 |
+
"data/wikigold/CONLL-format/data/wikigold.conll.txt"
|
| 32 |
+
)
|
| 33 |
+
|
| 34 |
+
# the label ids
|
| 35 |
+
NER_TAGS_DICT = {
|
| 36 |
+
"O": 0,
|
| 37 |
+
"PER": 1,
|
| 38 |
+
"LOC": 2,
|
| 39 |
+
"ORG": 3,
|
| 40 |
+
"MISC": 4,
|
| 41 |
+
}
|
| 42 |
+
|
| 43 |
+
NER_BIO_TAGS_DICT = {
|
| 44 |
+
"O": 0,
|
| 45 |
+
"B-PER": 1,
|
| 46 |
+
"I-PER": 2,
|
| 47 |
+
"B-LOC": 3,
|
| 48 |
+
"I-LOC": 4,
|
| 49 |
+
"B-ORG": 5,
|
| 50 |
+
"I-ORG": 6,
|
| 51 |
+
"B-MISC": 7,
|
| 52 |
+
"I-MISC": 8
|
| 53 |
+
}
|
| 54 |
+
|
| 55 |
+
|
| 56 |
+
class WikiGoldConfig(datasets.BuilderConfig):
|
| 57 |
+
"""BuilderConfig for WikiGold"""
|
| 58 |
+
|
| 59 |
+
def __init__(self, **kwargs):
|
| 60 |
+
"""BuilderConfig for WikiGold.
|
| 61 |
+
Args:
|
| 62 |
+
**kwargs: keyword arguments forwarded to super.
|
| 63 |
+
"""
|
| 64 |
+
super(WikiGoldConfig, self).__init__(**kwargs)
|
| 65 |
+
|
| 66 |
+
|
| 67 |
+
class WikiGold(datasets.GeneratorBasedBuilder):
|
| 68 |
+
def _info(self):
|
| 69 |
+
return datasets.DatasetInfo(
|
| 70 |
+
description=_DESCRIPTION,
|
| 71 |
+
features=datasets.Features(
|
| 72 |
+
{
|
| 73 |
+
"id": datasets.Value("string"),
|
| 74 |
+
"tokens": datasets.features.Sequence(datasets.Value("string")),
|
| 75 |
+
"ner_tags": datasets.features.Sequence(
|
| 76 |
+
datasets.features.ClassLabel(
|
| 77 |
+
names=["O", "PER", "LOC", "ORG", "MISC"]
|
| 78 |
+
)
|
| 79 |
+
),
|
| 80 |
+
"ner_bio_tags": datasets.features.Sequence(
|
| 81 |
+
datasets.features.ClassLabel(
|
| 82 |
+
names=["O", "B-PER", "I-PER", "B-LOC", "I-LOC",
|
| 83 |
+
"B-ORG", "I-ORG", "B-MISC", "I-MISC"]
|
| 84 |
+
)
|
| 85 |
+
),
|
| 86 |
+
}
|
| 87 |
+
),
|
| 88 |
+
supervised_keys=None,
|
| 89 |
+
citation=_CITATION,
|
| 90 |
+
license=_LICENCE,
|
| 91 |
+
)
|
| 92 |
+
|
| 93 |
+
def _split_generators(self, dl_manager):
|
| 94 |
+
"""Returns SplitGenerators."""
|
| 95 |
+
urls_to_download = dl_manager.download_and_extract(_URL)
|
| 96 |
+
return [
|
| 97 |
+
datasets.SplitGenerator(
|
| 98 |
+
name=datasets.Split.TRAIN,
|
| 99 |
+
gen_kwargs={"filepath": urls_to_download},
|
| 100 |
+
),
|
| 101 |
+
]
|
| 102 |
+
|
| 103 |
+
def _generate_examples(self, filepath=None):
|
| 104 |
+
num_lines = sum(1 for _ in open(filepath))
|
| 105 |
+
id = 0
|
| 106 |
+
|
| 107 |
+
with open(filepath, "r") as f:
|
| 108 |
+
tokens, ner_tags, ner_bio_tags = [], [], []
|
| 109 |
+
for line in tqdm(f, total=num_lines):
|
| 110 |
+
line = line.strip().split()
|
| 111 |
+
|
| 112 |
+
if line:
|
| 113 |
+
assert len(line) == 2
|
| 114 |
+
token, ner_tag = line
|
| 115 |
+
|
| 116 |
+
if token == "-DOCSTART-":
|
| 117 |
+
continue
|
| 118 |
+
|
| 119 |
+
tokens.append(token)
|
| 120 |
+
ner_bio_tags.append(ner_tag)
|
| 121 |
+
if ner_tag != "O":
|
| 122 |
+
ner_tag = ner_tag.split("-")[1]
|
| 123 |
+
ner_tags.append(NER_TAGS_DICT[ner_tag])
|
| 124 |
+
|
| 125 |
+
elif tokens:
|
| 126 |
+
# organize a record to be written into json
|
| 127 |
+
record = {
|
| 128 |
+
"tokens": tokens,
|
| 129 |
+
"id": str(id),
|
| 130 |
+
"ner_tags": ner_tags,
|
| 131 |
+
"ner_bio_tags": ner_bio_tags,
|
| 132 |
+
}
|
| 133 |
+
tokens, ner_tags = [], []
|
| 134 |
+
id += 1
|
| 135 |
+
yield record["id"], record
|
| 136 |
+
|
| 137 |
+
# take the last sentence
|
| 138 |
+
if tokens:
|
| 139 |
+
record = {
|
| 140 |
+
"tokens": tokens,
|
| 141 |
+
"id": str(id),
|
| 142 |
+
"ner_tags": ner_tags,
|
| 143 |
+
"ner_bio_tags": ner_bio_tags,
|
| 144 |
+
}
|
| 145 |
+
yield record["id"], record
|