jeffnyman commited on
Commit
705e35f
·
1 Parent(s): 38b729b

Upload rotten_tomatoes_reviews.py

Browse files
Files changed (1) hide show
  1. rotten_tomatoes_reviews.py +114 -0
rotten_tomatoes_reviews.py ADDED
@@ -0,0 +1,114 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import datasets
2
+ from datasets.tasks import TextClassification
3
+
4
+ _DESCRIPTION = """
5
+ Movie Review Dataset.
6
+
7
+ This is a dataset containing 4,265 positive and 4,265 negative processed
8
+ sentences from Rotten Tomatoes movie reviews.
9
+ """
10
+
11
+ _CITATION = """
12
+ @InProceedings{Pang+Lee:05a,
13
+ author = {Bo Pang and Lillian Lee},
14
+ title = {Seeing stars: Exploiting class relationships for sentiment
15
+ categorization with respect to rating scales},
16
+ booktitle = {Proceedings of the ACL},
17
+ year = 2005
18
+ }
19
+ """
20
+
21
+ _DOWNLOAD_URL = "https://testerstories/files/ai_learn/rt-polaritydata.tar.gz"
22
+
23
+
24
+ class RottenTomatoesReviews(datasets.GeneratorBasedBuilder):
25
+ VERSION = datasets.Version("1.0.0")
26
+
27
+ def _info(self):
28
+ return datasets.DatasetInfo(
29
+ description=_DESCRIPTION,
30
+ features=datasets.Features(
31
+ {
32
+ "text": datasets.Value("string"),
33
+ "label": datasets.features.ClassLabel(names=["neg", "pos"]),
34
+ }
35
+ ),
36
+ supervised_keys=[""],
37
+ homepage="http://www.cs.cornell.edu/people/pabo/movie-review-data/",
38
+ citation=_CITATION,
39
+ task_templates=[
40
+ TextClassification(text_column="text", label_column="label")
41
+ ],
42
+ )
43
+
44
+ def _split_generators(self, dl_manager):
45
+ archive = dl_manager.download(_DOWNLOAD_URL)
46
+
47
+ return [
48
+ datasets.SplitGenerator(
49
+ name=datasets.Split.TRAIN,
50
+ gen_kwargs={
51
+ "split_key": "train",
52
+ "files": dl_manager.iter_archive(archive),
53
+ },
54
+ ),
55
+ datasets.SplitGenerator(
56
+ name=datasets.Split.VALIDATION,
57
+ gen_kwargs={
58
+ "split_key": "validation",
59
+ "files": dl_manager.iter_archive(archive),
60
+ },
61
+ ),
62
+ datasets.SplitGenerator(
63
+ name=datasets.Split.TEST,
64
+ gen_kwargs={
65
+ "split_key": "test",
66
+ "files": dl_manager.iter_archive(archive),
67
+ },
68
+ ),
69
+ ]
70
+
71
+ def _get_examples_from_split(self, split_key, files):
72
+ data_dir = "rt-polaritydata/"
73
+ pos_samples, neg_samples = None, None
74
+
75
+ for path, f in files:
76
+ if path == data_dir + "rt-polarity.pos":
77
+ pos_samples = [line.decode("latin-1").strip() for line in f]
78
+ elif path == data_dir + "rt-polarity.neg":
79
+ neg_samples = [line.decode("latin-1").strip() for line in f]
80
+
81
+ if pos_samples is not None and neg_samples is not None:
82
+ break
83
+
84
+ i1 = int(len(pos_samples) * 0.8 + 0.5)
85
+ i2 = int(len(pos_samples) * 0.9 + 0.5)
86
+
87
+ train_samples = pos_samples[:i1] + neg_samples[:i1]
88
+ train_labels = (["pos"] * i1) + (["neg"] * i1)
89
+
90
+ validation_samples = pos_samples[i1:i2] + neg_samples[i1:i2]
91
+ validation_labels = (["pos"] * (i2 - i1)) + (["neg"] * (i2 - i1))
92
+
93
+ test_samples = pos_samples[i2:] + neg_samples[i2:]
94
+ test_labels = (["pos"] * (len(pos_samples) - i2)) + (
95
+ ["neg"] * (len(pos_samples) - i2)
96
+ )
97
+
98
+ if split_key == "train":
99
+ return (train_samples, train_labels)
100
+ if split_key == "validation":
101
+ return (validation_samples, validation_labels)
102
+ if split_key == "test":
103
+ return (test_samples, test_labels)
104
+ else:
105
+ raise ValueError(f"Invalid split key {split_key}")
106
+
107
+ def _generate_examples(self, split_key, files):
108
+ split_text, split_labels = self._get_examples_from_split(split_key, files)
109
+
110
+ for text, label in zip(split_text, split_labels):
111
+ data_key = split_key + "_" + text
112
+ feature_dict = {"text": text, "label": label}
113
+
114
+ yield data_key, feature_dict