def get_sentiment_sim(context_seqs, gen_seqs):
'''return the cosine similarity between the sentiment scores of each context and corresponding generated sequence;
the sentiment scores are given in spacy'''
gen_seqs = check_seqs_format(gen_seqs)
emotion_types = ['AFRAID', 'AMUSED', 'ANGRY', 'ANNOYED', 'DONT_CARE', 'HAPPY', 'INSPIRED', 'SAD']
gen_sentiment_sim_scores = []
for context_seq, gen_seqs_ in zip(context_seqs, gen_seqs):
context_sentiment = lexicon_methods.emotional_valence(encoder(context_seq))
context_sentiment = numpy.array([context_sentiment[emotion_type] for emotion_type in emotion_types]) + 1e-8 #add tiny number to avoid NaN when all scores are 0
sentiment_sim_scores = []
for gen_seq in gen_seqs_:
gen_sentiment = lexicon_methods.emotional_valence(encoder(gen_seq))
gen_sentiment = numpy.array([gen_sentiment[emotion_type] for emotion_type in emotion_types]) + 1e-8 #add tiny number to avoid NaN when all scores are 0
sentiment_sim = 1 - cosine(context_sentiment, gen_sentiment)
sentiment_sim_scores.append(sentiment_sim)
gen_sentiment_sim_scores.append(sentiment_sim_scores)
gen_sentiment_sim_scores = numpy.array(gen_sentiment_sim_scores)
return {'sentiment_sim_scores': gen_sentiment_sim_scores, 'mean_sentiment_sim_scores': numpy.mean(gen_sentiment_sim_scores)}
generation_metrics.py 文件源码
python
阅读 21
收藏 0
点赞 0
评论 0
评论列表
文章目录