<jupyter_start><jupyter_text>> Performing the comparison of sentiment classification using a Word Embedding approach on a dummy text using brown corpus.<jupyter_code>import gensim
import nltk 
nltk.download('brown')
from nltk.corpus import brown    
import math
model = gensim.models.Word2Vec(brown.sents())
# Find the most closest values with their similarity score of word 'University' 
university_model = model.wv.most_similar('University', topn=3)
university_model
print('Closest words to University is', university_model[0][0], ',', university_model[1][0], ',', university_model[2][0])
# Calculaing the cosine similarity between the word 'University' and the top 3 closest words to it
cosine_university = dict()
for i, val  in enumerate(university_model):
  cosine_university[val[0]] = model.wv.similarity('University', val[0])
for i, val in enumerate(cosine_university):
  print('Cosine similarity between Univerity and ', val, 'is: ', cosine_university[val])
# Find the most closest values with their similarity score of word 'Nation' 
nation_model = model.wv.most_similar('Nation', topn=3)
nation_model
print('Closest words to Nation is', nation_model[0][0], ',', nation_model[1][0], ',', nation_model[2][0])
# Calculaing the cosine similarity between the word 'Nation' and the top 3 closest words to it
cosine_nation = dict()
for i, val  in enumerate(nation_model):
  cosine_nation[val[0]] = model.wv.similarity('Nation', val[0])
for i, val in enumerate(cosine_nation):
  print('Cosine similarity between Nation and ', val, 'is: ', cosine_nation[val])
# Finding the sentiment of the sentence "There was no suspense at all"
positive_sentence = "This is a great movie with lots of suspense"
negative_sentence = "The movie was predictable and quite boring"
positive_words = positive_sentence.split(' ')
negative_words = negative_sentence.split(' ')
# Calculating the positive average of the embeddings
sum_positive = 0 
for i in positive_words:
  temp_positives = sum(model.wv[i]) / len(model.wv[i])
  sum_positive += temp_positives
average_positive = sum_positive / len(positive_words)
average_positive
# Calculating the negative average of the embeddings 
sum_negative = 0 
for i in negative_words:
  temp_negatives = sum(model.wv[i]) / len(model.wv[i])
  sum_negative += temp_negatives
average_negative = sum_negative / len(negative_words)
average_negative
# Calulating the average of the sentence "There was no suspense at all"
new_sentence = "There was no suspense at all"
new_words = new_sentence.split(' ')
sum_new = 0 
for i in new_words:
  temp_new = sum(model.wv[i]) / len(model.wv[i])
  sum_new += temp_new
average_new = sum_new / len(new_words)
average_new
# Calculating the distance between average_new and average_positive 
dist_new_pos = math.sqrt((average_new - average_positive) ** 2)
# Calculating the distance between average_new and average_negative
dist_new_neg = math.sqrt((average_new - average_negative) ** 2)
dist_new_pos, dist_new_neg
final_sentiment = lambda x: x == "Positive" if x == dist_new_pos else "Negative"
print('Final sentiment of the sentence is: ', final_sentiment(min(dist_new_pos, dist_new_neg)))<jupyter_output>Final sentiment of the sentence is:  Negative
{'fraction_non_alphanumeric': 0.09987515605493133, 'fraction_numerical': 0.008114856429463172, 'mean_word_length': 6.138084632516704, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '421790', 'license': 'MIT', 'n_tokens_mistral': 1064, 'n_tokens_neox': 996, 'n_words': 319, 'path': 'Word Embedding/word_embedding_gensim.ipynb', 'repo_name': 'neerajp99/nlp_playground', 'size': '3204'}