Returns a list of (chunk, polarity, subjectivity)-tuples for the given list of words, where chunk is a list of successive words: a known word optionally preceded by a modifier ("very good") or a negation ("not good").
(self, words=[], negation=True)
| 1638 | assessments = a) |
| 1639 | |
| 1640 | def assessments(self, words=[], negation=True): |
| 1641 | """ Returns a list of (chunk, polarity, subjectivity)-tuples for the given list of words, |
| 1642 | where chunk is a list of successive words: a known word optionally |
| 1643 | preceded by a modifier ("very good") or a negation ("not good"). |
| 1644 | """ |
| 1645 | a = [] |
| 1646 | m = None # Preceding modifier (i.e., adverb or adjective). |
| 1647 | n = None # Preceding negation (e.g., "not beautiful"). |
| 1648 | for w, pos in words: |
| 1649 | # Only assess known words, preferably by part-of-speech tag. |
| 1650 | # Including unknown words (polarity 0.0 and subjectivity 0.0) lowers the average. |
| 1651 | if w in self and pos in self[w]: |
| 1652 | p, s, i = self[w][pos] |
| 1653 | # Known word not preceded by a modifier ("good"). |
| 1654 | if m is None: |
| 1655 | a.append(dict(w=[w], p=p, s=s, i=i, n=1)) |
| 1656 | # Known word preceded by a modifier ("really good"). |
| 1657 | if m is not None: |
| 1658 | a[-1]["w"].append(w) |
| 1659 | a[-1]["p"] = min(p * a[-1]["i"], 1.0) |
| 1660 | a[-1]["s"] = min(p * a[-1]["i"], 1.0) |
| 1661 | a[-1]["i"] = i |
| 1662 | # Known word preceded by a negation ("not really good"). |
| 1663 | if n is not None: |
| 1664 | a[-1]["w"].insert(0, n) |
| 1665 | a[-1]["i"] = 1.0 / a[-1]["i"] |
| 1666 | a[-1]["n"] = -1 |
| 1667 | # Known word may be a negation. |
| 1668 | # Known word may be modifying the next word (i.e., it is a known adverb). |
| 1669 | m = None |
| 1670 | n = None |
| 1671 | if pos and pos in self.modifiers or any(map(self[w].__contains__, self.modifiers)): |
| 1672 | m = (w, pos) |
| 1673 | if negation and w in self.negations: |
| 1674 | n = w |
| 1675 | else: |
| 1676 | # Unknown word may be a negation ("not good"). |
| 1677 | if negation and w in self.negations: |
| 1678 | n = w |
| 1679 | # Unknown word. Retain negation across small words ("not a good"). |
| 1680 | elif n and len(w.strip("'")) > 1: |
| 1681 | n = None |
| 1682 | # Unknown word may be a negation preceded by a modifier ("really not good"). |
| 1683 | if n is not None and m is not None and (pos in self.modifiers or self.modifier(m[0])): |
| 1684 | a[-1]["w"].append(n) |
| 1685 | a[-1]["n"] = -1 |
| 1686 | n = None |
| 1687 | # Unknown word. Retain modifier across small words ("really is a good"). |
| 1688 | elif m and len(w) > 2: |
| 1689 | m = None |
| 1690 | # Exclamation marks boost previous words. |
| 1691 | if w == "!" and len(a) > 0: |
| 1692 | for x in a[-3:]: x["p"] = min(x["p"] * 1.25, 1.0) |
| 1693 | # Emoticon (happy). |
| 1694 | if w in (":)", ":-)", ":-d", "<3", u"♥"): |
| 1695 | a.append(dict(w=[w], p=+1.0, s=1.0, i=1.0, n=1)) |
| 1696 | # Emoticon (sad). |
| 1697 | if w in (":(", ":-(", ":-s"): |