amz20 / amz20.py
kuroneko5943's picture
从txt下载数据,支持dataview
96ec482
raw
history blame contribute delete
No virus
4.37 kB
from datasets import Value, ClassLabel,Sequence
import datasets
_AMZ20_CITATION = """\
"""
_AMZ20_DESCRIPTION = """\
GLUE, the General Language Understanding Evaluation benchmark
(https://gluebenchmark.com/) is a collection of resources for training,
evaluating, and analyzing natural language understanding systems.
"""
class AMZ20Config(datasets.BuilderConfig):
def __init__(
self,
text_features,
label_column,
data_url,
data_dir,
citation,
url,
label_classes=None,
process_label=lambda x: x,
**kwargs,
):
super(AMZ20Config, self).__init__(version=datasets.Version("1.0.0", ""), **kwargs)
self.text_features = text_features
self.label_column = label_column
self.label_classes = label_classes
self.data_url = data_url
self.data_dir = data_dir
self.citation = citation
self.url = url
self.process_label = process_label
class AMZ20(datasets.GeneratorBasedBuilder):
domain_list = ['Sandal', 'Magazine_Subscriptions', 'RiceCooker', 'Flashlight', 'Jewelry', 'CableModem', 'GraphicsCard',
'GPS', 'Projector', 'Keyboard', 'Video_Games', 'AlarmClock', 'HomeTheaterSystem', 'Vacuum', 'Gloves',
'Baby', 'Bag', 'Movies_TV', 'Dumbbell', 'Headphone']
BUILDER_CONFIGS = [
AMZ20Config(name=domain_name,
description= f'comments of JD {domain_name}.',
text_features={'sentence':'sentence', 'domain':'domain'},
label_classes=['POS','NEG', 'NEU'],
label_column='label',
citation="",
data_dir= "",
data_url = "https://huggingface.co/datasets/kuroneko3578/amz20/resolve/main/",
url='https://github.com/ws719547997/LNB-DA')
for domain_name in domain_list
]
def _info(self):
features = {'id':Value(dtype='int32', id=None),
'domain':Value(dtype='string', id=None),
'label':ClassLabel(num_classes=3, names=['POS', 'NEG', 'NEU'], names_file=None, id=None),
'rank':Value(dtype='int32', id=None),
'sentence':Value(dtype='string', id=None)}
return datasets.DatasetInfo(
description=_AMZ20_DESCRIPTION,
features=datasets.Features(features),
homepage=self.config.url,
citation=self.config.citation + "\n" + _AMZ20_CITATION,
)
def _split_generators(self, dl_manager):
test_file = rf'{self.config.data_url}test/{self.config.name}.txt'
dev_file = rf'{self.config.data_url}dev/{self.config.name}.txt'
train_file = rf'{self.config.data_url}train/{self.config.name}.txt'
return [datasets.SplitGenerator(name=datasets.Split.TEST,
gen_kwargs={
"data_file": dl_manager.download(test_file),
"split": "test",
},),
datasets.SplitGenerator(name=datasets.Split.VALIDATION,
gen_kwargs={
"data_file": dl_manager.download(dev_file),
"split": "dev",
},),
datasets.SplitGenerator(name=datasets.Split.TRAIN,
gen_kwargs={
"data_file": dl_manager.download(train_file),
"split": "train",
},)]
def _generate_examples(self, data_file, split):
with open(data_file, 'r', encoding='utf-8') as f:
for line in f:
lin = line.strip()
if not lin:
continue
lin_sp = lin.split('\t')
if len(lin_sp) < 5:
continue
# 列表头也被随机打散到数据集里面了
if lin_sp[0] == 'index':
continue
# id, {example}
yield lin_sp[0], {'sentence':lin_sp[4],'domain':lin_sp[1], 'label':lin_sp[2], 'id':lin_sp[0], 'rank':lin_sp[3]}