from __future__ import unicode_literals
import unittest
from nose.tools import *  # PEP8 asserts
from nose.plugins.attrib import attr

from textblob.sentiments import PatternAnalyzer, NaiveBayesAnalyzer, DISCRETE, CONTINUOUS


class TestPatternSentiment(unittest.TestCase):

    def setUp(self):
        self.analyzer = PatternAnalyzer()

    def test_kind(self):
        assert_equal(self.analyzer.kind, CONTINUOUS)

    def test_analyze(self):
        p1 = "I feel great this morning."
        n1 = "This is a terrible car."
        p1_result = self.analyzer.analyze(p1)
        n1_result = self.analyzer.analyze(n1)
        assert_true(p1_result[0] > 0)
        assert_true(n1_result[0] < 0)
        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):

    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)
        assert_equal(p1_result.classification, p1_result[0])
        assert_equal(p1_result.p_pos, p1_result[1])
        assert_equal(p1_result.p_neg, p1_result[2])


def assert_about_equal(first, second, places=4):
    return assert_equal(round(first, places), second)

if __name__ == '__main__':
    unittest.main()

[((47, 5, 47, 17), 'nose.plugins.attrib.attr', 'attr', ({(47, 10, 47, 16): '"""slow"""'}, {}), "('slow')", False, 'from nose.plugins.attrib import attr\n'), ((68, 4, 68, 19), 'unittest.main', 'unittest.main', ({}, {}), '()', False, 'import unittest\n'), ((12, 24, 12, 41), 'textblob.sentiments.PatternAnalyzer', 'PatternAnalyzer', ({}, {}), '()', False, 'from textblob.sentiments import PatternAnalyzer, NaiveBayesAnalyzer, DISCRETE, CONTINUOUS\n'), ((42, 24, 42, 44), 'textblob.sentiments.NaiveBayesAnalyzer', 'NaiveBayesAnalyzer', ({}, {}), '()', False, 'from textblob.sentiments import PatternAnalyzer, NaiveBayesAnalyzer, DISCRETE, CONTINUOUS\n')]