text_plain_output_1.png
from pandas import DataFrame

from sklearn.feature_extraction.text import CountVectorizer, TfidfTransformer

from sklearn.neighbors import NearestNeighbors

import numpy as np

import pandas as pd

import re

data_meta = pd.read_csv('/kaggle/input/content-based/meta_full_csv')
data_meta = data_meta.sample(50000)
data_meta['summaryRev'] = data_meta['summary'] + ' ' + data_meta['reviewText']

data_meta.query("asin == 'B00GIDADP0'")

data_meta.query("asin == 'B002Q46RDW'")

data_meta_1 = data_meta[['asin', 'overall', 'unixReviewTime']]
data_meta_1
data_meta_2 = data_meta[['asin', 'overall', 'summaryRev']]

data_meta_2
regEx = re.compile('[^a-z]+')
def cleanReviews(reviewText):
    reviewText = reviewText.lower()
    reviewText = regEx.sub(' ', reviewText).strip()
    return reviewText
data_meta_2['summaryClean'] = data_meta_2['summaryRev'].apply(cleanReviews)
data_meta_2 = data_meta_2.reset_index()

reviews = data_meta_2['summaryClean']
countVector = CountVectorizer(max_features=300, stop_words='english')
transformedReviews = countVector.fit_transform(reviews)
dfReviews = DataFrame(transformedReviews.A, columns=countVector.get_feature_names())
dfReviews = dfReviews.astype(int)

X = np.array(dfReviews)
tpercent = 0.9
tsize = int(np.floor(tpercent * len(dfReviews)))
dfReviews_train = X[:tsize]
dfReviews_test = X[tsize:]
lentrain = len(dfReviews_train)
lentest = len(dfReviews_test)

neighbor = NearestNeighbors(n_neighbors=3, algorithm='ball_tree').fit(dfReviews_train)
distances, indices = neighbor.kneighbors(dfReviews_train)

for i in range(lentest):
    a = neighbor.kneighbors([dfReviews_test[i]])
    related_product_list = a[1]
    first_related_product = [item[0] for item in related_product_list]
    first_related_product = str(first_related_product).strip('[]')
    first_related_product = int(first_related_product)
    second_related_product = [item[1] for item in related_product_list]
    second_related_product = str(second_related_product).strip('[]')
    second_related_product = int(second_related_product)

X = np.array(dfReviews)
tpercent = 0.85
tsize = int(np.floor(tpercent * len(dfReviews)))
dfReviews_train = X[:tsize]
dfReviews_test = X[tsize:]
lentrain = len(dfReviews_train)
lentest = len(dfReviews_test)

neighbor = NearestNeighbors(n_neighbors=3, algorithm='ball_tree').fit(dfReviews_train)
distances, indices = neighbor.kneighbors(dfReviews_train)

for i in range(lentest):
    a = neighbor.kneighbors([dfReviews_test[i]])
    related_product_list = a[1]
    first_related_product = [item[0] for item in related_product_list]
    first_related_product = str(first_related_product).strip('[]')
    first_related_product = int(first_related_product)
    second_related_product = [item[1] for item in related_product_list]
    second_related_product = str(second_related_product).strip('[]')
    second_related_product = int(second_related_product)
    print('Based on product reviews, for ', data_meta_2['asin'][lentrain + i], ' average rating is ', data_meta_2['overall'][lentrain + i])
    print('The first similar product is ', data_meta_2['asin'][first_related_product], ' average rating is ', data_meta_2['overall'][first_related_product])
    print('The second similar product is ', data_meta_2['asin'][second_related_product], ' average rating is ', data_meta_2['overall'][second_related_product])
    print('-----------------------------------------------------------')