File size: 5,586 Bytes
0e6b96c 76951c8 aa08036 0e6b96c 76951c8 aa08036 0e6b96c 933b31e 0e6b96c 76951c8 aa08036 0e6b96c 933b31e 0e6b96c |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 |
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]
}
|