-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhelpers.py
More file actions
25 lines (21 loc) · 709 Bytes
/
Copy pathhelpers.py
File metadata and controls
25 lines (21 loc) · 709 Bytes
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
from nltk.tokenize import word_tokenize
import csv
def preproc(text):
""" Text preprocessing and tokenization """
tokens = word_tokenize(text.replace("<br />", "\n"))
return tokens
def read_file(file_name, limit=None):
""" Read CSV data file """
print("Reading", file_name)
data = []
csvfile = open(file_name, 'r')
for i, line in enumerate(csv.DictReader(csvfile, delimiter="\t")):
if limit and i > limit:
break
if i % 1000 == 999:
print(i+1, "comments")
line['tokens'] = preproc(line['review'])
if 'sentiment' in line:
line['sentiment'] = int(line['sentiment'])
data.append(line)
return data