ebrigham commited on
Commit
eafec65
1 Parent(s): ff7c822

Update labels.py

Browse files
Files changed (1) hide show
  1. labels.py +32 -43
labels.py CHANGED
@@ -18,9 +18,9 @@
18
 
19
 
20
  import csv
21
- import os
22
 
23
  import datasets
 
24
 
25
 
26
  _DESCRIPTION = """\
@@ -48,10 +48,20 @@ _CITATION = """\
48
  }
49
  """
50
 
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",
@@ -61,35 +71,12 @@ _TOPICS = [
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
  def _split_generators(self, dl_manager):
@@ -101,14 +88,16 @@ class YahooAnswersTopics(datasets.GeneratorBasedBuilder):
101
  ]
102
 
103
  def _generate_examples(self, filepath):
104
- with open(filepath, encoding="utf-8") as f:
105
- rows = csv.reader(f)
106
- for i, row in enumerate(rows):
107
- yield i, {
108
- "id": i,
109
- "topic": int(row[0]) - 1,
110
- "question_title": row[1],
111
- "question_content": row[2],
112
- "best_answer": row[3],
113
- }
114
-
 
 
 
18
 
19
 
20
  import csv
 
21
 
22
  import datasets
23
+ from datasets.tasks import TextClassification
24
 
25
 
26
  _DESCRIPTION = """\
 
48
  }
49
  """
50
 
51
+ _TRAIN_DOWNLOAD_URL = "https://github.com/edubrigham/data/blob/a43e2eb61d49a7c5f9b7856de2538dc1a7712e47/ag_news_csv/train.csv"
52
+ _TEST_DOWNLOAD_URL = "https://github.com/edubrigham/data/blob/a43e2eb61d49a7c5f9b7856de2538dc1a7712e47/ag_news_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
+ "label": datasets.features.ClassLabel(names=[
65
  "Society & Culture",
66
  "Science & Mathematics",
67
  "Health",
 
71
  "Business & Finance",
72
  "Entertainment & Music",
73
  "Family & Relationships",
74
+ "Politics & Government"]),
75
+ }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
76
  ),
77
+ homepage="http://groups.di.unipi.it/~gulli/AG_corpus_of_news_articles.html",
78
+ citation=_CITATION,
79
+ task_templates=[TextClassification(text_column="text", label_column="label")],
80
  )
81
 
82
  def _split_generators(self, dl_manager):
 
88
  ]
89
 
90
  def _generate_examples(self, filepath):
91
+ """Generate AG News examples."""
92
+ with open(filepath, encoding="utf-8") as csv_file:
93
+ csv_reader = csv.reader(
94
+ csv_file, quotechar='"', delimiter=",", quoting=csv.QUOTE_ALL, skipinitialspace=True
95
+ )
96
+ for id_, row in enumerate(csv_reader):
97
+ label, title, description = row
98
+ # Original labels are [1, 2, 3, 4] ->
99
+ # ['World', 'Sports', 'Business', 'Sci/Tech']
100
+ # Re-map to [0, 1, 2, 3].
101
+ label = int(label) - 1
102
+ text = " ".join((title, description))
103
+ yield id_, {"text": text, "label": label}