I have a dataset from amazon reviews dataset: meta_Electronics.json.gz
The below code is given by instructor:
def read_product_description(fname):
'''
Load all product descriptions
Args:
fname: dataset file path
Returns:
dict: key is asin, value is description content
'''
result = {}
for i in parse(fname):
try:
if "Camera & Photo" in i["categories"][0]:
result[i["asin"]]=i["description"]
except:
continue
return result
I think the above code filters reviews in camera& photo category.
class TaggedDescriptionDocument(object):
'''
This class could save all products and review information in its dictionary and generate iter for TaggedDocument
which could used for Doc2Vec model
'''
def __init__(self, descriptondict):
self.descriptondict = descriptondict
def __iter__(self):
for asin in self.descriptondict:
for content in self.descriptondict[asin]:
yield TaggedDocument(clean_line(content), [asin])
Note: clean_line just cleans every single line in the content,remove punctuation,etc.
description_dict = read_product_description("meta_Electronics.json.gz")
des_documents = TaggedDescriptionDocument(description_dict)
After the above two functions,I think it creates a taggeddocument used for doc2vec model. However,when I tried to train a doc2vec model,it shows:
model_d = Doc2Vec(des_documents, vector_size=100, window=15, min_count=0, max_vocab_size=1000)
RuntimeError: you must first build vocabulary before training the model
The min_count is already 0. Is there anything wrong with the code? Any help will be appreciated!