Create simclue.py
Browse files- simclue.py +124 -0
simclue.py
ADDED
|
@@ -0,0 +1,124 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# -*- coding: utf-8 -*-
|
| 2 |
+
"""
|
| 3 |
+
@author:XuMing([email protected])
|
| 4 |
+
@description:
|
| 5 |
+
"""
|
| 6 |
+
|
| 7 |
+
"""Natural Language Inference (NLI) Chinese Corpus.(nli_zh)"""
|
| 8 |
+
|
| 9 |
+
import os
|
| 10 |
+
import json
|
| 11 |
+
import datasets
|
| 12 |
+
|
| 13 |
+
_DESCRIPTION = """SimCLUE:3000000+中文语义理解与匹配数据集"""
|
| 14 |
+
|
| 15 |
+
GITHUB_HOME = "https://github.com/CLUEbenchmark/SimCLUE"
|
| 16 |
+
|
| 17 |
+
_CITATION = "https://github.com/CLUEbenchmark/SimCLUE"
|
| 18 |
+
|
| 19 |
+
_DATA_URL = "https://storage.googleapis.com/cluebenchmark/tasks/simclue_public.zip"
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
class SimCLUEConfig(datasets.BuilderConfig):
|
| 23 |
+
|
| 24 |
+
def __init__(self, features, data_url, citation, url, label_classes=(0, 1), **kwargs):
|
| 25 |
+
super().__init__(version=datasets.Version("1.0.0"), **kwargs)
|
| 26 |
+
self.features = features
|
| 27 |
+
self.label_classes = label_classes
|
| 28 |
+
self.data_url = data_url
|
| 29 |
+
self.citation = citation
|
| 30 |
+
self.url = url
|
| 31 |
+
|
| 32 |
+
|
| 33 |
+
class SimCLUE(datasets.GeneratorBasedBuilder):
|
| 34 |
+
"""The Natural Language Inference Chinese(NLI_zh) Corpus."""
|
| 35 |
+
|
| 36 |
+
part_file = {'train_rank': 'train_rank.json',
|
| 37 |
+
'train_pair': 'train_pair.json',
|
| 38 |
+
'corpus': 'corpus.txt',
|
| 39 |
+
'train_pair_postive': 'train_pair_postive.json',
|
| 40 |
+
'dev': 'dev.json',
|
| 41 |
+
'test_public': 'test_public.json'}
|
| 42 |
+
|
| 43 |
+
BUILDER_CONFIGS = [
|
| 44 |
+
SimCLUEConfig(
|
| 45 |
+
name="train_rank",
|
| 46 |
+
description=_DESCRIPTION,
|
| 47 |
+
features=datasets.Features({"query": datasets.Value("string"),
|
| 48 |
+
"title": datasets.Value("string"),
|
| 49 |
+
"neg_title": datasets.Value("string")}),
|
| 50 |
+
data_url=_DATA_URL,
|
| 51 |
+
citation=_CITATION,
|
| 52 |
+
url=GITHUB_HOME,
|
| 53 |
+
),
|
| 54 |
+
SimCLUEConfig(
|
| 55 |
+
name="train_pair",
|
| 56 |
+
description=_DESCRIPTION,
|
| 57 |
+
features=datasets.Features({"sentence1": datasets.Value("string"),
|
| 58 |
+
"sentence2": datasets.Value("string"),
|
| 59 |
+
"label": datasets.Value("int32")}),
|
| 60 |
+
data_url=_DATA_URL,
|
| 61 |
+
citation=_CITATION,
|
| 62 |
+
url=GITHUB_HOME,
|
| 63 |
+
),
|
| 64 |
+
SimCLUEConfig(
|
| 65 |
+
name="corpus",
|
| 66 |
+
description=_DESCRIPTION,
|
| 67 |
+
features=datasets.Features({"sentence1": datasets.Value("string")}),
|
| 68 |
+
data_url=_DATA_URL,
|
| 69 |
+
citation=_CITATION,
|
| 70 |
+
url=GITHUB_HOME,
|
| 71 |
+
),
|
| 72 |
+
SimCLUEConfig(
|
| 73 |
+
name="train_pair_postive",
|
| 74 |
+
description=_DESCRIPTION,
|
| 75 |
+
features=datasets.Features({"sentence1": datasets.Value("string"),
|
| 76 |
+
"sentence2": datasets.Value("string"),
|
| 77 |
+
"label": datasets.Value("int32")}),
|
| 78 |
+
data_url=_DATA_URL,
|
| 79 |
+
citation=_CITATION,
|
| 80 |
+
url=GITHUB_HOME,
|
| 81 |
+
),
|
| 82 |
+
SimCLUEConfig(
|
| 83 |
+
name="dev",
|
| 84 |
+
description=_DESCRIPTION,
|
| 85 |
+
features=datasets.Features({"sentence1": datasets.Value("string"),
|
| 86 |
+
"sentence2": datasets.Value("string"),
|
| 87 |
+
"label": datasets.Value("int32")}),
|
| 88 |
+
data_url=_DATA_URL,
|
| 89 |
+
citation=_CITATION,
|
| 90 |
+
url=GITHUB_HOME,
|
| 91 |
+
),
|
| 92 |
+
SimCLUEConfig(
|
| 93 |
+
name="test_public",
|
| 94 |
+
description=_DESCRIPTION,
|
| 95 |
+
features=datasets.Features({"sentence1": datasets.Value("string"),
|
| 96 |
+
"sentence2": datasets.Value("string"),
|
| 97 |
+
"label": datasets.Value("int32")}),
|
| 98 |
+
data_url=_DATA_URL,
|
| 99 |
+
citation=_CITATION,
|
| 100 |
+
url=GITHUB_HOME,
|
| 101 |
+
),
|
| 102 |
+
]
|
| 103 |
+
|
| 104 |
+
def _info(self):
|
| 105 |
+
return datasets.DatasetInfo(
|
| 106 |
+
description=self.config.description,
|
| 107 |
+
features=self.config.features,
|
| 108 |
+
homepage=self.config.url,
|
| 109 |
+
citation=self.config.citation,
|
| 110 |
+
)
|
| 111 |
+
|
| 112 |
+
def _split_generators(self, dl_manager):
|
| 113 |
+
dl_dir = dl_manager.download_and_extract(self.config.data_url)
|
| 114 |
+
return [datasets.SplitGenerator(
|
| 115 |
+
name=self.config.name,
|
| 116 |
+
gen_kwargs={
|
| 117 |
+
"filepath": os.path.join(dl_dir, self.part_file[self.config.name]),
|
| 118 |
+
})]
|
| 119 |
+
|
| 120 |
+
def _generate_examples(self, filepath):
|
| 121 |
+
"""This function returns the examples in the raw (text) form."""
|
| 122 |
+
with open(filepath, 'r', encoding="utf-8") as f:
|
| 123 |
+
for idx, row in enumerate(f):
|
| 124 |
+
yield idx, json.loads(row)
|