imbesat-rizvi commited on
Commit
8f8eea5
1 Parent(s): baabeb7

Enabled config for with and without metadata such as headers, footers, quotes. Enabled direct download using sklearn

Browse files
Files changed (4) hide show
  1. dataset_info.json +0 -1
  2. dataset_infos.json +1 -0
  3. newsgroups.py +59 -11
  4. newsgroups.zip +0 -3
dataset_info.json DELETED
@@ -1 +0,0 @@
1
- {"description": "\nThe bydate version of the 20-newsgroup dataset fetched from scikit_learn and split in stratified manner into train, validation and test sets. The test set from the original 20 newsgroup dataset is retained while the original train set is split 80:20 into train and validation sets in stratified manner based on the newsgroup. The 20 different newsgroup are provided as the labels instead of config names as specified in the official huggingface dataset. Newsgroups are specified as labels to provide a simplified setup for text classification task. The 20 different newsgroup functioning as labels are:\n(1) alt.atheism\n(2) comp.graphics\n(3) comp.os.ms-windows.misc\n(4) comp.sys.ibm.pc.hardware\n(5) comp.sys.mac.hardware\n(6) comp.windows.x\n(7) misc.forsale\n(8) rec.autos\n(9) rec.motorcycles\n(10) rec.sport.baseball\n(11) rec.sport.hockey\n(12) sci.crypt\n(13) sci.electronics\n(14) sci.med\n(15) sci.space\n(16) soc.religion.christian\n(17) talk.politics.guns\n(18) talk.politics.mideast\n(19) talk.politics.misc\n(20) talk.religion.misc", "citation": "\n@inproceedings{Lang95,\n author = {Ken Lang},\n title = {Newsweeder: Learning to filter netnews}\n year = {1995}\n booktitle = {Proceedings of the Twelfth International Conference on Machine Learning}\n pages = {331-339}\n }\n ", "homepage": "http://qwone.com/~jason/20Newsgroups/", "license": "", "features": {"text": {"dtype": "large_string", "id": null, "_type": "Value"}, "labels": {"num_classes": 20, "names": ["alt.atheism", "comp.graphics", "comp.os.ms-windows.misc", "comp.sys.ibm.pc.hardware", "comp.sys.mac.hardware", "comp.windows.x", "misc.forsale", "rec.autos", "rec.motorcycles", "rec.sport.baseball", "rec.sport.hockey", "sci.crypt", "sci.electronics", "sci.med", "sci.space", "soc.religion.christian", "talk.politics.guns", "talk.politics.mideast", "talk.politics.misc", "talk.religion.misc"], "id": null, "_type": "ClassLabel"}}, "post_processed": null, "supervised_keys": null, "task_templates": null, "builder_name": "newsgroups", "config_name": "default", "version": {"version_str": "2.0.0", "description": null, "major": 2, "minor": 0, "patch": 0}, "splits": {"train": {"name": "train", "num_bytes": 17065029, "num_examples": 9051, "dataset_name": "newsgroups"}, "validation": {"name": "validation", "num_bytes": 4279761, "num_examples": 2263, "dataset_name": "newsgroups"}, "test": {"name": "test", "num_bytes": 13328728, "num_examples": 7532, "dataset_name": "newsgroups"}}, "download_checksums": {"https://huggingface.co/datasets/pensieves/newsgroups/resolve/main/newsgroups.zip": {"num_bytes": 14404706, "checksum": "e18ecd06210a2a95a0c98a9e5be09c2c438be3cd10f66b3bdc85ccee3f153bfd"}}, "download_size": 14404706, "post_processing_size": null, "dataset_size": 34673518, "size_in_bytes": 49078224}
 
 
dataset_infos.json ADDED
@@ -0,0 +1 @@
 
 
1
+ {"with_metadata": {"description": "The bydate version of the 20-newsgroup dataset fetched from scikit_learn and\nsplit in stratified manner into train, validation and test sets. With and\nwithout metadata is made available as individual config names. The test set\nfrom the original 20 newsgroup dataset is retained while the original train\nset is split 80:20 into train and validation sets in stratified manner based\non the newsgroup. The 20 different newsgroup are provided as the labels\ninstead of config names as specified in the official huggingface dataset.\nNewsgroups are specified as labels to provide a simplified setup for text\nclassification task. The 20 different newsgroup functioning as labels are:\n(1) alt.atheism\n(2) comp.graphics\n(3) comp.os.ms-windows.misc\n(4) comp.sys.ibm.pc.hardware\n(5) comp.sys.mac.hardware\n(6) comp.windows.x\n(7) misc.forsale\n(8) rec.autos\n(9) rec.motorcycles\n(10) rec.sport.baseball\n(11) rec.sport.hockey\n(12) sci.crypt\n(13) sci.electronics\n(14) sci.med\n(15) sci.space\n(16) soc.religion.christian\n(17) talk.politics.guns\n(18) talk.politics.mideast\n(19) talk.politics.misc\n(20) talk.religion.misc", "citation": "\n@inproceedings{Lang95,\n author = {Ken Lang},\n title = {Newsweeder: Learning to filter netnews}\n year = {1995}\n booktitle = {Proceedings of the Twelfth International Conference on Machine Learning}\n pages = {331-339}\n }\n ", "homepage": "http://qwone.com/~jason/20Newsgroups/", "license": "", "features": {"text": {"dtype": "large_string", "id": null, "_type": "Value"}, "labels": {"num_classes": 20, "names": ["alt.atheism", "comp.graphics", "comp.os.ms-windows.misc", "comp.sys.ibm.pc.hardware", "comp.sys.mac.hardware", "comp.windows.x", "misc.forsale", "rec.autos", "rec.motorcycles", "rec.sport.baseball", "rec.sport.hockey", "sci.crypt", "sci.electronics", "sci.med", "sci.space", "soc.religion.christian", "talk.politics.guns", "talk.politics.mideast", "talk.politics.misc", "talk.religion.misc"], "id": null, "_type": "ClassLabel"}}, "post_processed": null, "supervised_keys": null, "task_templates": null, "builder_name": "newsgroups", "config_name": "with_metadata", "version": {"version_str": "2.0.0", "description": null, "major": 2, "minor": 0, "patch": 0}, "splits": {"train": {"name": "train", "num_bytes": 17065029, "num_examples": 9051, "dataset_name": "newsgroups"}, "validation": {"name": "validation", "num_bytes": 4279761, "num_examples": 2263, "dataset_name": "newsgroups"}, "test": {"name": "test", "num_bytes": 13328728, "num_examples": 7532, "dataset_name": "newsgroups"}}, "download_checksums": {}, "download_size": 0, "post_processing_size": null, "dataset_size": 34673518, "size_in_bytes": 34673518}, "without_metadata": {"description": "The bydate version of the 20-newsgroup dataset fetched from scikit_learn and\nsplit in stratified manner into train, validation and test sets. With and\nwithout metadata is made available as individual config names. The test set\nfrom the original 20 newsgroup dataset is retained while the original train\nset is split 80:20 into train and validation sets in stratified manner based\non the newsgroup. The 20 different newsgroup are provided as the labels\ninstead of config names as specified in the official huggingface dataset.\nNewsgroups are specified as labels to provide a simplified setup for text\nclassification task. The 20 different newsgroup functioning as labels are:\n(1) alt.atheism\n(2) comp.graphics\n(3) comp.os.ms-windows.misc\n(4) comp.sys.ibm.pc.hardware\n(5) comp.sys.mac.hardware\n(6) comp.windows.x\n(7) misc.forsale\n(8) rec.autos\n(9) rec.motorcycles\n(10) rec.sport.baseball\n(11) rec.sport.hockey\n(12) sci.crypt\n(13) sci.electronics\n(14) sci.med\n(15) sci.space\n(16) soc.religion.christian\n(17) talk.politics.guns\n(18) talk.politics.mideast\n(19) talk.politics.misc\n(20) talk.religion.misc", "citation": "\n@inproceedings{Lang95,\n author = {Ken Lang},\n title = {Newsweeder: Learning to filter netnews}\n year = {1995}\n booktitle = {Proceedings of the Twelfth International Conference on Machine Learning}\n pages = {331-339}\n }\n ", "homepage": "http://qwone.com/~jason/20Newsgroups/", "license": "", "features": {"text": {"dtype": "large_string", "id": null, "_type": "Value"}, "labels": {"num_classes": 20, "names": ["alt.atheism", "comp.graphics", "comp.os.ms-windows.misc", "comp.sys.ibm.pc.hardware", "comp.sys.mac.hardware", "comp.windows.x", "misc.forsale", "rec.autos", "rec.motorcycles", "rec.sport.baseball", "rec.sport.hockey", "sci.crypt", "sci.electronics", "sci.med", "sci.space", "soc.religion.christian", "talk.politics.guns", "talk.politics.mideast", "talk.politics.misc", "talk.religion.misc"], "id": null, "_type": "ClassLabel"}}, "post_processed": null, "supervised_keys": null, "task_templates": null, "builder_name": "newsgroups", "config_name": "without_metadata", "version": {"version_str": "2.0.0", "description": null, "major": 2, "minor": 0, "patch": 0}, "splits": {"train": {"name": "train", "num_bytes": 10649695, "num_examples": 9051, "dataset_name": "newsgroups"}, "validation": {"name": "validation", "num_bytes": 2755338, "num_examples": 2263, "dataset_name": "newsgroups"}, "test": {"name": "test", "num_bytes": 8011416, "num_examples": 7532, "dataset_name": "newsgroups"}}, "download_checksums": {}, "download_size": 0, "post_processing_size": null, "dataset_size": 21416449, "size_in_bytes": 21416449}}
newsgroups.py CHANGED
@@ -1,4 +1,7 @@
1
  import datasets
 
 
 
2
  import pandas as pd
3
 
4
  _NEWSGROUPS = [
@@ -24,14 +27,20 @@ _NEWSGROUPS = [
24
  'talk.religion.misc',
25
  ]
26
 
27
- _DESCRIPTION = """
28
- The bydate version of the 20-newsgroup dataset fetched from scikit_learn and split in stratified manner into train, validation and test sets. The test set from the original 20 newsgroup dataset is retained while the original train set is split 80:20 into train and validation sets in stratified manner based on the newsgroup. The 20 different newsgroup are provided as the labels instead of config names as specified in the official huggingface dataset. Newsgroups are specified as labels to provide a simplified setup for text classification task. The 20 different newsgroup functioning as labels are:
 
 
 
 
 
 
 
 
29
  """
 
30
  _DESCRIPTION += "\n".join(f"({i+1}) {j}" for i,j in enumerate(_NEWSGROUPS))
31
 
32
-
33
- _DOWNLOAD_URL = "https://huggingface.co/datasets/pensieves/newsgroups/resolve/main/newsgroups.zip"
34
-
35
  _HOMEPAGE = "http://qwone.com/~jason/20Newsgroups/"
36
 
37
  _CITATION = """
@@ -44,10 +53,35 @@ _CITATION = """
44
  }
45
  """
46
 
 
 
 
47
 
 
 
 
 
48
  class Newsgroups(datasets.GeneratorBasedBuilder):
49
 
50
- VERSION = datasets.utils.Version("2.0.0")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
51
 
52
  def _info(self):
53
 
@@ -69,12 +103,26 @@ class Newsgroups(datasets.GeneratorBasedBuilder):
69
 
70
  def _split_generators(self, dl_manager):
71
 
72
- data_path = dl_manager.download(_DOWNLOAD_URL)
73
- data_df = pd.read_csv(data_path, compression="zip")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
74
 
75
- train_df = data_df.query(f"split == 'train'").drop(columns="split")
76
- val_df = data_df.query(f"split == 'validation'").drop(columns="split")
77
- test_df = data_df.query(f"split == 'test'").drop(columns="split")
78
 
79
  return [
80
  datasets.SplitGenerator(
 
1
  import datasets
2
+ import textwrap
3
+ from sklearn.datasets import fetch_20newsgroups
4
+ from sklearn.model_selection import train_test_split
5
  import pandas as pd
6
 
7
  _NEWSGROUPS = [
 
27
  'talk.religion.misc',
28
  ]
29
 
30
+ _DESCRIPTION = textwrap.dedent("""\
31
+ The bydate version of the 20-newsgroup dataset fetched from scikit_learn and
32
+ split in stratified manner into train, validation and test sets. With and
33
+ without metadata is made available as individual config names. The test set
34
+ from the original 20 newsgroup dataset is retained while the original train
35
+ set is split 80:20 into train and validation sets in stratified manner based
36
+ on the newsgroup. The 20 different newsgroup are provided as the labels
37
+ instead of config names as specified in the official huggingface dataset.
38
+ Newsgroups are specified as labels to provide a simplified setup for text
39
+ classification task. The 20 different newsgroup functioning as labels are:
40
  """
41
+ )
42
  _DESCRIPTION += "\n".join(f"({i+1}) {j}" for i,j in enumerate(_NEWSGROUPS))
43
 
 
 
 
44
  _HOMEPAGE = "http://qwone.com/~jason/20Newsgroups/"
45
 
46
  _CITATION = """
 
53
  }
54
  """
55
 
56
+ _VERSION = datasets.utils.Version("2.0.0")
57
+
58
+ class NewsgroupsConfig(datasets.BuilderConfig):
59
 
60
+ def __init__(self, **kwargs):
61
+ super(NewsgroupsConfig, self).__init__(version=_VERSION, **kwargs)
62
+
63
+
64
  class Newsgroups(datasets.GeneratorBasedBuilder):
65
 
66
+ BUILDER_CONFIGS = [
67
+ NewsgroupsConfig(
68
+ name="with_metadata",
69
+ description=textwrap.dedent("""\
70
+ The original complete bydate 20-Newsgroups dataset with the headers,
71
+ footers, and quotes metadata as intact and just the continuous
72
+ whitespaces (including new-line) replaced by single whitespace
73
+ characters."""
74
+ ),
75
+ ),
76
+ NewsgroupsConfig(
77
+ name="without_metadata",
78
+ description=textwrap.dedent("""\
79
+ The bydate 20-Newsgroups dataset without the headers, footers,
80
+ and quotes metadata as well as the continuous whitespaces
81
+ (including new-line) replaced by single whitespace characters."""
82
+ ),
83
+ ),
84
+ ]
85
 
86
  def _info(self):
87
 
 
103
 
104
  def _split_generators(self, dl_manager):
105
 
106
+ if self.config.name == "with_metadata":
107
+ train_data = fetch_20newsgroups(subset="train", random_state=42)
108
+ test_data = fetch_20newsgroups(subset="test", random_state=42)
109
+
110
+ else:
111
+ train_data = fetch_20newsgroups(subset="train", random_state=42, remove=("headers", "footers", "quotes"))
112
+ test_data = fetch_20newsgroups(subset="test", random_state=42, remove=("headers", "footers", "quotes"))
113
+
114
+ train_labels = [train_data["target_names"][i] for i in train_data["target"]]
115
+ test_labels = [test_data["target_names"][i] for i in test_data["target"]]
116
+
117
+ train_df = pd.DataFrame({"text": train_data["data"], "labels": train_labels})
118
+ test_df = pd.DataFrame({"text": test_data["data"], "labels": test_labels})
119
+
120
+ train_df["text"] = train_df["text"].str.replace("\s+", " ", regex=True)
121
+ test_df["text"] = test_df["text"].str.replace("\s+", " ", regex=True)
122
 
123
+ train_df, val_df = train_test_split(train_df, test_size=0.2, random_state=42, stratify=train_df["labels"])
124
+ train_df = train_df.reset_index(drop=True)
125
+ val_df = val_df.reset_index(drop=True)
126
 
127
  return [
128
  datasets.SplitGenerator(
newsgroups.zip DELETED
@@ -1,3 +0,0 @@
1
- version https://git-lfs.github.com/spec/v1
2
- oid sha256:e18ecd06210a2a95a0c98a9e5be09c2c438be3cd10f66b3bdc85ccee3f153bfd
3
- size 14404706