Skip to main content

Emotion analysis

English only, needs to translate first

We use the IsaacZhy/roberta-large-goemotions model for emotion identification as it supports 28 different emotion labels.

tokenizer = transformers.RobertaTokenizerFast.from_pretrained("IsaacZhy/roberta-large-goemotions")
model = transformers.RobertaForSequenceClassification.from_pretrained("IsaacZhy/roberta-large-goemotions")

id2label = {
"0": "admiration",
"1": "amusement",
"2": "anger",
"3": "annoyance",
"4": "approval",
"5": "caring",
"6": "confusion",
"7": "curiosity",
"8": "desire",
"9": "disappointment",
"10": "disapproval",
"11": "disgust",
"12": "embarrassment",
"13": "excitement",
"14": "fear",
"15": "gratitude",
"16": "grief",
"17": "joy",
"18": "love",
"19": "nervousness",
"20": "optimism",
"21": "pride",
"22": "realization",
"23": "relief",
"24": "remorse",
"25": "sadness",
"26": "surprise",
"27": "neutral"
}
def labelProbabilities(probabilities):
return {id2label[str(p)]: float(probabilities[0][p]) for p in range(len(probabilities[0]))}

def evaluate(sentence):
tokens = tokenizer.encode(sentence, return_tensors="pt")
with torch.no_grad():
result = model(tokens)
probabilities = torch.nn.functional.softmax(result.logits, dim=-1)
return labelProbabilities(probabilities)

def most_likely(sentence):
probabilities = evaluate(sentence)
return max(probabilities, key=probabilities.get)

It works well on tests but only works for English data. This is the case for all of the emotion classifiers I could find. Luckily, google automatically translate play store reviews to the language you need.

Some examples:

>>> most_likely("During covid, when all restaurants where closed OpenTable expired all my points. For I am deleting this App! Bad customer relationship management!")
'disappointment'

>>> most_likely("Try 3 crรฉdit cards for japanese restaurant at Tokyo... Never opentable want our credit card... Several restaurant at Tokyo use opentable, how is possible ? Gaijin can't use money ?? Bad night with family")
'curiosity'

>>> most_likely("Convenient, great experience")
'admiration'

>>> most_likely("Had the best time of my life")
'joy'

>>> most_likely("Obviously not the best way to pick a restaurant")
'disapproval'

While it can struggle on long comments, it can do pretty well on short form messages, with an understanding of the meaning of words and of negations.