|
""" |
|
Convert the Amazon reviews dataset to parquet format. |
|
|
|
Usage: |
|
$ make download |
|
$ python convert.py |
|
""" |
|
|
|
import os |
|
import gzip |
|
|
|
from slugify import slugify |
|
|
|
import pandas as pd |
|
|
|
|
|
OUTPUT_DIR = "amazon_reviews_2013" |
|
CHUNK_SIZE = 2000000 |
|
|
|
CATEGORIES = { |
|
"Amazon_Instant_Video.txt.gz": "Amazon Instant Video", |
|
"Arts.txt.gz": "Arts", |
|
"Automotive.txt.gz": "Automotive", |
|
"Baby.txt.gz": "Baby", |
|
"Beauty.txt.gz": "Beauty", |
|
"Books.txt.gz": "Book", |
|
"Cell_Phones_&_Accessories.txt.gz": "Cell Phone", |
|
"Clothing_&_Accessories.txt.gz": "Clothing", |
|
"Electronics.txt.gz": "Electronics", |
|
"Gourmet_Foods.txt.gz": "Gourmet Food", |
|
"Health.txt.gz": "Health", |
|
"Home_&_Kitchen.txt.gz": "Home & Kitchen", |
|
"Industrial_&_Scientific.txt.gz": "Industrial & Scientific", |
|
"Jewelry.txt.gz": "Jewelry", |
|
"Kindle_Store.txt.gz": "Kindle Store", |
|
"Movies_&_TV.txt.gz": "Movie & TV", |
|
"Musical_Instruments.txt.gz": "Musical Instrument", |
|
"Music.txt.gz": "Music", |
|
"Office_Products.txt.gz": "Office", |
|
"Patio.txt.gz": "Patio", |
|
"Pet_Supplies.txt.gz": "Pet Supply", |
|
"Shoes.txt.gz": "Shoe", |
|
"Software.txt.gz": "Software", |
|
"Sports_&_Outdoors.txt.gz": "Sports & Outdoor", |
|
"Tools_&_Home_Improvement.txt.gz": "Tools & Home Improvement", |
|
"Toys_&_Games.txt.gz": "Toy & Game", |
|
"Video_Games.txt.gz": "Video Game", |
|
"Watches.txt.gz": "Watch", |
|
} |
|
|
|
REVIEW_SCORE = { |
|
"1.0": 0, |
|
"2.0": 0, |
|
"4.0": 1, |
|
"5.0": 1, |
|
} |
|
|
|
CATEGORIES_LIST = list(CATEGORIES.values()) |
|
|
|
|
|
def to_parquet(categories, output_dir): |
|
""" |
|
Convert a single file to parquet |
|
""" |
|
n_chunks = 0 |
|
data = [] |
|
|
|
for filename in categories: |
|
|
|
for entry in parse_file(filename): |
|
if entry: |
|
data.append(entry) |
|
|
|
if len(data) == CHUNK_SIZE: |
|
save_parquet(data, n_chunks, output_dir) |
|
data = [] |
|
n_chunks += 1 |
|
|
|
if data: |
|
save_parquet(data, n_chunks, output_dir) |
|
n_chunks += 1 |
|
|
|
return n_chunks |
|
|
|
|
|
def save_parquet(data, chunk, output_dir): |
|
""" |
|
Save data to parquet |
|
""" |
|
fname = os.path.join(output_dir, f"complete-{chunk+1:04d}.parquet") |
|
|
|
df = pd.DataFrame(data) |
|
|
|
|
|
negative_rows = df[df["review/score"] == 0] |
|
positive_rows = df[df["review/score"] == 1] |
|
|
|
min_size = min(len(negative_rows), len(positive_rows)) |
|
rows_df = pd.concat([negative_rows.head(min_size), positive_rows.head(min_size)]) |
|
|
|
rows_df.to_parquet(fname, index=False) |
|
|
|
|
|
def parse_file(filename): |
|
""" |
|
Parse a single file. |
|
""" |
|
f = gzip.open(filename, "r") |
|
entry = {} |
|
for line in f: |
|
line = line.decode().strip() |
|
colon_pos = line.find(":") |
|
if colon_pos == -1: |
|
entry["product/category"] = CATEGORIES[filename] |
|
|
|
if entry["review/score"] == "3.0": |
|
entry = {} |
|
continue |
|
|
|
yield clean(entry) |
|
entry = {} |
|
continue |
|
e_name = line[:colon_pos] |
|
rest = line[colon_pos + 2 :] |
|
entry[e_name] = rest |
|
|
|
if entry and entry["review/score"] == "3.0": |
|
return |
|
|
|
yield clean(entry) |
|
|
|
|
|
def clean(entry): |
|
""" |
|
Clean the entry |
|
""" |
|
|
|
if not entry: |
|
return entry |
|
|
|
if entry["product/price"] == "unknown": |
|
entry["product/price"] = None |
|
|
|
entry["review/score"] = REVIEW_SCORE[entry["review/score"]] |
|
entry["review/time"] = int(entry["review/time"]) |
|
entry["product/category"] = int(CATEGORIES_LIST.index(entry["product/category"])) |
|
|
|
numerator, demoninator = entry["review/helpfulness"].split("/") |
|
numerator = int(numerator) |
|
demoninator = int(demoninator) |
|
|
|
if demoninator == 0: |
|
entry["review/helpfulness_ratio"] = 0 |
|
else: |
|
entry["review/helpfulness_ratio"] = numerator / demoninator |
|
|
|
entry["review/helpfulness_total_votes"] = demoninator |
|
|
|
|
|
del entry["review/userId"] |
|
del entry["review/profileName"] |
|
del entry["product/productId"] |
|
|
|
return entry |
|
|
|
|
|
def create_directories(): |
|
""" |
|
Create all output directories |
|
""" |
|
if not os.path.exists(OUTPUT_DIR): |
|
os.makedirs(OUTPUT_DIR, exist_ok=True) |
|
|
|
for category in CATEGORIES.values(): |
|
os.makedirs(os.path.join(OUTPUT_DIR, slugify(category)), exist_ok=True) |
|
|
|
os.makedirs(os.path.join(OUTPUT_DIR, "all"), exist_ok=True) |
|
|
|
|
|
def run(): |
|
""" |
|
Convert all files to parquet |
|
""" |
|
create_directories() |
|
|
|
to_parquet(CATEGORIES, os.path.join(OUTPUT_DIR, "all")) |
|
|
|
for path, category in CATEGORIES.items(): |
|
to_parquet( |
|
{path: category}, |
|
os.path.join(OUTPUT_DIR, slugify(category)), |
|
) |
|
|
|
|
|
if __name__ == "__main__": |
|
run() |
|
|