Python Code for AI Exercise: Assigning Sentiment Assignments for unknown words with Large Language Models

#AI Excercise

Github Link:

Nidhika1121/LLM_Sentiments: LLM_Sentiments (github.com)

Now, with this, exercise, sentiment can be assigned to the slangs in languages, even to unknown words, new words occurring in these times of app specific short words, added in our vocabulary.

In last article, we saw the AI exercise what 5 class sentiment alignment means.

However, the assignment of sentiments can be performed for existing three classes of neutral, positive and negative classes as well, this shall be covered in next article.

Here is the Python code for the 5 class sentiment alignment. The ipynb file is provided here and on Github. The codes corresponds to yesterdays article.

The results of running this code to determine the sentiment alignment of word nice, is as follows:

Neutral: Maximum Orietation 0.21501957

Bad: Maximum Orietation 0.6836092

Sad: Maximum Orietation 0.41360497

Happy: Maximum Orietation 0.5067967

Good: Maximum Orietation 1.0

The Maximum is for “nice” in the centre of cluster of the word “Good”

Note the word bad is showing a positive word too this can be set to right computations and evaluations when you compute similarity model.wv.similarity(“bad”,”good”) the value is high, I shall deal with this in coming artiles. This can be handled for automation using the right Large Language Model. 

The answer here is perfect, nice going to cluster of good, as nice is not a very high positively aligned word. 

 

Codes here:

 

 

“””AI Exercise: Sentiment_Of_Unknown_Words.ipynb

“””

 

# Mount your google drive in google colab

from google.colab import drive

drive.mount(‘/content/drive’)

import gensim

# Load Google’s pre-trained Word2Vec model.

model =  gensim.models.KeyedVectors.load_word2vec_format(‘/content/drive/MyDrive/GoogleNews-vectors-negative300.bin.gz’, binary=True)

 

vocabulary = model.wv.vocab

len(vocabulary)

model

similar_words = model.most_similar(“sad”, topn=20)

for w in similar_words:

 print(w)

 

similar_words = model.most_similar(“happy”, topn=20)

for w in similar_words:

 print(w)

 

similar_words = model.most_similar(“good”, topn=20)

for w in similar_words:

 print(w)

 

similar_words = model.most_similar(“bad”, topn=20)

for w in similar_words:

 print(w)

 

similar_words = model.most_similar(“neutral”, topn=20)

for w in similar_words:

 print(w)

 

sentiment_test = “nice”

similar_words = model.most_similar(sentiment_test, topn=10)

for w in similar_words:

 print(w[0])

 

similar_words = model.most_similar(“good“, topn=20)

for w in similar_words:

 print(w[0], “,”)

 

 

max = 0

i = 0 

index = 0

 

for w in similar_words:

 similarity = model.wv.similarity(sentiment_test, w[0])

 if max <= similarity:

   max =  similarity

   index = i

 print(“similarity between: “,  w[0],  ” and “, sentiment_test, ” is “,  similarity)

 i = i + 1 

 

print(“Maximum Orietation Towards”, similar_words[index][0], ” ” , max)

 

sentiment_test = “nice”

similar_words = model.most_similar(sentiment_test, topn=10)

for w in similar_words:

 print(w[0])

 

similar_words = model.most_similar(“happy“, topn=20)

for w in similar_words:

 print(w[0], “,”)

 

max = 0

i = 0 

index = 0

 

for w in similar_words:

 similarity = model.wv.similarity(sentiment_test, w[0])

 if max <= similarity:

   max =  similarity

   index = i

 print(“similarity between: “,  w[0],  ” and “, sentiment_test, ” is “,  similarity)

 i = i + 1 

 

print(“Maximum Orietation Towards”, similar_words[index][0], ” ” , max)

 

sentiment_test = “nice”

similar_words = model.most_similar(sentiment_test, topn=10)

 

 

for w in similar_words:

 print(w[0])

 

similar_words = model.most_similar(“sad“, topn=20)

for w in similar_words:

 print(w[0], “,”)

 

 

max = 0

i = 0 

index = 0

 

for w in similar_words:

 similarity = model.wv.similarity(sentiment_test, w[0])

 if max <= similarity:

   max =  similarity

   index = i

 print(“similarity between: “,  w[0],  ” and “, sentiment_test, ” is “,  similarity)

i = i + 1

print(“Maximum Orietation Towards”, similar_words[index][0], ” ” , max)

sentiment_test = “nice”

similar_words = model.most_similar(sentiment_test, topn=10)

for w in similar_words:

  print(w[0])

similar_words = model.most_similar(“bad“, topn=20)

for w in similar_words:

  print(w[0], “,”)

max = 0

i = 0 

index = 0

for w in similar_words:

  similarity = model.wv.similarity(sentiment_test, w[0])

  if max <= similarity:

    max =  similarity

    index = i

  print(“similarity between: “,  w[0],  ” and “, sentiment_test, ” is “,  similarity)

  i = i + 1 

print(“Maximum Orietation Towards”, similar_words[index][0], ” ” , max)

sentiment_test = “nice”

similar_words = model.most_similar(sentiment_test, topn=10)

for w in similar_words:

  print(w[0])

similar_words = model.most_similar(“neutral“, topn=20)

for w in similar_words:

  print(w[0], “,”)

max = 0

i = 0 

index = 0

for w in similar_words:

  similarity = model.wv.similarity(sentiment_test, w[0])

  if max <= similarity:

    max =  similarity

    index = i

  print(“similarity between: “,  w[0],  ” and “, sentiment_test, ” is “,  similarity)

  i = i + 1 

print(“Maximum Orietation Towards”, similar_words[index][0], ” ” , max)

Complete Code on Github

Published by Nidhika

In the AI blog, Nidhika Yadav covers topics of and related to Artificial Intelligence. She primarily talks about AI applications for good. She also talks about how AI can become harmful. She manages three independent blogs here, you can subscribe one or all of them. 1. Blog on Artificial Intelligence and future. https://nidhikayadav.org 2. In Blog on Global Issues and future, she covers important international issues and their future implications. https://nidhikayadav.com/ 3. Blog on cooking. Here she describes some delicious innovations and nutritious food. https://nidhikasrecipes.com/ Do subscribe to one or all of them.

Leave a comment