# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

import nltk
from nltk.collocations import *
import pandas as pd

with open('data.txt', 'rb') as f:
    data = f.read()

data = str(data).split(" ")

bigram_measures = nltk.collocations.BigramAssocMeasures()
finder = BigramCollocationFinder.from_words(data)
finder.apply_freq_filter(15) 
finder.nbest(bigram_measures.raw_freq, 15)

# +
bigram_measures = nltk.collocations.BigramAssocMeasures()
finder = BigramCollocationFinder.from_words(data)
finder.apply_freq_filter(15) 
df=finder.nbest(bigram_measures.raw_freq, 15)

num = len(df)
tempword = ''
wtempword = ''
whole = []

for i in range (0, num):
    tempnum = len(df[i])
    for x in range (0, tempnum):
        tempword = df[i][x]
        wtempword = wtempword +  " " + tempword
    whole.append(wtempword.lstrip())
    tempword = ""
    wtempword = ""
    
Resturants =  pd.read_excel(r"From2016_Sentimental_Burpple.xlsx")
Reviews = Resturants['Cleaned_Reviews'].tolist()
polarity = Resturants['polarity'].tolist()

num = len(whole)
num1 = len(Reviews)
count = 0
countlist = []
targetword = ''
targetreview = ''
average = float(0)
total = float(0)
averagelist = []

for i in range (0, num):
    targetword = whole[i]
    for x in range( 0, num1):
        targetreview = str(Reviews[x])
        if targetreview.find(targetword) >= 0:
            count = count + 1
            total = total + polarity[x]
        targetreview = ""
    average = total/count
    averagelist.append(average)
    countlist.append(count)
    count = 0
    average = 0
    total = 0
    targetword = ""
    
Freq = pd.DataFrame()

se = pd.Series(whole)
Freq['Word'] = se

se = pd.Series(countlist)
Freq['Count'] = se

se = pd.Series(averagelist)
Freq['Average_Polarity'] = se

Freq
# -

trigram_measures = nltk.collocations.TrigramAssocMeasures()
finder = TrigramCollocationFinder.from_words(data)
finder.apply_freq_filter(15) 
finder.nbest(trigram_measures.raw_freq, 15)  

# +
trigram_measures = nltk.collocations.TrigramAssocMeasures()
finder = TrigramCollocationFinder.from_words(data)
finder.apply_freq_filter(15) 
df = finder.nbest(trigram_measures.raw_freq, 15)  


num = len(df)
tempword = ''
wtempword = ''
whole = []

for i in range (0, num):
    tempnum = len(df[i])
    for x in range (0, tempnum):
        tempword = df[i][x]
        wtempword = wtempword +  " " + tempword
    whole.append(wtempword.lstrip())
    tempword = ""
    wtempword = ""
    
Resturants =  pd.read_excel(r"From2016_Sentimental_Burpple.xlsx")
Reviews = Resturants['Cleaned_Reviews'].tolist()
polarity = Resturants['polarity'].tolist()

num = len(whole)
num1 = len(Reviews)
count = 0
countlist = []
targetword = ''
targetreview = ''
average = float(0)
total = float(0)
averagelist = []

for i in range (0, num):
    targetword = whole[i]
    for x in range( 0, num1):
        targetreview = str(Reviews[x])
        if targetreview.find(targetword) >= 0:
            count = count + 1
            total = total + polarity[x]
        targetreview = ""
    average = total/count
    averagelist.append(average)
    countlist.append(count)
    count = 0
    average = 0
    total = 0
    targetword = ""
    
Freq1 = pd.DataFrame()

se = pd.Series(whole)
Freq1['Word'] = se

se = pd.Series(countlist)
Freq1['Count'] = se

se = pd.Series(averagelist)
Freq1['Average_Polarity'] = se

Freq1
# -

finder = BigramCollocationFinder.from_words(data)
scored = finder.score_ngrams(bigram_measures.raw_freq)
set(bigram for bigram, score in scored) 
sorted(finder.nbest(bigram_measures.raw_freq,20))

# +
finder = BigramCollocationFinder.from_words(data)
scored = finder.score_ngrams(bigram_measures.raw_freq)
set(bigram for bigram, score in scored) 
df = sorted(finder.nbest(bigram_measures.raw_freq,20))


num = len(df)
tempword = ''
wtempword = ''
whole = []

for i in range (0, num):
    tempnum = len(df[i])
    for x in range (0, tempnum):
        tempword = df[i][x]
        wtempword = wtempword +  " " + tempword
    whole.append(wtempword.lstrip())
    tempword = ""
    wtempword = ""
    
Resturants =  pd.read_excel(r"From2016_Sentimental_Burpple.xlsx")
Reviews = Resturants['Cleaned_Reviews'].tolist()
polarity = Resturants['polarity'].tolist()

num = len(whole)
num1 = len(Reviews)
count = 0
countlist = []
targetword = ''
targetreview = ''
average = float(0)
total = float(0)
averagelist = []

for i in range (0, num):
    targetword = whole[i]
    for x in range( 0, num1):
        targetreview = str(Reviews[x])
        if targetreview.find(targetword) >= 0:
            count = count + 1
            total = total + polarity[x]
        targetreview = ""
    average = total/count
    averagelist.append(average)
    countlist.append(count)
    count = 0
    average = 0
    total = 0
    targetword = ""
    
Freq2 = pd.DataFrame()

se = pd.Series(whole)
Freq2['Word'] = se

se = pd.Series(countlist)
Freq2['Count'] = se

se = pd.Series(averagelist)
Freq2['Average_Polarity'] = se

Freq2
# -

finder = TrigramCollocationFinder.from_words(data)
scored = finder.score_ngrams(trigram_measures.raw_freq)
set(trigram for trigram, score in scored) 
sorted(finder.nbest(trigram_measures.raw_freq,20))

# +
finder = TrigramCollocationFinder.from_words(data)
scored = finder.score_ngrams(trigram_measures.raw_freq)
set(trigram for trigram, score in scored) 
df = sorted(finder.nbest(trigram_measures.raw_freq,20))


num = len(df)
tempword = ''
wtempword = ''
whole = []

for i in range (0, num):
    tempnum = len(df[i])
    for x in range (0, tempnum):
        tempword = df[i][x]
        wtempword = wtempword +  " " + tempword
    whole.append(wtempword.lstrip())
    tempword = ""
    wtempword = ""
    
Resturants =  pd.read_excel(r"From2016_Sentimental_Burpple.xlsx")
Reviews = Resturants['Cleaned_Reviews'].tolist()
polarity = Resturants['polarity'].tolist()

num = len(whole)
num1 = len(Reviews)
count = 0
countlist = []
targetword = ''
targetreview = ''
average = float(0)
total = float(0)
averagelist = []

for i in range (0, num):
    targetword = whole[i]
    for x in range( 0, num1):
        targetreview = str(Reviews[x])
        if targetreview.find(targetword) >= 0:
            count = count + 1
            total = total + polarity[x]
        targetreview = ""
        
    if count == 0 or total == 0:
        average = 0
    else:
        average = total/count
    
    averagelist.append(average)
    countlist.append(count)
    count = 0
    average = 0
    total = 0
    targetword = ""
    
Freq3 = pd.DataFrame()

se = pd.Series(whole)
Freq3['Word'] = se

se = pd.Series(countlist)
Freq3['Count'] = se

se = pd.Series(averagelist)
Freq3['Average_Polarity'] = se

Freq3

# +
everything = Freq.append(Freq1)
everything = everything.append(Freq2)
everything = everything.append(Freq3)

everything = everything.sort_values(['Average_Polarity'], ascending=[0])

everything
# -



StarcoderdataJupyterScriptsDedupFiltered