Update labels.py
Browse files
labels.py
CHANGED
|
@@ -15,76 +15,51 @@
|
|
| 15 |
"""Yahoo! Answers Topic Classification Dataset"""
|
| 16 |
|
| 17 |
|
| 18 |
-
|
| 19 |
-
|
| 20 |
|
| 21 |
-
import datasets
|
| 22 |
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
Yahoo! Answers Topic Classification is text classification dataset. \
|
| 26 |
-
The dataset is the Yahoo! Answers corpus as of 10/25/2007. \
|
| 27 |
-
The Yahoo! Answers topic classification dataset is constructed using 10 largest main categories. \
|
| 28 |
-
From all the answers and other meta-information, this dataset only used the best answer content and the main category information.
|
| 29 |
-
"""
|
| 30 |
-
|
| 31 |
-
_URL = "https://drive.google.com/uc?export=download&id=0Bz8a_Dbh9Qhbd2JNdDBsQUdocVU"
|
| 32 |
-
|
| 33 |
-
_TOPICS = [
|
| 34 |
-
"Society & Culture",
|
| 35 |
-
"Science & Mathematics",
|
| 36 |
-
"Health",
|
| 37 |
-
"Education & Reference",
|
| 38 |
-
"Computers & Internet",
|
| 39 |
-
"Sports",
|
| 40 |
-
"Business & Finance",
|
| 41 |
-
"Entertainment & Music",
|
| 42 |
-
"Family & Relationships",
|
| 43 |
-
"Politics & Government",
|
| 44 |
-
]
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
class YahooAnswersTopics(datasets.GeneratorBasedBuilder):
|
| 48 |
-
"Yahoo! Answers Topic Classification Dataset"
|
| 49 |
-
|
| 50 |
-
VERSION = datasets.Version("1.0.0")
|
| 51 |
-
BUILDER_CONFIGS = [
|
| 52 |
-
datasets.BuilderConfig(
|
| 53 |
-
name="yahoo_answers_topics",
|
| 54 |
-
version=datasets.Version("1.0.0", ""),
|
| 55 |
-
),
|
| 56 |
-
]
|
| 57 |
|
| 58 |
def _info(self):
|
| 59 |
return datasets.DatasetInfo(
|
| 60 |
description=_DESCRIPTION,
|
| 61 |
features=datasets.Features(
|
| 62 |
{
|
| 63 |
-
"
|
| 64 |
-
"
|
| 65 |
-
|
| 66 |
-
"question_content": datasets.Value("string"),
|
| 67 |
-
"best_answer": datasets.Value("string"),
|
| 68 |
-
},
|
| 69 |
),
|
| 70 |
-
|
| 71 |
-
|
|
|
|
| 72 |
)
|
| 73 |
|
| 74 |
def _split_generators(self, dl_manager):
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
# Extracting (un-taring) the training data
|
| 78 |
-
data_dir = os.path.join(data_dir, "yahoo_answers_csv")
|
| 79 |
return [
|
| 80 |
-
datasets.SplitGenerator(
|
| 81 |
-
|
| 82 |
-
),
|
| 83 |
-
datasets.SplitGenerator(
|
| 84 |
-
name=datasets.Split.TEST, gen_kwargs={"filepath": os.path.join(data_dir, "test.csv")}
|
| 85 |
-
),
|
| 86 |
]
|
| 87 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 88 |
def _generate_examples(self, filepath):
|
| 89 |
with open(filepath, encoding="utf-8") as f:
|
| 90 |
rows = csv.reader(f)
|
|
|
|
| 15 |
"""Yahoo! Answers Topic Classification Dataset"""
|
| 16 |
|
| 17 |
|
| 18 |
+
_TRAIN_DOWNLOAD_URL = "https://drive.google.com/file/d/1Ehv1SSZ4n7ZLpUp7aSKNwHuC8UOgdfzL/view?usp=sharing"
|
| 19 |
+
_TEST_DOWNLOAD_URL = "https://drive.google.com/file/d/1UWUuTEkK20Pz-H0rt78n91hHeVUhtCh1/view?usp=sharing"
|
| 20 |
|
|
|
|
| 21 |
|
| 22 |
+
class AGNews(datasets.GeneratorBasedBuilder):
|
| 23 |
+
"""AG News topic classification dataset."""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
def _info(self):
|
| 26 |
return datasets.DatasetInfo(
|
| 27 |
description=_DESCRIPTION,
|
| 28 |
features=datasets.Features(
|
| 29 |
{
|
| 30 |
+
"text": datasets.Value("string"),
|
| 31 |
+
"label": datasets.features.ClassLabel(names=["World", "Sports", "Business", "Sci/Tech"]),
|
| 32 |
+
}
|
|
|
|
|
|
|
|
|
|
| 33 |
),
|
| 34 |
+
homepage="http://groups.di.unipi.it/~gulli/AG_corpus_of_news_articles.html",
|
| 35 |
+
citation=_CITATION,
|
| 36 |
+
task_templates=[TextClassification(text_column="text", label_column="label")],
|
| 37 |
)
|
| 38 |
|
| 39 |
def _split_generators(self, dl_manager):
|
| 40 |
+
train_path = dl_manager.download_and_extract(_TRAIN_DOWNLOAD_URL)
|
| 41 |
+
test_path = dl_manager.download_and_extract(_TEST_DOWNLOAD_URL)
|
|
|
|
|
|
|
| 42 |
return [
|
| 43 |
+
datasets.SplitGenerator(name=datasets.Split.TRAIN, gen_kwargs={"filepath": train_path}),
|
| 44 |
+
datasets.SplitGenerator(name=datasets.Split.TEST, gen_kwargs={"filepath": test_path}),
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
]
|
| 46 |
|
| 47 |
+
def _generate_examples(self, filepath):
|
| 48 |
+
"""Generate AG News examples."""
|
| 49 |
+
with open(filepath, encoding="utf-8") as csv_file:
|
| 50 |
+
csv_reader = csv.reader(
|
| 51 |
+
csv_file, quotechar='"', delimiter=",", quoting=csv.QUOTE_ALL, skipinitialspace=True
|
| 52 |
+
)
|
| 53 |
+
for id_, row in enumerate(csv_reader):
|
| 54 |
+
label, title, description = row
|
| 55 |
+
# Original labels are [1, 2, 3, 4] ->
|
| 56 |
+
# ['World', 'Sports', 'Business', 'Sci/Tech']
|
| 57 |
+
# Re-map to [0, 1, 2, 3].
|
| 58 |
+
label = int(label) - 1
|
| 59 |
+
text = " ".join((title, description))
|
| 60 |
+
yield id_, {"text": text, "label": label}
|
| 61 |
+
|
| 62 |
+
|
| 63 |
def _generate_examples(self, filepath):
|
| 64 |
with open(filepath, encoding="utf-8") as f:
|
| 65 |
rows = csv.reader(f)
|