<repo_name>kyleb-herts/sentimental-analysis<file_sep>/sentimental-analysis-plot-scatter-group.py

#!/usr/bin/python

#this code uses examples from https://nealcaren.org/lessons/wordlists/

import pandas as pd
from afinn import Afinn
import matplotlib.pyplot as plt
from datetime import datetime


def get_month(text_string):
    date = datetime.strptime(text_string, '%d/%m/%Y')
    return date.month


afinn = Afinn(language='en', emoticons=True)

pd.set_option('display.max_rows', None)

data = pd.read_csv("reviews_pseudoanonymised.csv")
data['month'] = data['date'].apply(get_month)
data['afinn_score'] = data['comment'].apply(afinn.score)

d = {"month":data['month'].array, "score":data['afinn_score'].array}
df = pd.DataFrame(data=d)
dk = df.groupby('month').sum()
dk.plot.bar()
plt.show()
<file_sep>/sentimental-analysis-plot-scatter.py

#!/usr/bin/python

#this code uses examples from https://nealcaren.org/lessons/wordlists/

import pandas as pd
from afinn import Afinn
import matplotlib.pyplot as plt


def modify_helpful_value(text_string):
    # NaN value reurn 1.0
    if text_string != text_string:
        return 1.0
    else:
        return text_string


afinn = Afinn(language='en', emoticons=True)

pd.set_option('display.max_rows', None)

data = pd.read_csv("reviews_pseudoanonymised.csv")
data['helpful'] = data['helpful'].apply(modify_helpful_value)
data['afinn_score'] = data['comment'].apply(afinn.score)

d = {"comment":data['afinn_score'].index.tolist(), "score":data['afinn_score'].array}
df = pd.DataFrame(data=d)
df.plot.scatter(x="comment", y="score")
plt.show()
d2 = {"comment":data['afinn_score'].index.tolist(), "helpful":data['helpful'].array}
df2 = pd.DataFrame(data=d2)
df2.plot.scatter(x="comment", y="helpful")
plt.show()
# df = pd.DataFrame({"x":keys, "score":values})
# ax = df.plot.scatter(x='x', y='score')
# plt.show()




# columns_to_display = ['comment', 'afinn_score']
# filtered_data = data.sort_values(by='afinn_score', ascending=False)[columns_to_display]

#print("\n")
#print(filtered_data)
#print("\n")
#print("Afinn Score From Comments Summary")
#print(data['afinn_score'].describe())

# filtered_data.to_csv('reviews_pseudoanonymised_afinn_score_applied.csv')
# data['afinn_score'].describe().to_csv('afinn_score_summary.csv')
# print("Sentiment Analyis completed - AFINN score applied")
<file_sep>/plot_chart.py
def add_chart():
    print("hello_world")
<file_sep>/standard_deviation_graph.py
#this code uses examples from https://nealcaren.org/lessons/wordlists/

import pandas as pd
from afinn import Afinn
import matplotlib.pyplot as plt
from datetime import datetime

afinn = Afinn(language='en', emoticons=True)

pd.set_option('display.max_rows', None)

data = pd.read_csv("reviews_pseudoanonymised.csv")
data['afinn_score'] = data['comment'].apply(afinn.score)

#Sets up blank y_co_ords array
y_co_ords=[]
#Iterates over data in panda array and adds it to python array
for i in data['afinn_score']:
  y_co_ords.append(i)

#Sorts the elements of the data into order
y_co_ords.sort()

#Sets minimum value as -14 (as this was established from previous analysis)
val=-14.0
#Array to be appended to depending on the frequency of each value
scores=[]
#Ready to store the x values of each score
x_values=[]

#Iterates 59 times due to range of scores that were established in previous analysis
for i in range(59):
  #Adds the frequency of a score to the scores array
  scores.append(y_co_ords.count(val))
  #Adds the x value that is to be corresponding to that score
  x_values.append(val)
  #Increments the val by 1 every iteration
  val=val+1

#Plots the frequency for the relevant scores
plt.scatter(x_values, scores)
#Labels the axes frequency and Score value
plt.xlabel('Score value', fontsize=18)
plt.ylabel('Frequency', fontsize=18)
#Displays the graph on the screen
plt.show()
<file_sep>/sentimental-analysis.py

#!/usr/bin/python

#this code uses examples from https://nealcaren.org/lessons/wordlists/

#import pandas an open source data analysis and manipulation tool - https://pandas.pydata.org/
import pandas as pd

#import afinn to  utilise afinn wordlist for sentimental analysis - https://github.com/fnielsen/afinn
from afinn import Afinn

def modify_helpful_value(text_string):
    # NaN value reurn 1.0
    if text_string != text_string:
        return 1.0
    else:
        return text_string

#set afinn language to English and enable afinn score of emoticons
afinn = Afinn(language='en', emoticons=True)

#in pandas set option to display all table rows
pd.set_option('display.max_rows', None)

#read data from provided csv file
data = pd.read_csv("reviews_pseudoanonymised.csv")

# Add the helpful column as the factor of the overall score
# if helpful column is nothing, return 1; otherwise, return original value
data['helpful'] = data['helpful'].apply(modify_helpful_value)

#apply afinn score to comments
data['afinn_score'] = data['comment'].apply(afinn.score)

#find word that match in afinn wordlist
data['afinn_triggers'] = data['comment'].apply(afinn.find_all)

#find scores of words matching afinn wordlist
data['afinn_trigger_words_scores'] = data['comment'].apply(afinn.scores_with_pattern)

#set column output to only be the comment and afinn score
columns_to_display = ['comment', 'afinn_score', 'afinn_triggers', 'afinn_trigger_words_scores']

#set rows to be sorted by afinn score
filtered_data = data.sort_values(by='afinn_score', ascending=False)[columns_to_display]

#set top 10 positive and top 10 negative comments as outputs
filtered_data_top_ten_positive = filtered_data.head(10)
filtered_data_top_ten_negative = filtered_data.sort_values(by='afinn_score', ascending=True).head(10)


#send outputs to terminal for testing
#print("\n")
#print(filtered_data)
#print("\n")
#print("Afinn Score From Comments Summary")
#print(data['afinn_score'].describe())
#print(filtered_data.head(10))
#print(filtered_data.sort_values(by='afinn_score', ascending=True).head(10))

#output data to csv files
filtered_data.to_csv('reviews_pseudoanonymised_afinn_score_applied.csv')
data['afinn_score'].describe().to_csv('afinn_score_summary.csv')
filtered_data_top_ten_positive.to_csv('afinn_score_top_ten_positive.csv')
filtered_data_top_ten_negative.to_csv('afinn_score_top_ten_negative.csv')

#notify user script has finished processing
print("Sentiment Analyis completed - AFINN score applied")

