MCPcopy Index your code
hub / github.com/clips/pattern / __call__

Method __call__

pattern/text/__init__.py:1585–1638  ·  view source on GitHub ↗

Returns a (polarity, subjectivity)-tuple for the given sentence, with polarity between -1.0 and 1.0 and subjectivity between 0.0 and 1.0. The sentence can be a string, Synset, Text, Sentence, Chunk, Word, Document, Vector. An optional weight parameter can be giv

(self, s, negation=True, **kwargs)

Source from the content-addressed store, hash-verified

1583 return tuple(self._synsets.get(id, (0.0, 0.0))[:2])
1584
1585 def __call__(self, s, negation=True, **kwargs):
1586 """ Returns a (polarity, subjectivity)-tuple for the given sentence,
1587 with polarity between -1.0 and 1.0 and subjectivity between 0.0 and 1.0.
1588 The sentence can be a string, Synset, Text, Sentence, Chunk, Word, Document, Vector.
1589 An optional weight parameter can be given,
1590 as a function that takes a list of words and returns a weight.
1591 """
1592 def avg(assessments, weighted=lambda w: 1):
1593 s, n = 0, 0
1594 for words, score in assessments:
1595 w = weighted(words)
1596 s += w * score
1597 n += w
1598 return s / float(n or 1)
1599 # A pattern.en.wordnet.Synset.
1600 # Sentiment(synsets("horrible", "JJ")[0]) => (-0.6, 1.0)
1601 if hasattr(s, "gloss"):
1602 a = [(s.synonyms[0],) + self.synset(s.id, pos=s.pos)]
1603 # A synset id.
1604 # Sentiment("a-00193480") => horrible => (-0.6, 1.0) (English WordNet)
1605 # Sentiment("c_267") => verschrikkelijk => (-0.9, 1.0) (Dutch Cornetto)
1606 elif isinstance(s, basestring) and RE_SYNSET.match(s):
1607 a = [(s.synonyms[0],) + self.synset(s.id, pos=s.pos)]
1608 # A string of words.
1609 # Sentiment("a horrible movie") => (-0.6, 1.0)
1610 elif isinstance(s, basestring):
1611 a = self.assessments(((w.lower(), None) for w in " ".join(self.tokenizer(s)).split()), negation)
1612 # A pattern.en.Text.
1613 elif hasattr(s, "sentences"):
1614 a = self.assessments(((w.lemma or w.string.lower(), w.pos[:2]) for w in chain(*s)), negation)
1615 # A pattern.en.Sentence or pattern.en.Chunk.
1616 elif hasattr(s, "lemmata"):
1617 a = self.assessments(((w.lemma or w.string.lower(), w.pos[:2]) for w in s.words), negation)
1618 # A pattern.en.Word.
1619 elif hasattr(s, "lemma"):
1620 a = self.assessments(((s.lemma or s.string.lower(), s.pos[:2]),), negation)
1621 # A pattern.vector.Document.
1622 # Average score = weighted average using feature weights.
1623 elif hasattr(s, "terms"):
1624 a = self.assessments(((w, None) for w in s.terms), negation)
1625 kwargs.setdefault("weight", lambda w: s.terms[w[0]])
1626 # A dict of (word, weight)-items.
1627 elif isinstance(s, dict):
1628 a = self.assessments(((w, None) for w in s), negation)
1629 kwargs.setdefault("weight", lambda w: s[w[0]])
1630 # A list of words.
1631 elif isinstance(s, list):
1632 a = self.assessments(((w, None) for w in s), negation)
1633 else:
1634 a = []
1635 weight = kwargs.get("weight", lambda w: 1)
1636 return Score(polarity = avg(map(lambda (w, p, s): (w, p), a), weight),
1637 subjectivity = avg(map(lambda (w, p, s): (w, s), a), weight),
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,

Callers

nothing calls this directly

Calls 8

synsetMethod · 0.95
assessmentsMethod · 0.95
ScoreClass · 0.85
avgFunction · 0.70
matchMethod · 0.45
splitMethod · 0.45
setdefaultMethod · 0.45
getMethod · 0.45

Tested by

no test coverage detected