Skip to content

Commit

Permalink
Merge pull request #170 from jeffakolb/pattern-sentiment-assessments
Browse files Browse the repository at this point in the history
Expose assessments from pattern analyzer
  • Loading branch information
sloria authored Dec 2, 2017
2 parents 7763b31 + 7b1ffb4 commit ef26d64
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 6 deletions.
1 change: 1 addition & 0 deletions AUTHORS.rst
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,4 @@ Contributors (chronological)
- `@jcalbert <https://github.com/jcalbert>`_
- Tyler James Harden `@tylerjharden <https://github.com/tylerjharden>`_
- `@pavelmalai <https://github.com/pavelmalai>`_
- Jeff Kolb `@jeffakolb <https://github.com/jeffakolb>`_
11 changes: 11 additions & 0 deletions tests/test_sentiments.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,17 @@ def test_analyze(self):
assert_equal(p1_result.polarity, p1_result[0])
assert_equal(p1_result.subjectivity, p1_result[1])

def test_analyze_assessments(self):
p1 = "I feel great this morning."
n1 = "This is a terrible car."
p1_result = self.analyzer.analyze(p1,keep_assessments=True)
n1_result = self.analyzer.analyze(n1,keep_assessments=True)
p1_assessment = p1_result.assessments[0]
n1_assessment = n1_result.assessments[0]
assert_true(p1_assessment[1] > 0)
assert_true(n1_assessment[1] < 0)
assert_equal(p1_result.polarity, p1_assessment[1])
assert_equal(p1_result.subjectivity, p1_assessment[2])

class TestNaiveBayesAnalyzer(unittest.TestCase):

Expand Down
13 changes: 13 additions & 0 deletions textblob/blob.py
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,19 @@ def sentiment(self):
"""
return self.analyzer.analyze(self.raw)

@cached_property
def sentiment_assessments(self):
"""Return a tuple of form (polarity, subjectivity, assessments ) where
polarity is a float within the range [-1.0, 1.0], subjectivity is a
float within the range [0.0, 1.0] where 0.0 is very objective and 1.0
is very subjective, and assessments is a list of polarity and
subjectivity scores for the assessed tokens.
:rtype: namedtuple of the form ``Sentiment(polarity, subjectivity,
assessments)``
"""
return self.analyzer.analyze(self.raw,keep_assessments=True)

@cached_property
def polarity(self):
"""Return the polarity score as a float within the range [-1.0, 1.0]
Expand Down
22 changes: 16 additions & 6 deletions textblob/en/sentiments.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,28 @@ class PatternAnalyzer(BaseSentimentAnalyzer):
"""Sentiment analyzer that uses the same implementation as the
pattern library. Returns results as a named tuple of the form:
``Sentiment(polarity, subjectivity)``
``Sentiment(polarity, subjectivity, [assessments])``
where [assessments] is a list of the assessed tokens and their
polarity and subjectivity scores
"""

kind = CONTINUOUS
#: Return type declaration
RETURN_TYPE = namedtuple('Sentiment', ['polarity', 'subjectivity'])

def analyze(self, text):
def analyze(self, text, keep_assessments=False):
"""Return the sentiment as a named tuple of the form:
``Sentiment(polarity, subjectivity)``.
``Sentiment(polarity, subjectivity, [assessments])``.
"""
return self.RETURN_TYPE(*pattern_sentiment(text))
#: Return type declaration
if keep_assessments:
RETURN_TYPE = namedtuple('Sentiment', ['polarity', 'subjectivity', 'assessments'])
assessments = pattern_sentiment(text).assessments
polarity,subjectivity = pattern_sentiment(text)
return RETURN_TYPE( polarity,subjectivity,assessments )

else:
RETURN_TYPE = namedtuple('Sentiment', ['polarity', 'subjectivity'])
return RETURN_TYPE(*pattern_sentiment(text))


def _default_feature_extractor(words):
Expand Down

0 comments on commit ef26d64

Please sign in to comment.