contemmcm commited on
Commit
3fbbc6d
1 Parent(s): 5dff079

Upload 2 files

Browse files
Files changed (2) hide show
  1. convert.py +19 -7
  2. polarity.csv +2 -2
convert.py CHANGED
@@ -1,8 +1,12 @@
1
  import csv
2
  import random
3
 
 
 
4
  import pandas as pd
5
 
 
 
6
 
7
  def remap_polarity(row):
8
  if row["label"] < 2:
@@ -15,15 +19,23 @@ def remap_polarity(row):
15
  def run():
16
  rows = []
17
 
18
- with open(
19
- "training.1600000.processed.noemoticon.csv", "r", encoding="ISO-8859-1"
20
- ) as file:
21
- reader = csv.reader(file)
 
 
 
 
 
 
22
  for row in reader:
23
- rows.append({"label": int(row[0]), "text": row[5]})
 
 
24
 
25
  df = pd.DataFrame(rows)
26
- df.to_csv("complete.csv", index=False)
27
 
28
  polarity_rows = [remap_polarity(row) for row in rows if row["label"] != 2]
29
  positive_rows = [row for row in polarity_rows if row["label"] == 1]
@@ -34,7 +46,7 @@ def run():
34
  random.shuffle(polarity_rows)
35
 
36
  df = pd.DataFrame(polarity_rows)
37
- df.to_csv("polarity.csv", index=False)
38
 
39
 
40
  if __name__ == "__main__":
 
1
  import csv
2
  import random
3
 
4
+ from io import StringIO
5
+
6
  import pandas as pd
7
 
8
+ from bs4 import BeautifulSoup
9
+
10
 
11
  def remap_polarity(row):
12
  if row["label"] < 2:
 
19
  def run():
20
  rows = []
21
 
22
+ with open("training.1600000.processed.noemoticon.csv", "rb") as file:
23
+ lines = [line[:-1] for line in file.readlines()]
24
+
25
+ for line in lines:
26
+ try:
27
+ data = StringIO(line.decode("utf-8"))
28
+ except UnicodeDecodeError:
29
+ data = StringIO(line.decode("latin-1", errors="ignore"))
30
+
31
+ reader = csv.reader(data, delimiter=",")
32
  for row in reader:
33
+ bs = BeautifulSoup(row[5], "lxml")
34
+ obj = {"label": int(row[0]), "text": bs.get_text()}
35
+ rows.append(obj)
36
 
37
  df = pd.DataFrame(rows)
38
+ df.to_csv("complete.csv", index=False, encoding="utf-8")
39
 
40
  polarity_rows = [remap_polarity(row) for row in rows if row["label"] != 2]
41
  positive_rows = [row for row in polarity_rows if row["label"] == 1]
 
46
  random.shuffle(polarity_rows)
47
 
48
  df = pd.DataFrame(polarity_rows)
49
+ df.to_csv("polarity.csv", index=False, encoding="utf8")
50
 
51
 
52
  if __name__ == "__main__":
polarity.csv CHANGED
@@ -1,3 +1,3 @@
1
  version https://git-lfs.github.com/spec/v1
2
- oid sha256:e1364036501f34082c11d08860c6962c94a591d4e81fc737beeb6e5aed2c63a1
3
- size 123551282
 
1
  version https://git-lfs.github.com/spec/v1
2
+ oid sha256:5c661cd1bb47f9c74eb29e8792f8ed6243c409887d80ab9e0c8ad1486c1f925a
3
+ size 123635617