# -*- coding: utf-8 -*-
"""
Unit tests for Senna
"""

from __future__ import unicode_literals
from os import environ, path, sep

import logging
import unittest

from nltk.classify import Senna
from nltk.tag import SennaTagger, SennaChunkTagger, SennaNERTagger

# Set Senna executable path for tests if it is not specified as an environment variable
if 'SENNA' in environ:
    SENNA_EXECUTABLE_PATH = path.normpath(environ['SENNA']) + sep
else:
    SENNA_EXECUTABLE_PATH = '/usr/share/senna-v3.0'

senna_is_installed = path.exists(SENNA_EXECUTABLE_PATH)

@unittest.skipUnless(senna_is_installed, "Requires Senna executable")
class TestSennaPipeline(unittest.TestCase):
    """Unittest for nltk.classify.senna"""

    def test_senna_pipeline(self):
        """Senna pipeline interface"""

        pipeline = Senna(SENNA_EXECUTABLE_PATH, ['pos', 'chk', 'ner'])
        sent = 'Dusseldorf is an international business center'.split()
        result = [(token['word'], token['chk'], token['ner'], token['pos']) for token in pipeline.tag(sent)]
        expected = [('Dusseldorf', 'B-NP', 'B-LOC', 'NNP'), ('is', 'B-VP',
            'O', 'VBZ'), ('an', 'B-NP', 'O', 'DT'), ('international', 'I-NP',
            'O', 'JJ'), ('business', 'I-NP', 'O', 'NN'), ('center', 'I-NP',
            'O', 'NN')]
        self.assertEqual(result, expected)

@unittest.skipUnless(senna_is_installed, "Requires Senna executable")
class TestSennaTagger(unittest.TestCase):
    """Unittest for nltk.tag.senna"""

    def test_senna_tagger(self):
        tagger = SennaTagger(SENNA_EXECUTABLE_PATH)
        result = tagger.tag('What is the airspeed of an unladen swallow ?'.split())
        expected = [('What', 'WP'), ('is', 'VBZ'), ('the', 'DT'), ('airspeed',
            'NN'),('of', 'IN'), ('an', 'DT'), ('unladen', 'NN'), ('swallow',
            'NN'), ('?', '.')]
        self.assertEqual(result, expected)

    def test_senna_chunk_tagger(self):
        chktagger = SennaChunkTagger(SENNA_EXECUTABLE_PATH)
        result_1 = chktagger.tag('What is the airspeed of an unladen swallow ?'.split())
        expected_1 = [('What', 'B-NP'), ('is', 'B-VP'), ('the', 'B-NP'), ('airspeed',
            'I-NP'), ('of', 'B-PP'), ('an', 'B-NP'), ('unladen', 'I-NP'), ('swallow',
            'I-NP'), ('?', 'O')]

        result_2 = list(chktagger.bio_to_chunks(result_1, chunk_type='NP'))
        expected_2 = [('What', '0'), ('the airspeed', '2-3'), ('an unladen swallow',
            '5-6-7')]
        self.assertEqual(result_1, expected_1)
        self.assertEqual(result_2, expected_2)

    def test_senna_ner_tagger(self):
        nertagger = SennaNERTagger(SENNA_EXECUTABLE_PATH)
        result_1 = nertagger.tag('Shakespeare theatre was in London .'.split())
        expected_1 = [('Shakespeare', 'B-PER'), ('theatre', 'O'), ('was', 'O'),
                ('in', 'O'), ('London', 'B-LOC'), ('.', 'O')]

        result_2 = nertagger.tag('UN headquarters are in NY , USA .'.split())
        expected_2 = [('UN', 'B-ORG'), ('headquarters', 'O'), ('are', 'O'),
        ('in', 'O'), ('NY', 'B-LOC'), (',', 'O'), ('USA', 'B-LOC'), ('.', 'O')]
        self.assertEqual(result_1, expected_1)
        self.assertEqual(result_2, expected_2)

[((21, 21, 21, 55), 'os.path.exists', 'path.exists', ({(21, 33, 21, 54): 'SENNA_EXECUTABLE_PATH'}, {}), '(SENNA_EXECUTABLE_PATH)', False, 'from os import environ, path, sep\n'), ((23, 1, 23, 69), 'unittest.skipUnless', 'unittest.skipUnless', ({(23, 21, 23, 39): 'senna_is_installed', (23, 41, 23, 68): '"""Requires Senna executable"""'}, {}), "(senna_is_installed, 'Requires Senna executable')", False, 'import unittest\n'), ((39, 1, 39, 69), 'unittest.skipUnless', 'unittest.skipUnless', ({(39, 21, 39, 39): 'senna_is_installed', (39, 41, 39, 68): '"""Requires Senna executable"""'}, {}), "(senna_is_installed, 'Requires Senna executable')", False, 'import unittest\n'), ((17, 28, 17, 59), 'os.path.normpath', 'path.normpath', ({(17, 42, 17, 58): "environ['SENNA']"}, {}), "(environ['SENNA'])", False, 'from os import environ, path, sep\n'), ((30, 19, 30, 70), 'nltk.classify.Senna', 'Senna', ({(30, 25, 30, 46): 'SENNA_EXECUTABLE_PATH', (30, 48, 30, 69): "['pos', 'chk', 'ner']"}, {}), "(SENNA_EXECUTABLE_PATH, ['pos', 'chk', 'ner'])", False, 'from nltk.classify import Senna\n'), ((44, 17, 44, 51), 'nltk.tag.SennaTagger', 'SennaTagger', ({(44, 29, 44, 50): 'SENNA_EXECUTABLE_PATH'}, {}), '(SENNA_EXECUTABLE_PATH)', False, 'from nltk.tag import SennaTagger, SennaChunkTagger, SennaNERTagger\n'), ((52, 20, 52, 59), 'nltk.tag.SennaChunkTagger', 'SennaChunkTagger', ({(52, 37, 52, 58): 'SENNA_EXECUTABLE_PATH'}, {}), '(SENNA_EXECUTABLE_PATH)', False, 'from nltk.tag import SennaTagger, SennaChunkTagger, SennaNERTagger\n'), ((65, 20, 65, 57), 'nltk.tag.SennaNERTagger', 'SennaNERTagger', ({(65, 35, 65, 56): 'SENNA_EXECUTABLE_PATH'}, {}), '(SENNA_EXECUTABLE_PATH)', False, 'from nltk.tag import SennaTagger, SennaChunkTagger, SennaNERTagger\n')]