| 367 | sys.path.pop(0) |
| 368 | |
| 369 | class SentiWordNet(Sentiment): |
| 370 | |
| 371 | def __init__(self, path="SentiWordNet*.txt", language="en"): |
| 372 | """ A sentiment lexicon with scores from SentiWordNet. |
| 373 | The value for each word is a tuple with values for |
| 374 | polarity (-1.0-1.0), subjectivity (0.0-1.0) and intensity (0.5-2.0). |
| 375 | """ |
| 376 | Sentiment.__init__(self, path=path, language=language) |
| 377 | |
| 378 | def load(self): |
| 379 | # Backwards compatibility: look for SentiWordNet*.txt in: |
| 380 | # given path, pattern/text/en/ or pattern/text/en/wordnet/ |
| 381 | try: f = ( |
| 382 | glob.glob(os.path.join(self.path)) + \ |
| 383 | glob.glob(os.path.join(MODULE, self.path)) + \ |
| 384 | glob.glob(os.path.join(MODULE, "..", self.path)))[0] |
| 385 | except IndexError: |
| 386 | raise ImportError, "can't find SentiWordnet data file" |
| 387 | # Map synset id: a-00193480" => (193480, JJ). |
| 388 | # Map synset id's to WordNet2 if VERSION == 2: |
| 389 | if int(float(VERSION)) == 3: |
| 390 | m = lambda id, pos: (int(id.lstrip("0")), _map32_pos2[pos]) |
| 391 | if int(float(VERSION)) == 2: |
| 392 | m = map32 |
| 393 | for s in open(f): |
| 394 | if not s.startswith(("#", "\t")): |
| 395 | pos, id, p, n, senses, gloss = s.split("\t") |
| 396 | w = senses.split() |
| 397 | k = m(id, pos) |
| 398 | v = (float(p) - float(n), |
| 399 | float(p) + float(n) |
| 400 | ) |
| 401 | # Apply the score to the first synonym in the synset. |
| 402 | # Several WordNet3 entries may point to the same WordNet2 entry. |
| 403 | if k is not None: |
| 404 | k = "%s-%s" % (pos, str(k[0]).zfill(8)) # "a-00193480" |
| 405 | if k not in self._synsets or w[0].endswith("#1"): |
| 406 | self._synsets[k] = v |
| 407 | for w in w: |
| 408 | if w.endswith("#1"): |
| 409 | dict.__setitem__(self, w[:-2].replace("_", " "), v) |
| 410 | |
| 411 | # Words are stored without diacritics, |
| 412 | # use wordnet.normalize(word). |
| 413 | def __getitem__(self, k): |
| 414 | return Sentiment.__getitem__(self, normalize(k)) |
| 415 | def get(self, k, *args, **kwargs): |
| 416 | return Sentiment.get(self, normalize(k), *args, **kwargs) |
| 417 | |
| 418 | def assessments(self, words=[], negation=True): |
| 419 | raise NotImplementedError |
| 420 | def __call__(self, s, negation=True): |
| 421 | raise NotImplementedError |
| 422 | |
| 423 | if not hasattr(Sentiment, "PLACEHOLDER"): |
| 424 | sentiwordnet = SentiWordNet() |
no outgoing calls
searching dependent graphs…