# Path: src/backend/math_utils.py
# def hellinger_distance(sqrt_doca, sqrt_docb, axis=1):
#     """
#     Returns the (squared) Hellinger Distance between documents.
# 
#     @para doca: is expected to be a 1d array (ie., a single document),
#     @param docb: is expected to be a 2d array(ie., the rest of the documents in the
#     corpus).
# 
#     Note that this expects to be given proper probability distributions.
#     """
#     return np.sum((sqrt_doca - sqrt_docb)**2, axis=axis)
# 
# Path: src/backend/tma_utils.py
# def slugify(value):
#     """
#     Normalizes string, converts to lowercase, removes non-alpha characters,
#     and converts spaces to hyphens.
#     """
#     import unicodedata 
#     value = unicodedata.normalize('NFKD', value).encode('ascii', 'ignore')
#     value = unicode(re.sub('[^\w\s-]', '', value).strip().lower())
#     return re.sub('[-\s]+', '-', value)    


from os import path
from src.backend.math_utils import hellinger_distance
from src.backend.tma_utils import slugify
import cPickle as pickle
import math
import pdb
import numpy as np
        elif isinstance(token, Document):
            token_topic_info = self.mydb.get_top_doc_topics(token.id, num)
        elif isinstance(token, Term):
            token_topic_info = self.mydb.get_top_term_topics(token.id, num)

        topics = {}
        for info in token_topic_info:
            score = info[3]
            if score: #check for reverse pairs in topic-topic search
                if (isinstance(token, Topic) and info[2] == int(token.topic_id)) or isinstance(token, Term):
                    t = self.get_topic(info[1])
                    if t is not None:
                        topics[t] = score #TODO: topic init needs work
                else:
                    t = self.get_topic(info[2])
                    if t is not None:
                        topics[t] = score 
        return topics


    def get_top_related_terms(self, term, top_n = 10):
        """
        Get the top_n terms related to the given term
        """
        term_id = term.id
        top_term_mat = pickle.load(open(self.term_topic_obj_loc,'rb'))
        max_score = 100000000

        # compute the inverse Hellinger distance using the topic distributions for each term  (lower is better)
        term = top_term_mat[term_id,:]

# Path: src/backend/math_utils.py
# def hellinger_distance(sqrt_doca, sqrt_docb, axis=1):
#     """
#     Returns the (squared) Hellinger Distance between documents.
# 
#     @para doca: is expected to be a 1d array (ie., a single document),
#     @param docb: is expected to be a 2d array(ie., the rest of the documents in the
#     corpus).
# 
#     Note that this expects to be given proper probability distributions.
#     """
#     return np.sum((sqrt_doca - sqrt_docb)**2, axis=axis)
# 
# Path: src/backend/tma_utils.py
# def slugify(value):
#     """
#     Normalizes string, converts to lowercase, removes non-alpha characters,
#     and converts spaces to hyphens.
#     """
#     import unicodedata 
#     value = unicodedata.normalize('NFKD', value).encode('ascii', 'ignore')
#     value = unicode(re.sub('[^\w\s-]', '', value).strip().lower())
#     return re.sub('[-\s]+', '-', value)    


# Path: src/backend/relations.py
from os import path
from src.backend.math_utils import hellinger_distance
from src.backend.tma_utils import slugify
import cPickle as pickle
import math
import pdb
import numpy as np

        elif isinstance(token, Document):
            token_topic_info = self.mydb.get_top_doc_topics(token.id, num)
        elif isinstance(token, Term):
            token_topic_info = self.mydb.get_top_term_topics(token.id, num)

        topics = {}
        for info in token_topic_info:
            score = info[3]
            if score: #check for reverse pairs in topic-topic search
                if (isinstance(token, Topic) and info[2] == int(token.topic_id)) or isinstance(token, Term):
                    t = self.get_topic(info[1])
                    if t is not None:
                        topics[t] = score #TODO: topic init needs work
                else:
                    t = self.get_topic(info[2])
                    if t is not None:
                        topics[t] = score 
        return topics


    def get_top_related_terms(self, term, top_n = 10):
        """
        Get the top_n terms related to the given term
        """
        term_id = term.id
        top_term_mat = pickle.load(open(self.term_topic_obj_loc,'rb'))
        max_score = 100000000

        # compute the inverse Hellinger distance using the topic distributions for each term  (lower is better)
        term = top_term_mat[term_id,:]

        scores = hellinger_distance(term, top_term_mat)