add reader
Browse files- named_timexes.py +148 -0
named_timexes.py
ADDED
@@ -0,0 +1,148 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# coding=utf-8
|
2 |
+
# Copyright 2022 Leon Derczynski
|
3 |
+
#
|
4 |
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
5 |
+
# you may not use this file except in compliance with the License.
|
6 |
+
# You may obtain a copy of the License at
|
7 |
+
#
|
8 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
9 |
+
#
|
10 |
+
# Unless required by applicable law or agreed to in writing, software
|
11 |
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
12 |
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
13 |
+
# See the License for the specific language governing permissions and
|
14 |
+
# limitations under the License.
|
15 |
+
|
16 |
+
# Lint as: python3
|
17 |
+
"""Named Temporal Expressions corpus (English)"""
|
18 |
+
|
19 |
+
import os
|
20 |
+
|
21 |
+
import datasets
|
22 |
+
|
23 |
+
|
24 |
+
logger = datasets.logging.get_logger(__name__)
|
25 |
+
|
26 |
+
|
27 |
+
_CITATION = """\
|
28 |
+
@inproceedings{brucato-etal-2013-recognising,
|
29 |
+
title = "Recognising and Interpreting Named Temporal Expressions",
|
30 |
+
author = "Brucato, Matteo and
|
31 |
+
Derczynski, Leon and
|
32 |
+
Llorens, Hector and
|
33 |
+
Bontcheva, Kalina and
|
34 |
+
Jensen, Christian S.",
|
35 |
+
booktitle = "Proceedings of the International Conference Recent Advances in Natural Language Processing {RANLP} 2013",
|
36 |
+
month = sep,
|
37 |
+
year = "2013",
|
38 |
+
address = "Hissar, Bulgaria",
|
39 |
+
publisher = "INCOMA Ltd. Shoumen, BULGARIA",
|
40 |
+
url = "https://aclanthology.org/R13-1015",
|
41 |
+
pages = "113--121",
|
42 |
+
}
|
43 |
+
"""
|
44 |
+
|
45 |
+
_DESCRIPTION = """\
|
46 |
+
This is a dataset annotated for _named temporal expression_ chunks.
|
47 |
+
|
48 |
+
The
|
49 |
+
commonest temporal expressions typically
|
50 |
+
contain date and time words, like April or
|
51 |
+
hours. Research into recognising and interpreting these typical expressions is mature in many languages. However, there is
|
52 |
+
a class of expressions that are less typical,
|
53 |
+
very varied, and difficult to automatically
|
54 |
+
interpret. These indicate dates and times,
|
55 |
+
but are harder to detect because they often do not contain time words and are not
|
56 |
+
used frequently enough to appear in conventional temporally-annotated corpora –
|
57 |
+
for example *Michaelmas* or *Vasant Panchami*.
|
58 |
+
|
59 |
+
For more details see [https://aclanthology.org/R13-1015.pdf](https://aclanthology.org/R13-1015.pdf)
|
60 |
+
"""
|
61 |
+
|
62 |
+
_URL = "http://www.derczynski.com/resources/named_timex.tar.bz2"
|
63 |
+
_TRAIN_FILE = "ntimex-train.conll"
|
64 |
+
_TEST_FILE = "ntimex-eval.conll"
|
65 |
+
|
66 |
+
class NamedTimexesConfig(datasets.BuilderConfig):
|
67 |
+
"""BuilderConfig for NamedTimexes"""
|
68 |
+
|
69 |
+
def __init__(self, **kwargs):
|
70 |
+
"""BuilderConfig for NamedTimexes.
|
71 |
+
|
72 |
+
Args:
|
73 |
+
**kwargs: keyword arguments forwarded to super.
|
74 |
+
"""
|
75 |
+
super(NamedTimexesConfig, self).__init__(**kwargs)
|
76 |
+
|
77 |
+
|
78 |
+
class NamedTimexes(datasets.GeneratorBasedBuilder):
|
79 |
+
"""NamedTimexes dataset."""
|
80 |
+
|
81 |
+
BUILDER_CONFIGS = [
|
82 |
+
NamedTimexesConfig(name="named-timexes", version=datasets.Version("1.0.0"), description="Named Temporal Expressions dataset"),
|
83 |
+
]
|
84 |
+
|
85 |
+
def _info(self):
|
86 |
+
return datasets.DatasetInfo(
|
87 |
+
description=_DESCRIPTION,
|
88 |
+
features=datasets.Features(
|
89 |
+
{
|
90 |
+
"id": datasets.Value("string"),
|
91 |
+
"tokens": datasets.Sequence(datasets.Value("string")),
|
92 |
+
"ntimex_tags": datasets.Sequence(
|
93 |
+
datasets.features.ClassLabel(
|
94 |
+
names=[
|
95 |
+
"O",
|
96 |
+
"T",
|
97 |
+
]
|
98 |
+
)
|
99 |
+
),
|
100 |
+
}
|
101 |
+
),
|
102 |
+
supervised_keys=None,
|
103 |
+
homepage="https://aclanthology.org/R13-1015.pdf",
|
104 |
+
citation=_CITATION,
|
105 |
+
)
|
106 |
+
|
107 |
+
def _split_generators(self, dl_manager):
|
108 |
+
"""Returns SplitGenerators."""
|
109 |
+
downloaded_file = dl_manager.download_and_extract(_URL)
|
110 |
+
|
111 |
+
data_files = {
|
112 |
+
"train": os.path.join(downloaded_file, _TRAIN_FILE),
|
113 |
+
"test": os.path.join(downloaded_file, _TEST_FILE),
|
114 |
+
}
|
115 |
+
|
116 |
+
return [
|
117 |
+
datasets.SplitGenerator(name=datasets.Split.TRAIN, gen_kwargs={"filepath": data_files["test"]}),
|
118 |
+
datasets.SplitGenerator(name=datasets.Split.TEST, gen_kwargs={"filepath": data_files["test"]}),
|
119 |
+
]
|
120 |
+
|
121 |
+
def _generate_examples(self, filepath):
|
122 |
+
guid = 0
|
123 |
+
with open(filepath, encoding="utf-8") as f:
|
124 |
+
logger.info("⏳ Generating examples from = %s", filepath)
|
125 |
+
tokens = []
|
126 |
+
ntimex_tags = []
|
127 |
+
for line in f:
|
128 |
+
if line.startswith("-DOCSTART-") or line.strip() == "" or line == "\n":
|
129 |
+
if tokens:
|
130 |
+
yield guid, {
|
131 |
+
"id": str(guid),
|
132 |
+
"tokens": tokens,
|
133 |
+
"ntimex_tags": ntimex_tags,
|
134 |
+
}
|
135 |
+
guid += 1
|
136 |
+
tokens = []
|
137 |
+
ntimex_tags = []
|
138 |
+
else:
|
139 |
+
# btc entries are tab separated
|
140 |
+
fields = line.split("\t")
|
141 |
+
tokens.append(fields[0])
|
142 |
+
ntimex_tags.append(fields[1].rstrip())
|
143 |
+
# last example
|
144 |
+
yield guid, {
|
145 |
+
"id": str(guid),
|
146 |
+
"tokens": tokens,
|
147 |
+
"ntimex_tags": ntimex_tags,
|
148 |
+
}
|