def test_sentiment(self): # Assert < 0 for negative adjectives and > 0 for positive adjectives. self.assertTrue(en.sentiment("wonderful")[0] > 0) self.assertTrue(en.sentiment("horrible")[0] < 0) self.assertTrue( en.sentiment(en.wordnet.synsets("horrible", pos="JJ")[0])[0] < 0) self.assertTrue( en.sentiment(en.Text(en.parse("A bad book. Really horrible.")))[0] < 0) # Assert that :) and :( are recognized. self.assertTrue(en.sentiment(":)")[0] > 0) self.assertTrue(en.sentiment(":(")[0] < 0) # Assert the accuracy of the sentiment analysis. # Given are the scores for Pang & Lee's polarity dataset v2.0: # http://www.cs.cornell.edu/people/pabo/movie-review-data/ # The baseline should increase (not decrease) when the algorithm is modified. from pattern.db import Datasheet from pattern.metrics import test reviews = [] for score, review in Datasheet.load( os.path.join(PATH, "corpora", "polarity-en-pang&lee.csv")): reviews.append((review, int(score) > 0)) A, P, R, F = test(lambda review: en.positive(review), reviews) self.assertTrue(A > 0.71) self.assertTrue(P > 0.72) self.assertTrue(R > 0.70) self.assertTrue(F > 0.71) print "pattern.en.sentiment()"
def setUp(self): # Parse sentences to test on. # Creating a Text creates Sentence, Chunk, PNP and Word. # Creating a Sentence tests Sentence.append() and # Sentence.parse_token(). self.text = "I'm eating pizza with a fork. What a tasty pizza!" self.text = en.Text(en.parse(self.text, relations=True, lemmata=True))