Marcio Monteiro
commited on
Commit
·
9f770cb
1
Parent(s):
9cd0946
first commit
Browse files- convert.py +71 -0
convert.py
ADDED
@@ -0,0 +1,71 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import csv
|
2 |
+
|
3 |
+
from random import shuffle
|
4 |
+
|
5 |
+
import pandas as pd
|
6 |
+
|
7 |
+
NEGATIVE = 0
|
8 |
+
POSITIVE = 1
|
9 |
+
|
10 |
+
ROTTEN = 0
|
11 |
+
FRESH = 1
|
12 |
+
|
13 |
+
|
14 |
+
def parse_is_top_critic(is_top_critic):
|
15 |
+
return is_top_critic == "True"
|
16 |
+
|
17 |
+
|
18 |
+
def parse_score_sentiment(score):
|
19 |
+
if score == "NEGATIVE":
|
20 |
+
return NEGATIVE
|
21 |
+
if score == "POSITIVE":
|
22 |
+
return POSITIVE
|
23 |
+
raise ValueError(f"Unknown score sentiment: {score}")
|
24 |
+
|
25 |
+
|
26 |
+
def parse_review_state(review_state):
|
27 |
+
if review_state == "rotten":
|
28 |
+
return ROTTEN
|
29 |
+
if review_state == "fresh":
|
30 |
+
return FRESH
|
31 |
+
|
32 |
+
raise ValueError(f"Unknown review state: {review_state}")
|
33 |
+
|
34 |
+
|
35 |
+
def run():
|
36 |
+
with open("rotten_tomatoes_movie_reviews.csv") as f:
|
37 |
+
reader = csv.DictReader(f)
|
38 |
+
rows = list(reader)
|
39 |
+
|
40 |
+
positive_rows = []
|
41 |
+
negative_rows = []
|
42 |
+
|
43 |
+
for row in rows:
|
44 |
+
row["isTopCritic"] = parse_is_top_critic(row["isTopCritic"])
|
45 |
+
row["scoreSentiment"] = parse_score_sentiment(row["scoreSentiment"])
|
46 |
+
row["reviewState"] = parse_review_state(row["reviewState"])
|
47 |
+
|
48 |
+
if row["scoreSentiment"] == POSITIVE:
|
49 |
+
positive_rows.append(row)
|
50 |
+
else:
|
51 |
+
negative_rows.append(row)
|
52 |
+
|
53 |
+
# Save rows to csv file called original.csv
|
54 |
+
pd.DataFrame(rows).to_csv("original.csv", index=False)
|
55 |
+
|
56 |
+
shuffle(positive_rows)
|
57 |
+
shuffle(negative_rows)
|
58 |
+
|
59 |
+
# Generate the balanced datasets
|
60 |
+
balanced_size = min(len(positive_rows), len(negative_rows))
|
61 |
+
balanced_rows = []
|
62 |
+
for i in range(0, balanced_size):
|
63 |
+
balanced_rows.append(positive_rows[i])
|
64 |
+
balanced_rows.append(negative_rows[i])
|
65 |
+
|
66 |
+
# Save balanced rows to csv file called balanced.csv
|
67 |
+
pd.DataFrame(balanced_rows).to_csv("balanced.csv", index=False)
|
68 |
+
|
69 |
+
|
70 |
+
if __name__ == "__main__":
|
71 |
+
run()
|