-
Notifications
You must be signed in to change notification settings - Fork 5
/
imdb_reader.py
76 lines (63 loc) · 2.23 KB
/
imdb_reader.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import os
import pickle
import numpy as np
from bs4 import BeautifulSoup
train_path = "data/imdb/train/"
test_path = "data/imdb/train/"
save_path = "data/imdb/imdb.pcl"
label_dic = {"neg":0, "pos":1}
def get_imdb():
train_data = []
train_labels = []
test_data = []
test_labels = []
if os.path.exists(save_path):
print("loading imdb data from pickle...")
with open(save_path, mode='rb') as f:
data = pickle.load(f)
train_data, train_labels, test_data, test_labels = data
return train_data, train_labels, test_data, test_labels
# read train files
if os.path.exists(train_path):
for key, val in label_dic.items():
path = os.path.join(train_path, key)
if os.path.exists(path):
data = read_reviews(path)
l = len(data)
labels = np.full((l,), val)
train_data.extend(data)
train_labels.extend(labels)
else:
raise FileNotFoundError
else:
raise FileNotFoundError
# read test files
if os.path.exists(test_path):
for key, val in label_dic.items():
path = os.path.join(test_path, key)
if os.path.exists(path):
data = read_reviews(path)
l = len(data)
labels = np.full((l,), val)
test_data.extend(data)
test_labels.extend(labels)
else:
raise FileNotFoundError
else:
raise FileNotFoundError
assert len(train_data)==len(train_labels) == len(test_data) == len(test_labels)
with open(save_path, mode='wb') as f:
pickle.dump([train_data, train_labels, test_data, test_labels], f)
return train_data, train_labels, test_data, test_labels
def read_reviews(path):
review_files = [os.path.join(path, f)
for f in os.listdir(path)
if f.endswith(".txt")]
reviews = []
for file in review_files:
review = open(file, mode='r').read()
bs = BeautifulSoup(review, "html.parser")
lower_case = bs.get_text().lower()
sentences = lower_case.split('. ')
reviews.append(sentences)
return reviews