holylovenia
commited on
Commit
•
1a90928
1
Parent(s):
edf66af
Upload creole_rc.py with huggingface_hub
Browse files- creole_rc.py +207 -0
creole_rc.py
ADDED
@@ -0,0 +1,207 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# coding=utf-8
|
2 |
+
# Copyright 2022 The HuggingFace Datasets Authors and the current dataset script contributor.
|
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 |
+
import csv
|
17 |
+
import json
|
18 |
+
from pathlib import Path
|
19 |
+
from typing import Dict, List, Tuple
|
20 |
+
|
21 |
+
import datasets
|
22 |
+
|
23 |
+
from seacrowd.utils import schemas
|
24 |
+
from seacrowd.utils.configs import SEACrowdConfig
|
25 |
+
from seacrowd.utils.constants import Licenses, Tasks
|
26 |
+
|
27 |
+
_CITATION = """\
|
28 |
+
@misc{lent2023creoleval,
|
29 |
+
title={CreoleVal: Multilingual Multitask Benchmarks for Creoles},
|
30 |
+
author={Heather Lent and Kushal Tatariya and Raj Dabre and Yiyi Chen and Marcell Fekete and Esther Ploeger and Li Zhou and Hans Erik Heje and Diptesh Kanojia and Paul Belony and Marcel Bollmann and \
|
31 |
+
Loïc Grobol and Miryam de Lhoneux and Daniel Hershcovich and Michel DeGraff and Anders Søgaard and Johannes Bjerva},
|
32 |
+
year={2023},
|
33 |
+
eprint={2310.19567},
|
34 |
+
archivePrefix={arXiv},
|
35 |
+
primaryClass={cs.CL}
|
36 |
+
}
|
37 |
+
"""
|
38 |
+
|
39 |
+
_DATASETNAME = "creole_rc"
|
40 |
+
_DESCRIPTION = """\
|
41 |
+
CreoleRC is a subset created by the CreoleVal paper. Relation classification (RC) aims to identify semantic associations between entities within a text, essential for applications like knowledge base \
|
42 |
+
completion and question answering. The dataset is sourced from Wikipedia and manually annotated. CreoleRC contains 5 creoles, but SEACrowd is interested specifically in the Chavacano subset.
|
43 |
+
"""
|
44 |
+
_HOMEPAGE = "https://github.com/hclent/CreoleVal/tree/main/nlu/relation_classification"
|
45 |
+
_LANGUAGES = ["cbk"]
|
46 |
+
_LICENSE = Licenses.CC_BY_SA_4_0.value
|
47 |
+
_LOCAL = False
|
48 |
+
_URLS = {
|
49 |
+
"csv": "https://raw.githubusercontent.com/hclent/CreoleVal/main/nlu/relation_classification/data/relation_extraction/cbk-zam.csv",
|
50 |
+
"json": "https://raw.githubusercontent.com/hclent/CreoleVal/main/nlu/relation_classification/data/relation_extraction/cbk-zam.json",
|
51 |
+
}
|
52 |
+
_SUPPORTED_TASKS = [Tasks.RELATION_EXTRACTION]
|
53 |
+
|
54 |
+
_SOURCE_VERSION = "1.0.0"
|
55 |
+
_SEACROWD_VERSION = "2024.06.20"
|
56 |
+
|
57 |
+
|
58 |
+
class CreoleRC(datasets.GeneratorBasedBuilder):
|
59 |
+
"""Creole relation classification dataset, Chavacano subset, from https://github.com/hclent/CreoleVal/tree/main/nlu/relation_classification."""
|
60 |
+
|
61 |
+
SOURCE_VERSION = datasets.Version(_SOURCE_VERSION)
|
62 |
+
SEACROWD_VERSION = datasets.Version(_SEACROWD_VERSION)
|
63 |
+
|
64 |
+
# You will be able to load the "source" or "seacrowd" configurations with
|
65 |
+
# ds_source = datasets.load_dataset('my_dataset', name='source')
|
66 |
+
# ds_seacrowd = datasets.load_dataset('my_dataset', name='seacrowd')
|
67 |
+
|
68 |
+
# For local datasets you can make use of the `data_dir` and `data_files` kwargs
|
69 |
+
# https://huggingface.co/docs/datasets/add_dataset.html#downloading-data-files-and-organizing-splits
|
70 |
+
# ds_source = datasets.load_dataset('my_dataset', name='source', data_dir="/path/to/data/files")
|
71 |
+
# ds_seacrowd = datasets.load_dataset('my_dataset', name='seacrowd', data_dir="/path/to/data/files")
|
72 |
+
|
73 |
+
SEACROWD_SCHEMA_NAME = "kb"
|
74 |
+
|
75 |
+
BUILDER_CONFIGS = [
|
76 |
+
SEACrowdConfig(
|
77 |
+
name=f"{_DATASETNAME}_source",
|
78 |
+
version=SOURCE_VERSION,
|
79 |
+
description=f"{_DATASETNAME} source schema",
|
80 |
+
schema="source",
|
81 |
+
subset_id=_DATASETNAME,
|
82 |
+
),
|
83 |
+
SEACrowdConfig(
|
84 |
+
name=f"{_DATASETNAME}_seacrowd_{SEACROWD_SCHEMA_NAME}",
|
85 |
+
version=SEACROWD_VERSION,
|
86 |
+
description=f"{_DATASETNAME} SEACrowd schema",
|
87 |
+
schema=f"seacrowd_{SEACROWD_SCHEMA_NAME}",
|
88 |
+
subset_id=_DATASETNAME,
|
89 |
+
),
|
90 |
+
]
|
91 |
+
|
92 |
+
DEFAULT_CONFIG_NAME = f"{_DATASETNAME}_source"
|
93 |
+
|
94 |
+
def _info(self) -> datasets.DatasetInfo:
|
95 |
+
|
96 |
+
if self.config.schema == "source":
|
97 |
+
features = datasets.Features(
|
98 |
+
{
|
99 |
+
"id": datasets.Value("string"),
|
100 |
+
"sentence": datasets.Value("string"),
|
101 |
+
"ent1": datasets.Value("string"),
|
102 |
+
"ent2": datasets.Value("string"),
|
103 |
+
"ent1_qcode": datasets.Value("string"),
|
104 |
+
"ent2_qcode": datasets.Value("string"),
|
105 |
+
"property": datasets.Value("string"),
|
106 |
+
"property_desc": datasets.Value("string"),
|
107 |
+
"tokens": datasets.Sequence(datasets.Value("string")),
|
108 |
+
"edgeset_left": datasets.Sequence(datasets.Value("int32")),
|
109 |
+
"edgeset_right": datasets.Sequence(datasets.Value("int32")),
|
110 |
+
"edgeset_triple": datasets.Sequence(datasets.Value("string")),
|
111 |
+
}
|
112 |
+
)
|
113 |
+
|
114 |
+
elif self.config.schema == f"seacrowd_{self.SEACROWD_SCHEMA_NAME}":
|
115 |
+
features = schemas.kb_features
|
116 |
+
|
117 |
+
return datasets.DatasetInfo(
|
118 |
+
description=_DESCRIPTION,
|
119 |
+
features=features,
|
120 |
+
homepage=_HOMEPAGE,
|
121 |
+
license=_LICENSE,
|
122 |
+
citation=_CITATION,
|
123 |
+
)
|
124 |
+
|
125 |
+
def _split_generators(self, dl_manager: datasets.DownloadManager) -> List[datasets.SplitGenerator]:
|
126 |
+
"""Returns SplitGenerators."""
|
127 |
+
|
128 |
+
data_paths = {
|
129 |
+
"csv": Path(dl_manager.download_and_extract(_URLS["csv"])),
|
130 |
+
"json": Path(dl_manager.download_and_extract(_URLS["json"])),
|
131 |
+
}
|
132 |
+
|
133 |
+
return [
|
134 |
+
datasets.SplitGenerator(
|
135 |
+
name=datasets.Split.TRAIN,
|
136 |
+
gen_kwargs={
|
137 |
+
"csv_filepath": data_paths["csv"],
|
138 |
+
"json_filepath": data_paths["json"],
|
139 |
+
"split": "train",
|
140 |
+
},
|
141 |
+
)
|
142 |
+
]
|
143 |
+
|
144 |
+
# method parameters are unpacked from `gen_kwargs` as given in `_split_generators`
|
145 |
+
|
146 |
+
def _generate_examples(self, csv_filepath: Path, json_filepath: Path, split: str) -> Tuple[int, Dict]:
|
147 |
+
"""Yields examples as (key, example) tuples."""
|
148 |
+
# read csv file
|
149 |
+
with open(csv_filepath, "r", encoding="utf-8") as csv_file:
|
150 |
+
csv_reader = csv.reader(csv_file)
|
151 |
+
csv_data = [row for row in csv_reader]
|
152 |
+
csv_data = csv_data[1:] # remove header
|
153 |
+
|
154 |
+
# read json file
|
155 |
+
with open(json_filepath, "r", encoding="utf-8") as json_file:
|
156 |
+
json_data = json.load(json_file)
|
157 |
+
|
158 |
+
# properties descriptions from https://github.com/hclent/CreoleVal/tree/main/nlu/relation_classification
|
159 |
+
# for properties present in Chavacano subset
|
160 |
+
properties_desc = {"P17": "country", "P30": "continent", "P106": "occupation", "P131": "located in the administrative territorial entity", "P361": "part of", "P1376": " capital of country"}
|
161 |
+
|
162 |
+
num_sample = len(csv_data)
|
163 |
+
|
164 |
+
for i in range(num_sample):
|
165 |
+
if self.config.schema == "source":
|
166 |
+
example = {
|
167 |
+
"id": str(i),
|
168 |
+
"sentence": csv_data[i][0],
|
169 |
+
"ent1": csv_data[i][1],
|
170 |
+
"ent2": csv_data[i][2],
|
171 |
+
"ent1_qcode": csv_data[i][3],
|
172 |
+
"ent2_qcode": csv_data[i][4],
|
173 |
+
"property": csv_data[i][5],
|
174 |
+
"property_desc": properties_desc[csv_data[i][5]],
|
175 |
+
"tokens": json_data[i]["tokens"],
|
176 |
+
"edgeset_left": json_data[i]["edgeSet"]["left"],
|
177 |
+
"edgeset_right": json_data[i]["edgeSet"]["right"],
|
178 |
+
"edgeset_triple": json_data[i]["edgeSet"]["triple"],
|
179 |
+
}
|
180 |
+
elif self.config.schema == f"seacrowd_{self.SEACROWD_SCHEMA_NAME}":
|
181 |
+
offset_entity1 = csv_data[i][0].find(csv_data[i][1])
|
182 |
+
offset_entity2 = csv_data[i][0].find(csv_data[i][2])
|
183 |
+
|
184 |
+
if (offset_entity1 == -1) or (offset_entity2 == -1):
|
185 |
+
continue
|
186 |
+
|
187 |
+
example = {
|
188 |
+
"id": str(i),
|
189 |
+
"passages": [{"id": f"passage-{i}", "type": "text", "text": [csv_data[i][0]], "offsets": [[0, len(csv_data[i][0])]]}],
|
190 |
+
"entities": [
|
191 |
+
{"id": f"{i}-entity-{csv_data[i][3]}", "type": "text", "text": [csv_data[i][1]], "normalized": [{"db_name": csv_data[i][1], "db_id": csv_data[i][3]}], "offsets": [[offset_entity1, offset_entity1 + len(csv_data[i][1])]]},
|
192 |
+
{"id": f"{i}-entity-{csv_data[i][4]}", "type": "text", "text": [csv_data[i][2]], "normalized": [{"db_name": csv_data[i][2], "db_id": csv_data[i][4]}], "offsets": [[offset_entity2, offset_entity2 + len(csv_data[i][2])]]},
|
193 |
+
],
|
194 |
+
"events": [],
|
195 |
+
"coreferences": [],
|
196 |
+
"relations": [
|
197 |
+
{
|
198 |
+
"id": f"{i}-relation-{csv_data[i][5]}",
|
199 |
+
"type": properties_desc[csv_data[i][5]],
|
200 |
+
"arg1_id": f"{i}-entity-{csv_data[i][3]}",
|
201 |
+
"arg2_id": f"{i}-entity-{csv_data[i][4]}",
|
202 |
+
"normalized": [{"db_name": properties_desc[csv_data[i][5]], "db_id": csv_data[i][5]}],
|
203 |
+
}
|
204 |
+
],
|
205 |
+
}
|
206 |
+
|
207 |
+
yield i, example
|