text_html_output_1.png text_plain_output_1.png
from collections import Counter

import numpy as np # linear algebra

import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)

csv = pd.read_csv('/kaggle/input/trip-advisor-hotel-reviews/tripadvisor_hotel_reviews.csv')
csv = csv.rename(columns={'Review': 'review', 'Rating': 'rating'})
csv['rating'] = csv['rating'] - 1

reviews = csv['review'].tolist()
ratings = csv['rating'].tolist()
features = {}

top200 = sorted(features, key=features.get, reverse=True)[:1000]
top400 = sorted(features, key=features.get, reverse=True)[:400]

train_test_split = 0.8
reviews_train = reviews[:round(train_test_split * len(reviews))]
reviews_test = reviews[round(train_test_split * len(reviews)):]
ratings_train = ratings[:round(train_test_split * len(ratings))]
ratings_test = ratings[round(train_test_split * len(ratings)):]

M200 = []
M400 = []
for review in reviews_train:
    c = Counter([review[i:i + 3] for i in range(len(review) - 2)])
    M200.append([c[g3] for g3 in top200])
    M400.append([c[g3] for g3 in top400])
M200 = np.array(M200)
M400 = np.array(M400)

M200_test = []
M400_test = []
for review in reviews_test:
    c = Counter([review[i:i + 3] for i in range(len(review) - 2)])
    M200_test.append([c[g3] for g3 in top200])
    M400_test.append([c[g3] for g3 in top400])
M200_test = np.array(M200_test)
M400_test = np.array(M400_test)

M200_test.shape