application_vnd.jupyter.stderr_output_1.png
from nltk.tokenize import sent_tokenize

from statistics import mean

import nltk

import numpy as np

import numpy as np # linear algebra

import pandas as pd

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

import re

import string

import nltk
from nltk.tokenize import sent_tokenize
tokenizer = nltk.data.load('tokenizers/punkt/english.pickle')
fp = open('/kaggle/input/hotel-text-data/text.txt')
data = fp.read()
sentences = sent_tokenize(data)

df = pd.DataFrame(sentences, columns=['sentences'])

cleanliness = ['satisfactory', 'ample', 'hygienic', 'proper', 'ambience', 'odour', 'dirty', 'clean', 'smell', 'cleanliness']
service = ['desk', 'check in', 'check out', 'reliable', 'fast', 'convenient', 'service', 'hospitality']
location = ['railway', 'view', 'station', 'airport', 'distance', 'far', 'close', 'train', 'metro', 'transport', 'market', 'mall', 'surrounding', 'areas', 'highway', 'traffic', 'out', 'location']
value = ['price', 'amount', 'rate', 'cheap', 'worth', 'low', 'money', 'economical', 'reasonable', 'fee', 'expensive', 'charge', 'value']
room = ['bed', 'bunkbeds', 'toilet', 'bathroom', 'shower', 'dryer', 'fridge', 'space', 'spacious', 'outdated', 'noisy', 'room']
food = ['drink', 'breakfast', 'spicy', 'food', 'tasty', 'tea', 'coffee', 'buffet', 'bar', 'restaurant', 'dinner', 'lunch', 'brunch', 'delicious']
facility = ['pool', 'gym', 'wifi', 'spa', 'internet', 'wireless', 'broken', 'parking', 'ventilation', 'maintained', 'facility', 'lot', 'premises']
staff = ['friendly', 'helpful', 'reliable', 'quick', 'good', 'polite', 'staff']

from nltk.corpus import wordnet
import string
from nltk import pos_tag
from nltk.corpus import stopwords
from nltk.tokenize import WhitespaceTokenizer
from nltk.stem import WordNetLemmatizer
def clean_text(text):
    text = text.lower()
    text = [word.strip(string.punctuation) for word in text.split(' ')]
    text = [word for word in text if not any((c.isdigit() for c in word))]
    text = [t for t in text if len(t) > 0]
    text = [t for t in text if len(t) > 1]
    text = ' '.join(text)
    return text
df['sentences_clean'] = df['sentences'].apply(lambda x: clean_text(x))

df1 = df[df['sentences_clean'] != '']

import re
wordlist = []
for i in df1['sentences_clean']:
    wordlist.append(re.split('\\s+', i))

np.array(wordlist)

a = []
for i in range(len(wordlist)):
    a.append(len(wordlist[i]))

def calculate(word, category):
    clean = []
    for i in range(len(word)):
        sum = 0
        sum1 = 0
        for j in range(len(word[i])):
            sum = sum + fuzz.ratio(word[i][j], category)
        clean.append(sum / a[i])
    return clean
c = []
for k in cleanliness:
    c.append(calculate(wordlist, k))

c = np.array(c).transpose()

s = []
for k in service:
    s.append(calculate(wordlist, k))

s = np.array(s).transpose()
se = []
for i in s:
    se.append(mean(i))

l = []
for k in location:
    l.append(calculate(wordlist, k))

l = np.array(l).transpose()
lo = []
for i in l:
    lo.append(mean(i))

v = []
for k in value:
    v.append(calculate(wordlist, k))
v = np.array(v).transpose()
va = []
for i in v:
    va.append(mean(i))

r = []
for k in room:
    r.append(calculate(wordlist, k))
r = np.array(r).transpose()
ro = []
for i in r:
    ro.append(mean(i))

f = []
for k in food:
    f.append(calculate(wordlist, k))
f = np.array(f).transpose()
fo = []
for i in f:
    fo.append(mean(i))