ebrigham commited on
Commit
b767009
·
1 Parent(s): 30f98f7

Update labels.py

Browse files
Files changed (1) hide show
  1. labels.py +49 -25
labels.py CHANGED
@@ -51,44 +51,68 @@ _CITATION = """\
51
  _TRAIN_DOWNLOAD_URL = "https://github.com/edubrigham/data/blob/541db4cdbb566aa5909e3eb4904d64f9683e5d4a/yahoo_answers_csv/train.csv"
52
  _TEST_DOWNLOAD_URL = "https://github.com/edubrigham/data/blob/541db4cdbb566aa5909e3eb4904d64f9683e5d4a/yahoo_answers_csv/test.csv"
53
 
 
 
 
 
 
 
 
 
 
 
 
 
54
 
55
- class AGNews(datasets.GeneratorBasedBuilder):
56
- """AG News topic classification dataset."""
 
 
 
 
 
 
 
 
 
57
 
58
  def _info(self):
59
  return datasets.DatasetInfo(
60
  description=_DESCRIPTION,
61
  features=datasets.Features(
62
  {
63
- "text": datasets.Value("string"),
64
- "labels": datasets.features.ClassLabel(names=["Society & Culture", "Science & Mathematics", "Health","Education & Reference", "Computers & Internet", "Sports", "Business & Finance", "Entertainment & Music", "Family & Relationships", "Politics & Government"]),
65
- }
 
 
 
66
  ),
67
- homepage="http://groups.di.unipi.it/~gulli/AG_corpus_of_news_articles.html",
68
- citation=_CITATION,
69
- task_templates=[TextClassification(text_column="text", label_column="labels")],
70
  )
 
71
 
72
  def _split_generators(self, dl_manager):
73
  train_path = dl_manager.download_and_extract(_TRAIN_DOWNLOAD_URL)
74
  test_path = dl_manager.download_and_extract(_TEST_DOWNLOAD_URL)
75
  return [
76
- datasets.SplitGenerator(name=datasets.Split.TRAIN, gen_kwargs={"filepath": train_path}),
77
- datasets.SplitGenerator(name=datasets.Split.TEST, gen_kwargs={"filepath": test_path}),
 
 
 
78
  ]
79
-
80
- def _generate_examples(self, filepath):
81
- """Generate AG News examples."""
82
- with open(filepath, encoding="utf-8") as csv_file:
83
- csv_reader = csv.reader(
84
- csv_file, quotechar='"', delimiter=",", quoting=csv.QUOTE_ALL, skipinitialspace=True
85
- )
86
- for id_, row in enumerate(csv_reader):
87
- label, title, description = row
88
- # Original labels are [1, 2, 3, 4] ->
89
- # ['World', 'Sports', 'Business', 'Sci/Tech']
90
- # Re-map to [0, 1, 2, 3].
91
- label = int(label) - 1
92
- text = " ".join((title, description))
93
- yield id_, {"text": text, "labels": label}
94
 
 
51
  _TRAIN_DOWNLOAD_URL = "https://github.com/edubrigham/data/blob/541db4cdbb566aa5909e3eb4904d64f9683e5d4a/yahoo_answers_csv/train.csv"
52
  _TEST_DOWNLOAD_URL = "https://github.com/edubrigham/data/blob/541db4cdbb566aa5909e3eb4904d64f9683e5d4a/yahoo_answers_csv/test.csv"
53
 
54
+ _TOPICS = [
55
+ "Society & Culture",
56
+ "Science & Mathematics",
57
+ "Health",
58
+ "Education & Reference",
59
+ "Computers & Internet",
60
+ "Sports",
61
+ "Business & Finance",
62
+ "Entertainment & Music",
63
+ "Family & Relationships",
64
+ "Politics & Government",
65
+ ]
66
 
67
+
68
+ class YahooAnswersTopics(datasets.GeneratorBasedBuilder):
69
+ "Yahoo! Answers Topic Classification Dataset"
70
+
71
+ VERSION = datasets.Version("1.0.0")
72
+ BUILDER_CONFIGS = [
73
+ datasets.BuilderConfig(
74
+ name="yahoo_answers_topics",
75
+ version=datasets.Version("1.0.0", ""),
76
+ ),
77
+ ]
78
 
79
  def _info(self):
80
  return datasets.DatasetInfo(
81
  description=_DESCRIPTION,
82
  features=datasets.Features(
83
  {
84
+ "id": datasets.Value("int32"),
85
+ "topic": datasets.features.ClassLabel(names=_TOPICS),
86
+ "question_title": datasets.Value("string"),
87
+ "question_content": datasets.Value("string"),
88
+ "best_answer": datasets.Value("string"),
89
+ },
90
  ),
91
+ supervised_keys=None,
92
+ homepage="https://github.com/LC-John/Yahoo-Answers-Topic-Classification-Dataset",
 
93
  )
94
+
95
 
96
  def _split_generators(self, dl_manager):
97
  train_path = dl_manager.download_and_extract(_TRAIN_DOWNLOAD_URL)
98
  test_path = dl_manager.download_and_extract(_TEST_DOWNLOAD_URL)
99
  return [
100
+ datasets.SplitGenerator(
101
+ name=datasets.Split.TRAIN, gen_kwargs={"filepath": train_path}
102
+ ),
103
+ datasets.SplitGenerator(
104
+ name=datasets.Split.TEST, gen_kwargs={"filepath": test_path}),
105
  ]
106
+
107
+ def _generate_examples(self, filepath):
108
+ with open(filepath, encoding="utf-8") as f:
109
+ rows = csv.reader(f)
110
+ for i, row in enumerate(rows):
111
+ yield i, {
112
+ "id": i,
113
+ "topic": int(row[0]) - 1,
114
+ "question_title": row[1],
115
+ "question_content": row[2],
116
+ "best_answer": row[3],
117
+ }
 
 
 
118