|
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): |
|
|
|
return datasets.DatasetInfo( |
|
|
|
description=_DESCRIPTION, |
|
|
|
features=datasets.Features( |
|
{ |
|
"text": datasets.Value("string"), |
|
"classes": datasets.Sequence(datasets.Value("string")), |
|
"target": datasets.Value("int8") |
|
} |
|
), |
|
|
|
|
|
|
|
supervised_keys=None, |
|
) |
|
|
|
def _split_generators(self, dl_manager): |
|
"""Returns SplitGenerators.""" |
|
|
|
|
|
|
|
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.""" |
|
|
|
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] |
|
} |
|
|