#!/usr/bin/env python3

"""
(C) 2019 Damir Cavar, Oren Baldinger, Maanvitha Gongalla, Anurag Kumar, Murali Kammili, Boli Fang

Testing wrappers for NLTK to JSON-NLP output format.

Licensed under the Apache License 2.0, see the file LICENSE for more details.

Brought to you by the NLP-Lab.org (https://nlp-lab.org/)!
"""

from collections import OrderedDict
from unittest import TestCase

import pyjsonnlp
import pytest
from pyjsonnlp import validation

from polyglotjsonnlp import PolyglotPipeline
from . import mocks

text = "Autonomous cars from the countryside of France shift insurance liability toward manufacturers. People are afraid that they will crash."

class TestPolyglot(TestCase):

    def test_process(self):
        pyjsonnlp.__version__ = '0.1'
        actual = PolyglotPipeline.process(text, )
        expected = OrderedDict([('meta', OrderedDict(
            [('DC.conformsTo', '0.1'), ('DC.created', '2019-01-25T17:04:34'), ('DC.date', '2019-01-25T17:04:34')])), (
                                'documents', {'1': OrderedDict([('meta', OrderedDict(
                                    [('DC.conformsTo', '0.1'), ('DC.source', 'SpaCy 2.0.12'),
                                     ('DC.created', '2019-01-25T17:04:34'), ('DC.date', '2019-01-25T17:04:34'),
                                     ('DC.language', 'en')])), ('id', '1'), ('text',
                                                                             'Autonomous cars from the countryside of France shift insurance liability toward manufacturers. People are afraid that they will crash.'),
                                                                ('tokenList', {1: {'id': 1, 'text': 'Autonomous',
                                                                                   'lemma': 'autonomous', 'xpos': 'JJ',
                                                                                   'upos': 'ADJ', 'entity_iob': 'O',
                                                                                   'characterOffsetBegin': 0,
                                                                                   'characterOffsetEnd': 10,
                                                                                   'lang': 'en',
                                                                                   'features': {'Overt': 'Yes',
                                                                                                'Stop': 'No',
                                                                                                'Alpha': 'Yes',
                                                                                                'Foreign': 'No'},
                                                                                   'misc': {'SpaceAfter': 'Yes'},
                                                                                   'shape': 'Xxxxx'},
                                                                               2: {'id': 2, 'text': 'cars',
                                                                                   'lemma': 'car', 'xpos': 'NNS',
                                                                                   'upos': 'NOUN', 'entity_iob': 'O',
                                                                                   'characterOffsetBegin': 11,
                                                                                   'characterOffsetEnd': 15,
                                                                                   'lang': 'en',
                                                                                   'features': {'Overt': 'Yes',
                                                                                                'Stop': 'No',
                                                                                                'Alpha': 'Yes',
                                                                                                'Foreign': 'No'},
                                                                                   'misc': {'SpaceAfter': 'Yes'},
                                                                                   'shape': 'xxxx'},
                                                                               3: {'id': 3, 'text': 'from',
                                                                                   'lemma': 'from', 'xpos': 'IN',
                                                                                   'upos': 'ADP', 'entity_iob': 'O',
                                                                                   'characterOffsetBegin': 16,
                                                                                   'characterOffsetEnd': 20,
                                                                                   'lang': 'en',
                                                                                   'features': {'Overt': 'Yes',
                                                                                                'Stop': 'Yes',
                                                                                                'Alpha': 'Yes',
                                                                                                'Foreign': 'No'},
                                                                                   'misc': {'SpaceAfter': 'Yes'},
                                                                                   'shape': 'xxxx'},
                                                                               4: {'id': 4, 'text': 'the',
                                                                                   'lemma': 'the', 'xpos': 'DT',
                                                                                   'upos': 'DET', 'entity_iob': 'O',
                                                                                   'characterOffsetBegin': 21,
                                                                                   'characterOffsetEnd': 24,
                                                                                   'lang': 'en',
                                                                                   'features': {'Overt': 'Yes',
                                                                                                'Stop': 'Yes',
                                                                                                'Alpha': 'Yes',
                                                                                                'Foreign': 'No'},
                                                                                   'misc': {'SpaceAfter': 'Yes'},
                                                                                   'shape': 'xxx'},
                                                                               5: {'id': 5, 'text': 'countryside',
                                                                                   'lemma': 'countryside', 'xpos': 'NN',
                                                                                   'upos': 'NOUN', 'entity_iob': 'O',
                                                                                   'characterOffsetBegin': 25,
                                                                                   'characterOffsetEnd': 36,
                                                                                   'lang': 'en',
                                                                                   'features': {'Overt': 'Yes',
                                                                                                'Stop': 'No',
                                                                                                'Alpha': 'Yes',
                                                                                                'Foreign': 'No'},
                                                                                   'misc': {'SpaceAfter': 'Yes'},
                                                                                   'shape': 'xxxx'},
                                                                               6: {'id': 6, 'text': 'of', 'lemma': 'of',
                                                                                   'xpos': 'IN', 'upos': 'ADP',
                                                                                   'entity_iob': 'O',
                                                                                   'characterOffsetBegin': 37,
                                                                                   'characterOffsetEnd': 39,
                                                                                   'lang': 'en',
                                                                                   'features': {'Overt': 'Yes',
                                                                                                'Stop': 'Yes',
                                                                                                'Alpha': 'Yes',
                                                                                                'Foreign': 'No'},
                                                                                   'misc': {'SpaceAfter': 'Yes'},
                                                                                   'shape': 'xx'},
                                                                               7: {'id': 7, 'text': 'France',
                                                                                   'lemma': 'france', 'xpos': 'NNP',
                                                                                   'upos': 'PROPN', 'entity_iob': 'B',
                                                                                   'characterOffsetBegin': 40,
                                                                                   'characterOffsetEnd': 46,
                                                                                   'lang': 'en',
                                                                                   'features': {'Overt': 'Yes',
                                                                                                'Stop': 'No',
                                                                                                'Alpha': 'Yes',
                                                                                                'Foreign': 'No'},
                                                                                   'misc': {'SpaceAfter': 'Yes'},
                                                                                   'shape': 'Xxxxx', 'entity': 'GPE'},
                                                                               8: {'id': 8, 'text': 'shift',
                                                                                   'lemma': 'shift', 'xpos': 'NN',
                                                                                   'upos': 'NOUN', 'entity_iob': 'O',
                                                                                   'characterOffsetBegin': 47,
                                                                                   'characterOffsetEnd': 52,
                                                                                   'lang': 'en',
                                                                                   'features': {'Overt': 'Yes',
                                                                                                'Stop': 'No',
                                                                                                'Alpha': 'Yes',
                                                                                                'Foreign': 'No'},
                                                                                   'misc': {'SpaceAfter': 'Yes'},
                                                                                   'shape': 'xxxx'},
                                                                               9: {'id': 9, 'text': 'insurance',
                                                                                   'lemma': 'insurance', 'xpos': 'NN',
                                                                                   'upos': 'NOUN', 'entity_iob': 'O',
                                                                                   'characterOffsetBegin': 53,
                                                                                   'characterOffsetEnd': 62,
                                                                                   'lang': 'en',
                                                                                   'features': {'Overt': 'Yes',
                                                                                                'Stop': 'No',
                                                                                                'Alpha': 'Yes',
                                                                                                'Foreign': 'No'},
                                                                                   'misc': {'SpaceAfter': 'Yes'},
                                                                                   'shape': 'xxxx'},
                                                                               10: {'id': 10, 'text': 'liability',
                                                                                    'lemma': 'liability', 'xpos': 'NN',
                                                                                    'upos': 'NOUN', 'entity_iob': 'O',
                                                                                    'characterOffsetBegin': 63,
                                                                                    'characterOffsetEnd': 72,
                                                                                    'lang': 'en',
                                                                                    'features': {'Overt': 'Yes',
                                                                                                 'Stop': 'No',
                                                                                                 'Alpha': 'Yes',
                                                                                                 'Foreign': 'No'},
                                                                                    'misc': {'SpaceAfter': 'Yes'},
                                                                                    'shape': 'xxxx'},
                                                                               11: {'id': 11, 'text': 'toward',
                                                                                    'lemma': 'toward', 'xpos': 'IN',
                                                                                    'upos': 'ADP', 'entity_iob': 'O',
                                                                                    'characterOffsetBegin': 73,
                                                                                    'characterOffsetEnd': 79,
                                                                                    'lang': 'en',
                                                                                    'features': {'Overt': 'Yes',
                                                                                                 'Stop': 'Yes',
                                                                                                 'Alpha': 'Yes',
                                                                                                 'Foreign': 'No'},
                                                                                    'misc': {'SpaceAfter': 'Yes'},
                                                                                    'shape': 'xxxx'},
                                                                               12: {'id': 12, 'text': 'manufacturers',
                                                                                    'lemma': 'manufacturer',
                                                                                    'xpos': 'NNS', 'upos': 'NOUN',
                                                                                    'entity_iob': 'O',
                                                                                    'characterOffsetBegin': 80,
                                                                                    'characterOffsetEnd': 93,
                                                                                    'lang': 'en',
                                                                                    'features': {'Overt': 'Yes',
                                                                                                 'Stop': 'No',
                                                                                                 'Alpha': 'Yes',
                                                                                                 'Foreign': 'No'},
                                                                                    'misc': {'SpaceAfter': 'No'},
                                                                                    'shape': 'xxxx'},
                                                                               13: {'id': 13, 'text': '.', 'lemma': '.',
                                                                                    'xpos': '.', 'upos': 'PUNCT',
                                                                                    'entity_iob': 'O',
                                                                                    'characterOffsetBegin': 93,
                                                                                    'characterOffsetEnd': 94,
                                                                                    'lang': 'en',
                                                                                    'features': {'Overt': 'Yes',
                                                                                                 'Stop': 'No',
                                                                                                 'Alpha': 'No',
                                                                                                 'Foreign': 'No'},
                                                                                    'misc': {'SpaceAfter': 'Yes'}},
                                                                               14: {'id': 14, 'text': 'People',
                                                                                    'lemma': 'people', 'xpos': 'NNS',
                                                                                    'upos': 'NOUN', 'entity_iob': 'O',
                                                                                    'characterOffsetBegin': 95,
                                                                                    'characterOffsetEnd': 101,
                                                                                    'lang': 'en',
                                                                                    'features': {'Overt': 'Yes',
                                                                                                 'Stop': 'No',
                                                                                                 'Alpha': 'Yes',
                                                                                                 'Foreign': 'No'},
                                                                                    'misc': {'SpaceAfter': 'Yes'},
                                                                                    'shape': 'Xxxxx'},
                                                                               15: {'id': 15, 'text': 'are',
                                                                                    'lemma': 'be', 'xpos': 'VBP',
                                                                                    'upos': 'VERB', 'entity_iob': 'O',
                                                                                    'characterOffsetBegin': 102,
                                                                                    'characterOffsetEnd': 105,
                                                                                    'lang': 'en',
                                                                                    'features': {'Overt': 'Yes',
                                                                                                 'Stop': 'Yes',
                                                                                                 'Alpha': 'Yes',
                                                                                                 'Foreign': 'No'},
                                                                                    'misc': {'SpaceAfter': 'Yes'},
                                                                                    'shape': 'xxx'},
                                                                               16: {'id': 16, 'text': 'afraid',
                                                                                    'lemma': 'afraid', 'xpos': 'JJ',
                                                                                    'upos': 'ADJ', 'entity_iob': 'O',
                                                                                    'characterOffsetBegin': 106,
                                                                                    'characterOffsetEnd': 112,
                                                                                    'lang': 'en',
                                                                                    'features': {'Overt': 'Yes',
                                                                                                 'Stop': 'No',
                                                                                                 'Alpha': 'Yes',
                                                                                                 'Foreign': 'No'},
                                                                                    'misc': {'SpaceAfter': 'Yes'},
                                                                                    'shape': 'xxxx'},
                                                                               17: {'id': 17, 'text': 'that',
                                                                                    'lemma': 'that', 'xpos': 'IN',
                                                                                    'upos': 'ADP', 'entity_iob': 'O',
                                                                                    'characterOffsetBegin': 113,
                                                                                    'characterOffsetEnd': 117,
                                                                                    'lang': 'en',
                                                                                    'features': {'Overt': 'Yes',
                                                                                                 'Stop': 'Yes',
                                                                                                 'Alpha': 'Yes',
                                                                                                 'Foreign': 'No'},
                                                                                    'misc': {'SpaceAfter': 'Yes'},
                                                                                    'shape': 'xxxx'},
                                                                               18: {'id': 18, 'text': 'they',
                                                                                    'lemma': '-PRON-', 'xpos': 'PRP',
                                                                                    'upos': 'PRON', 'entity_iob': 'O',
                                                                                    'characterOffsetBegin': 118,
                                                                                    'characterOffsetEnd': 122,
                                                                                    'lang': 'en',
                                                                                    'features': {'Overt': 'Yes',
                                                                                                 'Stop': 'Yes',
                                                                                                 'Alpha': 'Yes',
                                                                                                 'Foreign': 'No'},
                                                                                    'misc': {'SpaceAfter': 'Yes'},
                                                                                    'shape': 'xxxx'},
                                                                               19: {'id': 19, 'text': 'will',
                                                                                    'lemma': 'will', 'xpos': 'MD',
                                                                                    'upos': 'VERB', 'entity_iob': 'O',
                                                                                    'characterOffsetBegin': 123,
                                                                                    'characterOffsetEnd': 127,
                                                                                    'lang': 'en',
                                                                                    'features': {'Overt': 'Yes',
                                                                                                 'Stop': 'Yes',
                                                                                                 'Alpha': 'Yes',
                                                                                                 'Foreign': 'No'},
                                                                                    'misc': {'SpaceAfter': 'Yes'},
                                                                                    'shape': 'xxxx'},
                                                                               20: {'id': 20, 'text': 'crash',
                                                                                    'lemma': 'crash', 'xpos': 'VB',
                                                                                    'upos': 'VERB', 'entity_iob': 'O',
                                                                                    'characterOffsetBegin': 128,
                                                                                    'characterOffsetEnd': 133,
                                                                                    'lang': 'en',
                                                                                    'features': {'Overt': 'Yes',
                                                                                                 'Stop': 'No',
                                                                                                 'Alpha': 'Yes',
                                                                                                 'Foreign': 'No'},
                                                                                    'misc': {'SpaceAfter': 'No'},
                                                                                    'shape': 'xxxx'},
                                                                               21: {'id': 21, 'text': '.', 'lemma': '.',
                                                                                    'xpos': '.', 'upos': 'PUNCT',
                                                                                    'entity_iob': 'O',
                                                                                    'characterOffsetBegin': 133,
                                                                                    'characterOffsetEnd': 134,
                                                                                    'lang': 'en',
                                                                                    'features': {'Overt': 'Yes',
                                                                                                 'Stop': 'No',
                                                                                                 'Alpha': 'No',
                                                                                                 'Foreign': 'No'},
                                                                                    'misc': {'SpaceAfter': 'No'}}}), (
                                                                'sentences', {
                                                                    '0': {'id': '0', 'tokenFrom': 1, 'tokenTo': 14,
                                                                          'tokens': [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11,
                                                                                     12, 13]},
                                                                    '1': {'id': '1', 'tokenFrom': 14, 'tokenTo': 22,
                                                                          'tokens': [14, 15, 16, 17, 18, 19, 20, 21]}}),
                                                                ('dependencies', [{'style': 'universal', 'arcs': {1: [
                                                                    {'sentenceId': '0', 'label': 'amod', 'governor': 2,
                                                                     'dependent': 1}], 2: [
                                                                    {'sentenceId': '0', 'label': 'nsubj', 'governor': 8,
                                                                     'dependent': 2}], 3: [
                                                                    {'sentenceId': '0', 'label': 'prep', 'governor': 2,
                                                                     'dependent': 3}], 4: [
                                                                    {'sentenceId': '0', 'label': 'det', 'governor': 5,
                                                                     'dependent': 4}], 5: [
                                                                    {'sentenceId': '0', 'label': 'pobj', 'governor': 3,
                                                                     'dependent': 5}], 6: [
                                                                    {'sentenceId': '0', 'label': 'prep', 'governor': 5,
                                                                     'dependent': 6}], 7: [
                                                                    {'sentenceId': '0', 'label': 'pobj', 'governor': 6,
                                                                     'dependent': 7}], 8: [
                                                                    {'sentenceId': '0', 'label': 'root', 'governor': 0,
                                                                     'dependent': 8}], 9: [
                                                                    {'sentenceId': '0', 'label': 'compound',
                                                                     'governor': 10, 'dependent': 9}], 10: [
                                                                    {'sentenceId': '0', 'label': 'dobj', 'governor': 8,
                                                                     'dependent': 10}], 11: [
                                                                    {'sentenceId': '0', 'label': 'prep', 'governor': 8,
                                                                     'dependent': 11}], 12: [
                                                                    {'sentenceId': '0', 'label': 'pobj', 'governor': 11,
                                                                     'dependent': 12}], 13: [
                                                                    {'sentenceId': '0', 'label': 'punct', 'governor': 8,
                                                                     'dependent': 13}], 14: [
                                                                    {'sentenceId': '1', 'label': 'nsubj',
                                                                     'governor': 15, 'dependent': 14}], 15: [
                                                                    {'sentenceId': '1', 'label': 'root', 'governor': 0,
                                                                     'dependent': 15}], 16: [
                                                                    {'sentenceId': '1', 'label': 'acomp',
                                                                     'governor': 15, 'dependent': 16}], 17: [
                                                                    {'sentenceId': '1', 'label': 'mark', 'governor': 20,
                                                                     'dependent': 17}], 18: [
                                                                    {'sentenceId': '1', 'label': 'nsubj',
                                                                     'governor': 20, 'dependent': 18}], 19: [
                                                                    {'sentenceId': '1', 'label': 'aux', 'governor': 20,
                                                                     'dependent': 19}], 20: [
                                                                    {'sentenceId': '1', 'label': 'ccomp',
                                                                     'governor': 16, 'dependent': 20}], 21: [
                                                                    {'sentenceId': '1', 'label': 'punct',
                                                                     'governor': 15, 'dependent': 21}]}}]), (
                                                                'expressions', [
                                                                    {'type': 'NP', 'head': 2, 'dependency': 'nsubj',
                                                                     'tokens': [1, 2, 3]},
                                                                    {'type': 'NP', 'head': 5, 'dependency': 'pobj',
                                                                     'tokens': [4, 5, 6]},
                                                                    {'type': 'NP', 'head': 10, 'dependency': 'dobj',
                                                                     'tokens': [9, 10]}])])})])
        #assert actual == expected, actual
        assert isinstance(actual, OrderedDict)

    # def test_model_not_found(self):
    #     with pytest.raises(ModuleNotFoundError):
    #         get_model('martian_core', False, False)


    def test_validation(self):
        assert validation.is_valid(PolyglotPipeline.process(text, ))
