feed / feed.py
futin's picture
Update feed.py
aa08036
from curses.ascii import isalpha
import os
import csv
import re
from typing import Sequence
import json
import ast
import datasets
_DESCRIPTION = """\
Example dataset toxic
"""
_DATA_URL = "https://drive.google.com/uc?id=1Ldnn3YYt_ErYq4ZGSon1MvcP3uJO0_PX"
_DATA_ENG = "https://drive.google.com/uc?id=1p-iyKTRhUXaDmqsx69Zvb4ivjaCmVVr8"
_TEXT = {
"sen_vi": [" thất vọng", " bình thường", " hài lòng"],
"sen_en": [" negative", " neutral", " positive"],
"top_vi": [" giảng viên", " môn học", " phòng học", " tổng thể"],
"top_en": [" lecturer", " curriculum", " facility", " general"],
"top_en_": ["lecturer", "curriculum", "facility", "general"],
"sen_en_": ["negative", "neutral", "positive"],
"sen_vi_": ["thất vọng", "bình thường", "hài lòng"],
"top_vi_": ["giảng viên", "môn học", "phòng học", "tổng thể"],
}
class Config(datasets.BuilderConfig):
"""BuilderConfig for GLUE."""
def __init__(self, data_url, **kwargs):
"""BuilderConfig
Args:
data_url: `string`, url to the dataset (word or raw level)
**kwargs: keyword arguments forwarded to super.
"""
super(Config, self).__init__(
version=datasets.Version(
"1.0.0",
),
**kwargs,
)
self.data_url = data_url
class Guess(datasets.GeneratorBasedBuilder):
VERSION = datasets.Version("0.1.0")
BUILDER_CONFIGS = [
Config(
name="top_vi",
data_url=_DATA_URL,
description="data",
),
Config(
name="top_en",
data_url=_DATA_ENG,
description="data",
),
Config(
name="sen_vi",
data_url=_DATA_URL,
description="data",
),
Config(
name="sen_en",
data_url=_DATA_ENG,
description="data",
),
Config(
name="sen_en_",
data_url=_DATA_ENG,
description="data",
),
Config(
name="top_en_",
data_url=_DATA_ENG,
description="data",
),
Config(
name="top_vi_",
data_url=_DATA_URL,
description="data",
),
Config(
name="sen_vi_",
data_url=_DATA_URL,
description="data",
),
]
def _info(self):
# TODO(wikitext): Specifies the datasets.DatasetInfo object
return datasets.DatasetInfo(
# This is the description that will appear on the datasets page.
description=_DESCRIPTION,
# datasets.features.FeatureConnectors
features=datasets.Features(
{
"text": datasets.Value("string"),
"classes": datasets.Sequence(datasets.Value("string")),
"target": datasets.Value("int8")
}
),
# If there's a common (input, target) tuple from the features,
# specify them here. They'll be used if as_supervised=True in
# builder.as_dataset.
supervised_keys=None,
)
def _split_generators(self, dl_manager):
"""Returns SplitGenerators."""
# TODO(wikitext): Downloads the data and defines the splits
# dl_manager is a datasets.download.DownloadManager that can be used to
# download and extract URLs
data_file = dl_manager.download(self.config.data_url)
return [
datasets.SplitGenerator(
name=datasets.Split.TEST,
gen_kwargs={"data_file": data_file, "type": self.config.name},
),
]
def _generate_examples(self, data_file, type):
"""Yields examples."""
# TODO(wikitext): Yields (key, example) tuples from the dataset
with open(data_file, 'r') as f:
lines = list(f)
if type[:3] == 'sen':
_CLASS = {
"negative": 0,
"neutral": 1,
"positive": 2,
}
else:
_CLASS = {
"lecturer": 0,
"curriculum": 1,
"facility": 2,
"others": 3
}
TEXT_ = _TEXT[type]
for idx, line in enumerate(lines):
json_object = ast.literal_eval(line)
if type[:3] == 'top':
label = json_object['topic']
else:
label = json_object['sentiment']
if label not in _CLASS:
continue
_text = json_object['text']
_classes = []
_PROMPT = {
"sen_vi": f'{_text} Cảm thấy ',
"sen_en": f'{_text} The sentiment of this sentence is ',
"top_vi": f'Nói về ',
"top_en": f'Comment about ',
"sen_en_": f'{_text} The sentiment of this sentence is ',
"top_en_": f'Comment about ',
"sen_vi_": f'{_text} Cảm thấy ',
"top_vi_": f'Nói về ',
}
for _cl in TEXT_:
if type[:3] == 'sen':
_classes.append(_cl)
else:
_classes.append(f'{_cl}. {_text}')
yield idx, {
"text" : _PROMPT[type],
"classes" : _classes,
"target" : _CLASS[label]
}