├── README.md ├── emotion.py └── wnaffect.py /README.md: -------------------------------------------------------------------------------- 1 | # WNAffect 2 | WNAffect is a python package to get the emotion of a word using WordNet resources. 3 | 4 | ## Requirements 5 | This package requires NLTK, [WordNet 1.6 (UNIX-like)](http://wordnet.princeton.edu/wordnet/download/old-versions/) and [WordNet-Domains 3.2](http://wndomains.fbk.eu/download.html). 6 | 7 | ***WARNING: In the ```wn-domains-3.2/wn-affect-1.1/a-hierarchy.xml``` file, you should correct `simpathy` by `sympathy`.*** 8 | 9 | ## Introduction 10 | [WordNet](https://en.wikipedia.org/wiki/WordNet) is a lexical resource database for the English language. WordNet Domains 3.2 is a XML resource providing a set of emotional words organized in a tree: 11 | 12 | ``` 13 | ┌physical-state 14 | ├behaviour 15 | ├trait 16 | ├sensation 17 | ├situation┐ 18 | │ └emotion-eliciting-situation 19 | ├signal┐ 20 | │ └edonic-signal 21 | root┤ 22 | │ ┌cognitive-state 23 | │ ├cognitive-affective-state 24 | └mental-state┤ 25 | │ ┌mood 26 | └affective-state┤ 27 | │ ┌emotionlessness 28 | │ ┌apathy┤ 29 | │ │ └neutral-languor 30 | │ ┌neutral-emotion┤ 31 | │ │ └neutral-unconcern┐ 32 | │ │ │ ┌distance 33 | │ │ └indifference┤ 34 | │ │ └withdrawal 35 | │ │ ┌thing 36 | │ │ ├pensiveness 37 | │ │ ├gravity┐ 38 | │ │ │ └earnestness 39 | │ │ ├ambiguous-fear┐ 40 | │ │ │ └reverence 41 | │ │ │ ┌ambiguous-hope 42 | │ │ ├ambiguous-expectation┤ 43 | │ │ │ └fever┐ 44 | │ │ │ └buck-fever 45 | │ ├ambiguous-emotion┤ 46 | │ │ │ ┌unrest 47 | │ │ │ ├tumult 48 | │ │ ├ambiguous-agitation┤ 49 | │ │ │ │ ┌electricity 50 | │ │ │ └stir┤ 51 | │ │ │ └sensation 52 | │ │ └surprise┐ 53 | │ │ │ ┌surprise 54 | │ │ │ ├stupefaction 55 | │ │ └astonishment┤ 56 | │ │ └wonder┐ 57 | │ │ └awe 58 | │ │ ┌gratitude┐ 59 | │ │ │ └gratefulness 60 | │ │ ├levity┐ 61 | │ │ │ └playfulness 62 | │ │ ├positive-fear┐ 63 | │ │ │ └frisson 64 | │ │ ├fearlessness┐ 65 | │ │ │ └security┐ 66 | │ │ │ └confidence 67 | │ │ ├positive-expectation┐ 68 | │ │ │ └anticipation┐ 69 | │ │ │ └positive-suspense 70 | │ │ │ ┌self-esteem 71 | │ │ ├self-pride┤ 72 | │ │ │ ├amour-propre 73 | │ │ │ └ego 74 | │ │ │ ┌attachment 75 | │ │ │ ├protectiveness 76 | │ │ ├affection┤ 77 | │ │ │ ├soft-spot 78 | │ │ │ └regard 79 | │ │ │ ┌gusto 80 | │ │ │ ├exuberance 81 | │ │ ├enthusiasm┤ 82 | │ │ │ └eagerness┐ 83 | │ │ │ └enthusiasm-ardor 84 | │ │ │ ┌hopefulness 85 | │ │ │ ├encouragement 86 | │ │ ├positive-hope┤ 87 | │ │ │ └optimism┐ 88 | │ │ │ └sanguinity 89 | │ │ │ ┌placidity 90 | │ │ │ ├coolness 91 | │ │ ├calmness┤ 92 | │ │ │ │ ┌peace 93 | │ │ │ └tranquillity┤ 94 | │ │ │ └easiness┐ 95 | │ │ │ └positive-languor 96 | │ │ │ ┌worship 97 | │ │ │ ├love-ardor 98 | │ │ │ ├amorousness 99 | │ │ │ ├puppy-love 100 | │ │ │ ├devotion 101 | │ │ ├love┤ 102 | │ │ │ ├lovingness┐ 103 | │ │ │ │ └warmheartedness 104 | │ │ │ ├benevolence┐ 105 | │ │ │ │ └beneficence 106 | │ │ │ └loyalty 107 | │ ├positive-emotion┤ 108 | │ │ │ ┌amusement 109 | │ │ │ ├exuberance 110 | │ │ │ ├happiness 111 | │ │ │ ├bonheur 112 | │ │ │ ├gladness 113 | │ │ │ ├rejoicing 114 | │ │ │ ├elation┐ 115 | │ │ │ │ └euphoria 116 | │ │ │ ├exultation┐ 117 | │ │ │ │ └triumph 118 | │ │ │ │ ┌bang 119 | │ │ │ ├exhilaration┤ 120 | │ │ │ │ └titillation 121 | │ │ ├joy┤ 122 | │ │ │ ├contentment┐ 123 | │ │ │ │ │ ┌satisfaction-pride 124 | │ │ │ │ │ ├fulfillment 125 | │ │ │ │ └satisfaction┤ 126 | │ │ │ │ ├complacency┐ 127 | │ │ │ │ │ └smugness 128 | │ │ │ │ └gloat 129 | │ │ │ │ ┌comfortableness 130 | │ │ │ ├belonging┤ 131 | │ │ │ │ └closeness┐ 132 | │ │ │ │ └togetherness 133 | │ │ │ │ ┌hilarity 134 | │ │ │ ├merriment┤ 135 | │ │ │ │ ├jollity 136 | │ │ │ │ └jocundity 137 | │ │ │ │ ┌buoyancy 138 | │ │ │ └cheerfulness┤ 139 | │ │ │ └carefreeness 140 | │ │ │ ┌fondness 141 | │ │ │ ├captivation 142 | │ │ │ ├preference┐ 143 | │ │ │ │ └weakness 144 | │ │ │ ├approval┐ 145 | │ │ │ │ └favor 146 | │ │ │ ├admiration┐ 147 | │ │ │ │ └hero-worship 148 | │ │ └liking┤ 149 | │ │ │ ┌kindheartedness 150 | │ │ │ ├compatibility 151 | │ │ ├sympathy┤ 152 | │ │ │ ├empathy┐ 153 | │ │ │ │ └identification 154 | │ │ │ └positive-concern┐ 155 | │ │ │ └softheartedness 156 | │ │ │ ┌amicability 157 | │ │ └friendliness┤ 158 | │ │ ├brotherhood 159 | │ │ └good-will 160 | └emotion┤ 161 | │ ┌ingratitude 162 | │ ├daze 163 | │ │ ┌self-depreciation 164 | │ ├humility┤ 165 | │ │ └meekness 166 | │ │ ┌commiseration 167 | │ │ ├tenderness 168 | │ ├compassion┤ 169 | │ │ └mercifulness┐ 170 | │ │ └forgiveness 171 | │ │ ┌hopelessness 172 | │ │ ├resignation┐ 173 | │ │ │ └defeatism 174 | │ ├despair┤ 175 | │ │ ├pessimism┐ 176 | │ │ │ └cynicism 177 | │ │ └discouragement┐ 178 | │ │ └despair-intimidation 179 | │ │ ┌conscience 180 | │ │ ├self-disgust 181 | │ ├shame┤ 182 | │ │ │ ┌self-consciousness 183 | │ │ │ ├shamefacedness 184 | │ │ │ ├chagrin 185 | │ │ └embarrassment┤ 186 | │ │ ├discomfiture 187 | │ │ ├abashment 188 | │ │ └confusion 189 | │ │ ┌discomfiture 190 | │ │ ├distress 191 | │ │ ├negative-concern 192 | │ │ ├anxiousness 193 | │ │ ├insecurity 194 | │ │ ├edginess 195 | │ │ ├sinking 196 | │ │ ├scruple 197 | │ ├anxiety┤ 198 | │ │ │ ┌stewing 199 | │ │ │ ├tumult 200 | │ │ ├negative-agitation┤ 201 | │ │ │ └fidget┐ 202 | │ │ │ └impatience 203 | │ │ ├solicitude 204 | │ │ ├anxiousness 205 | │ │ ├angst 206 | │ │ └jitteriness 207 | │ │ ┌alarm 208 | │ │ ├creeps 209 | │ │ ├horror 210 | │ │ ├hysteria 211 | │ │ ├panic 212 | │ │ ├scare 213 | │ │ ├stage-fright 214 | │ │ ├fear-intimidation 215 | │ │ ├negative-unconcern┐ 216 | │ │ │ └heartlessness┐ 217 | │ │ │ └cruelty 218 | │ ├negative-fear┤ 219 | │ │ │ ┌trepidation 220 | │ │ │ ├negative-suspense 221 | │ │ │ ├chill 222 | │ │ ├apprehension┤ 223 | │ │ │ │ ┌shadow 224 | │ │ │ └foreboding┤ 225 | │ │ │ └presage 226 | │ │ │ ┌shyness 227 | │ │ └timidity┤ 228 | │ │ │ ┌hesitance 229 | │ │ └diffidence┤ 230 | │ │ └unassertiveness 231 | └negative-emotion┤ 232 | │ ┌disinclination 233 | │ ├unfriendliness 234 | │ ├antipathy 235 | │ ├disapproval 236 | │ ├contempt 237 | │ ┌dislike┤ 238 | │ │ │ ┌repugnance 239 | │ │ ├disgust┤ 240 | │ │ │ └nausea 241 | │ │ └alienation┐ 242 | │ │ └isolation 243 | ├general-dislike┤ 244 | │ │ ┌abhorrence 245 | │ │ ├misanthropy 246 | │ │ ├misogamy 247 | │ │ ├misogyny 248 | │ │ ├misology 249 | │ │ ├misopedia 250 | │ │ ├murderousness 251 | │ │ ├despisal 252 | │ │ ├misoneism┐ 253 | │ │ │ └misocainea 254 | │ │ │ ┌maleficence 255 | │ │ ├malevolence┤ 256 | │ │ │ ├vindictiveness 257 | │ │ │ └malice 258 | │ ├hate┤ 259 | │ │ │ ┌animosity 260 | │ │ │ ├class-feeling 261 | │ │ │ ├antagonism 262 | │ │ │ ├aggression 263 | │ │ │ ├belligerence┐ 264 | │ │ │ │ └warpath 265 | │ │ └hostility┤ 266 | │ │ │ ┌heartburning 267 | │ │ │ ├sulkiness 268 | │ │ │ ├grudge 269 | │ │ └resentment┤ 270 | │ │ │ ┌covetousness 271 | │ │ └envy┤ 272 | │ │ └jealousy 273 | │ │ ┌infuriation 274 | │ │ ├umbrage 275 | │ │ ├huffiness 276 | │ │ ├dander 277 | │ │ ├indignation┐ 278 | │ │ │ └dudgeon 279 | │ │ │ ┌wrath 280 | │ │ ├fury┤ 281 | │ │ │ └lividity 282 | │ └anger┤ 283 | │ │ ┌pique 284 | │ │ ├frustration 285 | │ ├annoyance┤ 286 | │ │ ├displeasure 287 | │ │ ├harassment 288 | │ │ └aggravation 289 | │ │ ┌irascibility 290 | │ └bad-temper┤ 291 | │ └fit 292 | │ ┌dolefulness 293 | │ ├misery 294 | │ ├forlornness 295 | │ ├weepiness 296 | │ ├downheartedness 297 | │ ├cheerlessness┐ 298 | │ │ └joylessness 299 | │ │ ┌gloom 300 | │ ├melancholy┤ 301 | │ │ ├world-weariness 302 | │ │ └heavyheartedness 303 | └sadness┤ 304 | │ ┌attrition 305 | │ ┌regret-sorrow┤ 306 | │ │ │ ┌guilt 307 | │ │ └compunction┤ 308 | │ │ └repentance 309 | ├sorrow┤ 310 | │ │ ┌self-pity 311 | │ │ ├grief┐ 312 | │ │ │ └dolor 313 | │ └lost-sorrow┤ 314 | │ │ ┌woe 315 | │ └mournfulness┤ 316 | │ └plaintiveness 317 | │ ┌demoralization 318 | │ ├helplessness 319 | │ ├dysphoria 320 | └depression┤ 321 | ├oppression┐ 322 | │ └weight 323 | └despondency┐ 324 | └blue-devils 325 | ``` 326 | *(printed using [pptree](https://github.com/clemtoy/pptree))* 327 | 328 | The WNAffect python package allows to find the emotion of a given word and to navigate in the tree. 329 | This package contains two classes: `WNAffect` and `Emotion`. These two classes are described below. 330 | 331 | ## Documentation 332 | ### WNAffect 333 | 334 | The WNAffect object allows to load WordNet 1.6 and Word Domains 3.2 resources. 335 | 336 | #### WNAffect(*wordnet16_dir*, *wn_domains_dir*) 337 | This is the constructor of a WNAffect object. It requires two arguments: 338 | - `wordnet16_dir` the wordnet-1.6 folder 339 | - `wn_domains_dir` the wn-domains-3.2 folder 340 | 341 | Example: 342 | ```python 343 | wna = WNAffect('wordnet-1.6/', 'wn-domains-3.2/') 344 | ``` 345 | 346 | #### get_emotion(*word*, *pos*) 347 | This function returns the emotion of a given word. 348 | - `word` the word 349 | - `pos` the part-of-speech tag of the word. You can get the part-of-speech tag using a third party library such as NLTK. The tag should be one of the [Pen Treebank tag set](https://www.ling.upenn.edu/courses/Fall_2003/ling001/penn_treebank_pos.html). 350 | - Returns an Emotion object 351 | ```python 352 | emo = wna.get_emotion('angry', 'JJ') 353 | ``` 354 | 355 | #### get_emotion_synset(*offset*) 356 | If you use WordNet in your own project, you can directly get the emotion of a synset using this function. 357 | - `offset` WordNet offset of the [synset](http://www.nltk.org/howto/wordnet.html#synsets) 358 | - Returns: an Emotion object 359 | 360 | Example: 361 | ```python 362 | emo = wna.get_emotion_synset(5574157) 363 | ``` 364 | 365 | ### Emotion 366 | 367 | The Emotion object is a node in the tree of emotions. 368 | The leafs are emotions and the other nodes are categories of emotions. 369 | 370 | - `name` the name of the Emotion 371 | - `parent` the parent Emotion in the tree 372 | - `level` the level of the node in the tree 373 | - `children` the array of children Emotion nodes 374 | 375 | The Emotion class also contains a static dict `Emotion.emotions` containing the emotion names as keys and the emotion objects as values. 376 | 377 | Notice that Emotion implements `__str__`. Indeed, `print(emo)` is equivalent to `print(emo.name)`. 378 | 379 | #### get_level(*level*) 380 | Searches in the parent nodes the emotion at the given level. 381 | - `level` the level to look for 382 | - Returns the Emotion object at the given level 383 | 384 | Example: 385 | 386 | ```python 387 | parent = emo.get_level(emo.level - 1) 388 | ``` 389 | 390 | #### nb_children() 391 | Count the number of all the children from the current Emotion to the leaves. 392 | - Returns the number 393 | 394 | Example: 395 | 396 | ```python 397 | n = emo.nb_children() 398 | ``` 399 | 400 | #### Emotion.printTree(*emotion=None*) 401 | *You should instantiate WNAffect before calling this static function.* 402 | 403 | Prints the tree of emotions from the given node to the leaves. 404 | - `emotion`: the emotion to print from. If not provided, the method prints the whole tree. 405 | 406 | Example: 407 | 408 | ```python 409 | Emotion.printTree(Emotion.emotions["annoyance"]) 410 | ``` 411 | Output: 412 | ``` 413 | ┌pique 414 | ├frustration 415 | annoyance┤ 416 | ├displeasure 417 | ├harassment 418 | └aggravation 419 | ``` 420 | *(printed using [pptree](https://github.com/clemtoy/pptree))* 421 | 422 | ## Example 423 | 424 | To import WNAffect package: 425 | 426 | ```python 427 | >>> from wnaffect import WNAffect 428 | >>> from emotion import Emotion # if needed 429 | ``` 430 | 431 | The first step is to instantiate a WNAffect object to load the resources 432 | ```python 433 | >>> wna = WNAffect('wordnet-1.6/', 'wn-domains-3.2/') 434 | ``` 435 | 436 | Then, you can get the emotion of a word using the get_emotion function. 437 | This function requires the part-of-speech tag. 438 | You can get the part-of-speech tag using a third party library such as NLTK. 439 | ```python 440 | >>> emo = wna.get_emotion('angry', 'JJ') 441 | >>> print(emo) 442 | anger 443 | ``` 444 | 445 | What is the parent emotion? 446 | ```python 447 | >>> parent = emo.get_level(emo.level - 1) 448 | >>> print(parent) 449 | general-dislike 450 | ``` 451 | 452 | And the root emotion? 453 | ```python 454 | >>> root = emo.get_level(0) 455 | >>> print(root) 456 | root 457 | ``` 458 | 459 | Let's print all the parent emotions: 460 | ```python 461 | >>> print(' -> '.join([emo.get_level(i).name for i in range(emo.level + 1)])) 462 | root -> mental-state -> affective-state -> emotion -> negative-emotion -> general-dislike -> anger 463 | ``` 464 | 465 | How many children has the annoyance emotion? 466 | ```python 467 | >>> annoyance = Emotion.emotions["annoyance"] 468 | >>> annoyance.nb_children() 469 | 5 470 | ``` 471 | 472 | Let's print them: 473 | ```python 474 | >>> Emotion.printTree(annoyance) # WNAffect should have been instantiated previously 475 | ┌pique 476 | ├frustration 477 | annoyance┤ 478 | ├displeasure 479 | ├harassment 480 | └aggravation 481 | ``` 482 | -------------------------------------------------------------------------------- /emotion.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | """ 3 | Clement Michard (c) 2015 4 | """ 5 | 6 | class Emotion: 7 | """Defines an emotion.""" 8 | 9 | emotions = {} # name to emotion (str -> Emotion) 10 | 11 | def __init__(self, name, parent_name=None): 12 | """Initializes an Emotion object. 13 | name -- name of the emotion (str) 14 | parent_name -- name of the parent emotion (str) 15 | """ 16 | 17 | self.name = name 18 | self.parent = None 19 | self.level = 0 20 | self.children = [] 21 | 22 | if parent_name: 23 | self.parent = Emotion.emotions[parent_name] if parent_name else None 24 | self.parent.children.append(self) 25 | self.level = self.parent.level + 1 26 | 27 | 28 | def get_level(self, level): 29 | """Returns the parent of self at the given level. 30 | level -- level in the hierarchy (int) 31 | """ 32 | 33 | em = self 34 | while em.level > level and em.level >= 0: 35 | em = em.parent 36 | return em 37 | 38 | 39 | def __str__(self): 40 | """Returns the emotion string formatted.""" 41 | 42 | return self.name 43 | 44 | 45 | def nb_children(self): 46 | """Returns the number of children of the emotion.""" 47 | 48 | return sum(child.nb_children() for child in self.children) + 1 49 | 50 | 51 | @staticmethod 52 | def printTree(emotion=None, indent="", last='updown'): 53 | """Prints the hierarchy of emotions. 54 | emotion -- root emotion (Emotion) 55 | """ 56 | 57 | if not emotion: 58 | emotion = Emotion.emotions["root"] 59 | 60 | size_branch = {child: child.nb_children() for child in emotion.children} 61 | leaves = sorted(emotion.children, key=lambda emotion: emotion.nb_children()) 62 | up, down = [], [] 63 | if leaves: 64 | while sum(size_branch[e] for e in down) < sum(size_branch[e] for e in leaves): 65 | down.append(leaves.pop()) 66 | up = leaves 67 | 68 | for leaf in up: 69 | next_last = 'up' if up.index(leaf) is 0 else '' 70 | next_indent = '{0}{1}{2}'.format(indent, ' ' if 'up' in last else '│', " " * len(emotion.name)) 71 | Emotion.printTree(leaf, indent=next_indent, last=next_last) 72 | if last == 'up': 73 | start_shape = '┌' 74 | elif last == 'down': 75 | start_shape = '└' 76 | elif last == 'updown': 77 | start_shape = ' ' 78 | else: 79 | start_shape = '├' 80 | if up: 81 | end_shape = '┤' 82 | elif down: 83 | end_shape = '┐' 84 | else: 85 | end_shape = '' 86 | print('{0}{1}{2}{3}'.format(indent, start_shape, emotion.name, end_shape)) 87 | for leaf in down: 88 | next_last = 'down' if down.index(leaf) is len(down) - 1 else '' 89 | next_indent = '{0}{1}{2}'.format(indent, ' ' if 'down' in last else '│', " " * len(emotion.name)) 90 | Emotion.printTree(leaf, indent=next_indent, last=next_last) 91 | -------------------------------------------------------------------------------- /wnaffect.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | """ 3 | Clement Michard (c) 2015 4 | """ 5 | 6 | import os 7 | import sys 8 | import nltk 9 | from WNAffect.emotion import Emotion 10 | from nltk.corpus import WordNetCorpusReader 11 | import xml.etree.ElementTree as ET 12 | 13 | class WNAffect: 14 | """WordNet-Affect ressource.""" 15 | 16 | def __init__(self, wordnet16_dir, wn_domains_dir): 17 | """Initializes the WordNet-Affect object.""" 18 | 19 | cwd = os.getcwd() 20 | nltk.data.path.append(cwd) 21 | wn16_path = "{0}/dict".format(wordnet16_dir) 22 | self.wn16 = WordNetCorpusReader(os.path.abspath("{0}/{1}".format(cwd, wn16_path)), nltk.data.find(wn16_path)) 23 | self.flat_pos = {'NN':'NN', 'NNS':'NN', 'JJ':'JJ', 'JJR':'JJ', 'JJS':'JJ', 'RB':'RB', 'RBR':'RB', 'RBS':'RB', 'VB':'VB', 'VBD':'VB', 'VGB':'VB', 'VBN':'VB', 'VBP':'VB', 'VBZ':'VB'} 24 | self.wn_pos = {'NN':self.wn16.NOUN, 'JJ':self.wn16.ADJ, 'VB':self.wn16.VERB, 'RB':self.wn16.ADV} 25 | self._load_emotions(wn_domains_dir) 26 | self.synsets = self._load_synsets(wn_domains_dir) 27 | 28 | 29 | 30 | def _load_synsets(self, wn_domains_dir): 31 | """Returns a dictionary POS tag -> synset offset -> emotion (str -> int -> str).""" 32 | 33 | tree = ET.parse("{0}/wn-affect-1.1/a-synsets.xml".format(wn_domains_dir)) 34 | root = tree.getroot() 35 | pos_map = { "noun": "NN", "adj": "JJ", "verb": "VB", "adv": "RB" } 36 | 37 | synsets = {} 38 | for pos in ["noun", "adj", "verb", "adv"]: 39 | tag = pos_map[pos] 40 | synsets[tag] = {} 41 | for elem in root.findall(".//{0}-syn-list//{0}-syn".format(pos, pos)): 42 | offset = int(elem.get("id")[2:]) 43 | if not offset: continue 44 | if elem.get("categ"): 45 | synsets[tag][offset] = Emotion.emotions[elem.get("categ")] if elem.get("categ") in Emotion.emotions else None 46 | elif elem.get("noun-id"): 47 | synsets[tag][offset] = synsets[pos_map["noun"]][int(elem.get("noun-id")[2:])] 48 | 49 | return synsets 50 | 51 | def _load_emotions(self, wn_domains_dir): 52 | """Loads the hierarchy of emotions from the WordNet-Affect xml.""" 53 | 54 | tree = ET.parse("{0}/wn-affect-1.1/a-hierarchy.xml".format(wn_domains_dir)) 55 | root = tree.getroot() 56 | for elem in root.findall("categ"): 57 | name = elem.get("name") 58 | if name == "root": 59 | Emotion.emotions["root"] = Emotion("root") 60 | else: 61 | Emotion.emotions[name] = Emotion(name, elem.get("isa")) 62 | 63 | def get_emotion(self, word, pos): 64 | """Returns the emotion of the word. 65 | word -- the word (str) 66 | pos -- part-of-speech (str) 67 | """ 68 | 69 | if pos in self.flat_pos: 70 | pos = self.flat_pos[pos] 71 | synsets = self.wn16.synsets(word, self.wn_pos[pos]) 72 | if synsets: 73 | for synset in synsets: 74 | offset = synset.offset() 75 | if offset in self.synsets[pos]: 76 | return self.synsets[pos][offset] 77 | return None 78 | 79 | def get_emotion_synset(self, offset): 80 | """Returns the emotion of the synset. 81 | offset -- synset offset (int) 82 | """ 83 | 84 | for pos in self.flat_pos.values(): 85 | if offset in self.synsets[pos]: 86 | return self.synsets[pos][offset] 87 | return None 88 | 89 | 90 | 91 | if __name__ == "__main__": 92 | wordnet16, wndomains32, word, pos = sys.argv[1:5] 93 | wna = WNAffect(wordnet16, wndomains32) 94 | print(wna.get_emotion(word, pos)) 95 | --------------------------------------------------------------------------------