"""
The query interface for whoami.

Client should use Whoami through the functions in this module.
"""

from .defextract import extract_assertion, extract_question
from .output import strings


def get_assertion_definitions(assertion, subject=[], lemma_pos=['n'], lowercase=True, get_rep=False):
    """Extract definitions from the given assertion.

    Arguments:
    assertion (unicode) -- an is-a definition sentence.
    subject (list of unicode strings) -- subject text or its synonyms. This
        is optional, but it helps whoami parse sentence more accurately,
        especially when the subject does not look like a noun.
    lemma_pos -- list of POS tags of words to lemmatize. Tags must by a subset
        of ['n', 'a', 'v'] for noun, adjective, and verb. Named entities are not
        lemmatized, but the definite article will be stripped.
    lowercase (bool) -- whether to force output definitions to be in lowercase.
        If false, the definitions remain truecase.
    get_rep (bool) -- whether to return the internal representations of the
        definition tree as well.

    Return:
        If get_rep is False (default):
            List of unicode strings. Each string is a definition extracted from
            assertion.
        Otherwise:
            A tuple (definitions, reps), where definition is a list of
            definition strings, and reps is the string representation of the
            internal definition tree.

    Example of what a definition tree look like:
    "The R Broadway Local is a rapid transit service in the B Division of the New York City Subway"
    --> [subject: local [def: service [mod: rapid], [comp: transit], [in: Division [comp: B], [of: Subway [comp: New York City]]]]]

    Exceptions:
    whoami.exceptions.ParseError if assertion cannot be parsed as an is-a
        definition sentence.
    """
    entry = extract_assertion(assertion, subject=subject)
    if get_rep:
        return strings(entry, lemma_pos=lemma_pos, lowercase=lowercase), \
            str(entry)
    return strings(entry, lemma_pos=lemma_pos, lowercase=lowercase)


def get_question_definitions(question, subject=[], lemma_pos=['n'], lowercase=True, get_rep=False):

    """Extract definitions from the given question.

    Arguments:
    question (unicode) -- a what-is-NP or which-NP-VP question.
    See get_assertion_definitions documentation for other arguments.

    Return:
        If get_rep is False (default):
            List of unicode strings. Each string is a definition extracted from
            the NP subject of the question.
        Otherwise:
            A tuple (definitions, reps), where definition is a list of
            definition strings, and reps is the string representation of the
            internal definition tree.
        Note that because question is not supported yet, this function always
        returns an empty list or a tuple of empty list and empty string.

    Exceptions:
    whoami.exceptions.ParseError if a NP definition cannot be extracted from
        the given question.
    """
    entry = extract_question(question)
    if get_rep:
        return strings(entry, lemma_pos=lemma_pos, lowercase=lowercase), \
            str(entry)
    return strings(entry, lemma_pos=lemma_pos, lowercase=lowercase)
