-
Notifications
You must be signed in to change notification settings - Fork 111
/
convert_reviews.py
executable file
·64 lines (51 loc) · 1.76 KB
/
convert_reviews.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
#!/usr/bin/env python3
# Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
import sys
import time
import random
import json
def contains_illegal_chars(fields):
if "\u001a" in fields["reviewer_name"]:
return True
if "\u001a" in fields["title"]:
return True
if "\u001a" in fields["text"]:
return True
return False
def process(data):
fields = {}
fields["asin"] = data["asin"]
fields["timestamp"] = data["unixReviewTime"]
fields["reviewer_id"] = data["reviewerID"]
fields["reviewer_name"] = data["reviewerName"]
fields["title"] = data["summary"]
fields["text"] = data["reviewText"]
fields["stars"] = int(data["overall"])
fields["upvotes"] = int(data["helpful"][0])
fields["downvotes"] = int(data["helpful"][1] - data["helpful"][0])
if contains_illegal_chars(fields):
return None
document = {}
document["put"] = "id:review:review::" + fields["asin"] + "-" + fields["reviewer_id"]
document["fields"] = fields
return document
def main():
output_data = []
lines = 0
skipped = 0
for line in sys.stdin.readlines():
try:
lines += 1
if lines % 1000 == 0:
sys.stderr.write("Processed %d lines so far...\n" % lines)
processed = process(eval(line))
if processed is not None:
output_data.append(processed)
else:
skipped += 1
except Exception as e:
skipped += 1 # silently skip errors for now
print(json.dumps(output_data, indent=2))
sys.stderr.write("Done. Processed %d lines. Skipped %d lines, probably due to missing data.\n" % (lines, skipped))
if __name__ == "__main__":
main()