wuenlp_tools.utils.sentiment
1from pathlib import Path 2 3import pandas as pd 4 5_EMOTION_COLUMNS = ( 6 "anticipation", 7 "sadness", 8 "fear", 9 "anger", 10 "disgust", 11 "trust", 12 "surprise", 13 "joy", 14 "positive", 15 "negative", 16) 17 18 19class SentimentLexicon(object): 20 def __init__(self, lexicon: pd.DataFrame): 21 self.lexicon = lexicon 22 self._word_scores: dict[str, dict[str, float]] | None = None 23 24 def _ensure_word_scores(self) -> dict[str, dict[str, float]]: 25 if self._word_scores is None: 26 grouped = self.lexicon.groupby(level=0, sort=False).mean(numeric_only=True) 27 lookup: dict[str, dict[str, float]] = {} 28 for word, row in grouped.iterrows(): 29 lookup[str(word)] = { 30 "sentiment": float(row["positive"] - row["negative"]), 31 **{column: float(row[column]) for column in _EMOTION_COLUMNS}, 32 } 33 self._word_scores = lookup 34 return self._word_scores 35 36 def get_word_scores(self, word: str) -> dict[str, float] | None: 37 return self._ensure_word_scores().get(word) 38 39 def sentiment(self, word: str) -> float: 40 scores = self.get_word_scores(word) 41 if scores is None: 42 raise KeyError(word) 43 return scores["sentiment"] 44 45 def anger(self, word: str): 46 return self._column_score(word, "anger") 47 48 def anticipation(self, word: str): 49 return self._column_score(word, "anticipation") 50 51 def disgust(self, word: str): 52 return self._column_score(word, "disgust") 53 54 def fear(self, word: str): 55 return self._column_score(word, "fear") 56 57 def joy(self, word: str): 58 return self._column_score(word, "joy") 59 60 def sadness(self, word: str): 61 return self._column_score(word, "sadness") 62 63 def surprise(self, word: str): 64 return self._column_score(word, "surprise") 65 66 def trust(self, word: str): 67 return self._column_score(word, "trust") 68 69 def _column_score(self, word: str, column: str) -> float: 70 scores = self.get_word_scores(word) 71 if scores is None: 72 raise KeyError(word) 73 return scores[column] 74 75 def __contains__(self, item): 76 return item in self._ensure_word_scores()
class
SentimentLexicon:
20class SentimentLexicon(object): 21 def __init__(self, lexicon: pd.DataFrame): 22 self.lexicon = lexicon 23 self._word_scores: dict[str, dict[str, float]] | None = None 24 25 def _ensure_word_scores(self) -> dict[str, dict[str, float]]: 26 if self._word_scores is None: 27 grouped = self.lexicon.groupby(level=0, sort=False).mean(numeric_only=True) 28 lookup: dict[str, dict[str, float]] = {} 29 for word, row in grouped.iterrows(): 30 lookup[str(word)] = { 31 "sentiment": float(row["positive"] - row["negative"]), 32 **{column: float(row[column]) for column in _EMOTION_COLUMNS}, 33 } 34 self._word_scores = lookup 35 return self._word_scores 36 37 def get_word_scores(self, word: str) -> dict[str, float] | None: 38 return self._ensure_word_scores().get(word) 39 40 def sentiment(self, word: str) -> float: 41 scores = self.get_word_scores(word) 42 if scores is None: 43 raise KeyError(word) 44 return scores["sentiment"] 45 46 def anger(self, word: str): 47 return self._column_score(word, "anger") 48 49 def anticipation(self, word: str): 50 return self._column_score(word, "anticipation") 51 52 def disgust(self, word: str): 53 return self._column_score(word, "disgust") 54 55 def fear(self, word: str): 56 return self._column_score(word, "fear") 57 58 def joy(self, word: str): 59 return self._column_score(word, "joy") 60 61 def sadness(self, word: str): 62 return self._column_score(word, "sadness") 63 64 def surprise(self, word: str): 65 return self._column_score(word, "surprise") 66 67 def trust(self, word: str): 68 return self._column_score(word, "trust") 69 70 def _column_score(self, word: str, column: str) -> float: 71 scores = self.get_word_scores(word) 72 if scores is None: 73 raise KeyError(word) 74 return scores[column] 75 76 def __contains__(self, item): 77 return item in self._ensure_word_scores()