class TestNaiveBayesAnalyzer(unittest.TestCase): def setUp(self): self.analyzer = NaiveBayesAnalyzer() def test_kind(self): assert_equal(self.analyzer.kind, DISCRETE) @attr('slow') def test_analyze(self): p1 = 'I feel great this morning.' n1 = 'This is a terrible car.' p1_result = self.analyzer.analyze(p1) assert_equal(p1_result[0], 'pos') assert_equal(self.analyzer.analyze(n1)[0], 'neg') # The 2nd item should be the probability that it is positive assert_true(isinstance(p1_result[1], float)) # 3rd item is probability that it is negative assert_true(isinstance(p1_result[2], float)) assert_about_equal(p1_result[1] + p1_result[2], 1)
def __init__(self): # create custom components self.naive_bayes_analyzer = NaiveBayesAnalyzer() self.conll_extractor = ConllExtractor() self.nltk_tagger = NLTKTagger() self.perceptron_tagger = PerceptronTagger() if DEV_ENV: return # train all components (default and custom) text = 'TextBlob blobs great!' default_blob = TextBlob(text) default_blob.sentiment default_blob.noun_phrases default_blob.pos_tags custom_blob = TextBlob(text, analyzer=self.naive_bayes_analyzer, np_extractor=self.conll_extractor, pos_tagger=self.nltk_tagger) custom_blob.sentiment custom_blob.noun_phrases custom_blob.pos_tags custom2_blob = TextBlob(text, pos_tagger=self.perceptron_tagger) custom2_blob.pos_tags
def test_override_analyzer(self): b = tb.Blobber(analyzer=NaiveBayesAnalyzer()) blob = b("How now?") blob2 = b("Brown cow") assert_true(isinstance(blob.analyzer, NaiveBayesAnalyzer)) assert_true(blob.analyzer is blob2.analyzer)
def test_can_get_subjectivity_and_polarity_with_different_analyzer(self): blob = tb.TextBlob("I love this car.", analyzer=NaiveBayesAnalyzer()) pattern = PatternAnalyzer() assert_equal(blob.polarity, pattern.analyze(str(blob))[0]) assert_equal(blob.subjectivity, pattern.analyze(str(blob))[1])
def test_discrete_sentiment(self): blob = tb.TextBlob("I feel great today.", analyzer=NaiveBayesAnalyzer()) assert_equal(blob.sentiment[0], 'pos')
def test_can_use_different_sentanalyzer(self): blob = tb.TextBlob("I love this car", analyzer=NaiveBayesAnalyzer()) assert_true(isinstance(blob.analyzer, NaiveBayesAnalyzer))
def setUp(self): self.analyzer = NaiveBayesAnalyzer()