-
Notifications
You must be signed in to change notification settings - Fork 3
/
generate_csv_nb_svc.py
54 lines (45 loc) · 1.61 KB
/
generate_csv_nb_svc.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
"""
Creating input file for running NB and SVM classifiers
"""
import json
import csv
import re
def normalize_text(text):
"""
Remove non-ASCII chars.
"""
text = re.sub('[^\x00-\x7F]+', ' ', text)
return text
def json_to_csv(json_file, csv_file, count=None):
"""
json_file ==> .json file to be converted
csv_file ==> output csv file
fileType ==> 'business' or 'reviews'
count ==> Enter number of records to be created, ignore if all records are needed.
"""
with open("restaurantID.txt", 'r') as bus_id:
id_list = bus_id.read()
if count is not None:
curr = 0
with open(csv_file, 'w', encoding='utf-8', errors='replace') as file:
csv_writer = csv.writer(file, lineterminator='\n')
csv_writer.writerow(["text", "stars"])
with open(json_file, encoding='utf-8', errors='replace') as j_file:
for line in j_file:
data = json.loads(line)
if data['business_id'] in id_list:
csv_writer.writerow([data['text'], data['stars']])
if count is not None:
curr += 1
if curr == count:
break
print("File {} created successfully.".format(csv_file))
def main():
"""
Entry-point for the function.
"""
json_file = "yelp_academic_dataset_review.json"
csv_file = "yelp_academic_dataset_review.csv"
json_to_csv(json_file, csv_file)
if __name__ == "__main__":
main()