# Path: newsApp/encodedEntity.py
# class EncodedEntity:
#   """
#   Represents a phonetically encoded entity
#   """
# 
#   def __init__(self, plainEntity):
#     """
#     Instantiates a new encoded entity object.
#     Requires 'plainEntity': plain text entity to encode
#     """
# 
#     self.plain = plainEntity
#     self.encoded = jellyfish.metaphone(plainEntity)
# 
# Path: newsApp/textHelper.py
# def removeNonAsciiChars(text):
#     return unicodedata.normalize('NFKD', text).encode('ascii','ignore').decode()


import itertools
import logging
import string
import math
import nltk
from retrying import retry
from .encodedEntity import EncodedEntity
from .textHelper import removeNonAsciiChars
@retry(stop_max_attempt_number=3)
def getEntitiesInternal(text):
    if not text:
        return []

    text = removeNonAsciiChars(text)

    sentences = nltk.sent_tokenize(text)
    sentences = [nltk.word_tokenize(sent) for sent in sentences]
    sentences = [nltk.pos_tag(sent) for sent in sentences]

    entities = []
    for sentence in sentences:
        extractedEntities = nltk.ne_chunk(sentence, binary=True).subtrees(
            filter = lambda x: x.label() == 'NE')
        for entity in extractedEntities:
            newEntity = ' '.join([leaf[0] for leaf in entity.leaves()])
            entities.append(newEntity)

    return list(set(entities))

def getEntities(text):
    try:
        return getEntitiesInternal(text)
    except Exception as e:
        logging.info("Could not extract entities for text: '%s'", text)
        return []

def compareEntities(entity1, entity2, doc1EntityWeights, doc2EntityWeights):
    entity1Weigth = doc1EntityWeights.get(entity1, 0.8)

# Path: newsApp/encodedEntity.py
# class EncodedEntity:
#   """
#   Represents a phonetically encoded entity
#   """
# 
#   def __init__(self, plainEntity):
#     """
#     Instantiates a new encoded entity object.
#     Requires 'plainEntity': plain text entity to encode
#     """
# 
#     self.plain = plainEntity
#     self.encoded = jellyfish.metaphone(plainEntity)
# 
# Path: newsApp/textHelper.py
# def removeNonAsciiChars(text):
#     return unicodedata.normalize('NFKD', text).encode('ascii','ignore').decode()


# Path: newsApp/textHelperNltk.py
import itertools
import logging
import string
import math
import nltk
from retrying import retry
from .encodedEntity import EncodedEntity
from .textHelper import removeNonAsciiChars

@retry(stop_max_attempt_number=3)
def getEntitiesInternal(text):
    if not text:
        return []

    text = removeNonAsciiChars(text)

    sentences = nltk.sent_tokenize(text)
    sentences = [nltk.word_tokenize(sent) for sent in sentences]
    sentences = [nltk.pos_tag(sent) for sent in sentences]

    entities = []
    for sentence in sentences:
        extractedEntities = nltk.ne_chunk(sentence, binary=True).subtrees(
            filter = lambda x: x.label() == 'NE')
        for entity in extractedEntities:
            newEntity = ' '.join([leaf[0] for leaf in entity.leaves()])
            entities.append(newEntity)

    return list(set(entities))

def getEntities(text):
    try:
        return getEntitiesInternal(text)
    except Exception as e:
        logging.info("Could not extract entities for text: '%s'", text)
        return []

def compareEntities(entity1, entity2, doc1EntityWeights, doc2EntityWeights):
    entity1Weigth = doc1EntityWeights.get(entity1, 0.8)

    entity1 = EncodedEntity(entity1)