# Path: context/nlp/entities.py
# def collapse_entities(entity_list):
#     """Return unique list of entity names collapsed into shortest forms"""
#     name_list = []
# 
#     for e in entity_list:
#         name_forms = sorted(e['name_forms'], key=lambda s: len(s))
#         while name_forms:
#             name = name_forms.pop(0)
#             for i in range(len(name_forms) - 1, -1, -1):
#                 s = name_forms[i]
#                 if s.find(name) > -1:
#                     del name_forms[i]
#             if name not in name_list:
#                 name_list.append(name)
#         name_list.extend([s for s in name_forms if s not in name_list])
# 
#     return name_list
# 
# Path: context/nlp/util.py
# def quoted_terms(term_list):
#     """Return list of terms with multi-word terms quoted"""
#     return [MULTIWORD_PAT.sub(r'"\1 \2"', t) for t in term_list]


from .nlp.entities import collapse_entities
from .nlp.util import quoted_terms
    a OR b c OR d   is          (a OR b) AND (c OR d)
                    is not      a OR (b AND c) OR d

    For example, the query:

        free OR good beer OR shirt

    is interpreted as:

        (free OR good) AND (beer OR shirt)

    so it will return tweets containing:

                "free" and "beer"
        or      "free" and "shirt"
        or      "good" and "beer"
        or      "good" and "shirt"

    """
    q_entities = []
    if entities:
        q_entities = collapse_entities(
            [d for d in entities if d['score'] > 0.01])

    q_keywords = []
    if keywords:
        q_keywords = [d['keyword'] for d in keywords \
            if d['count'] > 2 and d['keyword'] not in q_entities]

    q_keywords = quoted_terms(q_keywords[:5])

# Path: context/nlp/entities.py
# def collapse_entities(entity_list):
#     """Return unique list of entity names collapsed into shortest forms"""
#     name_list = []
# 
#     for e in entity_list:
#         name_forms = sorted(e['name_forms'], key=lambda s: len(s))
#         while name_forms:
#             name = name_forms.pop(0)
#             for i in range(len(name_forms) - 1, -1, -1):
#                 s = name_forms[i]
#                 if s.find(name) > -1:
#                     del name_forms[i]
#             if name not in name_list:
#                 name_list.append(name)
#         name_list.extend([s for s in name_forms if s not in name_list])
# 
#     return name_list
# 
# Path: context/nlp/util.py
# def quoted_terms(term_list):
#     """Return list of terms with multi-word terms quoted"""
#     return [MULTIWORD_PAT.sub(r'"\1 \2"', t) for t in term_list]


# Path: context/query.py
from .nlp.entities import collapse_entities
from .nlp.util import quoted_terms

    a OR b c OR d   is          (a OR b) AND (c OR d)
                    is not      a OR (b AND c) OR d

    For example, the query:

        free OR good beer OR shirt

    is interpreted as:

        (free OR good) AND (beer OR shirt)

    so it will return tweets containing:

                "free" and "beer"
        or      "free" and "shirt"
        or      "good" and "beer"
        or      "good" and "shirt"

    """
    q_entities = []
    if entities:
        q_entities = collapse_entities(
            [d for d in entities if d['score'] > 0.01])

    q_keywords = []
    if keywords:
        q_keywords = [d['keyword'] for d in keywords \
            if d['count'] > 2 and d['keyword'] not in q_entities]

    q_keywords = quoted_terms(q_keywords[:5])

    q_entities = quoted_terms(q_entities[:5])