<jupyter_start><jupyter_text>Emotion Mining<jupyter_code>#Sentiment analysis
import pandas as pd
afinn = pd.read_csv('Afinn.csv', sep=',', encoding='latin-1')
afinn.shape
afinn.head(20)
from nltk import tokenize
red_unique = tokenize.sent_tokenize(" ".join(iphone_reviews))
red_unique[5:15]
sent_df = pd.DataFrame(red_unique, columns=['word'])
sent_df
affinity_scores = afinn.set_index('word')['value'].to_dict()
import spacy
spacy.load('en_core_web_sm')
from spacy.lang.en import English
#Custom function :score each word in a sentence in lemmatised form, 
#but calculate the score for the whole original sentence.
nlp = spacy.load('en_core_web_sm')
sentiment_lexicon = affinity_scores

def calculate_sentiment(text: str = None):
    sent_score = 0
    if text:
        word = nlp(text)
        for word in red_unique:
            sent_score += sentiment_lexicon.get(word.lemma_, 0)
    return sent_score
#Custom function :score each word in a sentence in lemmatised form, 
#but calculate the score for the whole original sentence.
nlp = spacy.load('en_core_web_sm')
sentiment_lexicon = affinity_scores

def calculate_sentiment(text: str = None):
    sent_score = 0
    if text:
        sentence = nlp(text)
        for word in sentence:
            sent_score += sentiment_lexicon.get(word.lemma_, 0)
    return sent_score
# test that it works
calculate_sentiment(text = 'amazing')
sent_df['sentiment_value'] = sent_df['word'].apply(calculate_sentiment)
# how many words are in the sentence?
sent_df['word_count'] = sent_df['word'].str.split().apply(len)
sent_df['word_count'].head(10)
sent_df.sort_values(by='sentiment_value').tail(10)
# Sentiment score of the whole review
sent_df['sentiment_value'].describe()
# Sentiment score of the whole review
sent_df[sent_df['sentiment_value']<=0].head()
sent_df[sent_df['sentiment_value']>=10].head()
sent_df['index']=range(0,len(sent_df))
import seaborn as sns
import matplotlib.pyplot as plt
sns.distplot(sent_df['sentiment_value'])
plt.figure(figsize=(15, 10))
sns.lineplot(y='sentiment_value',x='index',data=sent_df)
sent_df.plot.scatter(x='word_count', y='sentiment_value', figsize=(8,8), title='Sentence sentiment value to sentence word count')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12314606741573034, 'fraction_numerical': 0.010786516853932584, 'mean_word_length': 5.934579439252336, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '186021', 'license': 'Apache-2.0', 'n_tokens_mistral': 782, 'n_tokens_neox': 758, 'n_words': 177, 'path': 'Text Mining - ( Amazon Review ).ipynb', 'repo_name': 'shraddhaghadage/Text_Mining', 'size': '2225'}