| 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). |