import math


def get_cosine(question_vector, sentence_vector):
    """ Implementation of  Cosine Similarity (https://en.wikipedia.org/wiki/Cosine_similarity).

    Formula = [SUM(1,n) of Ai * Bi] / {[Square root of (SUM(1,n) Ai^2)] * [Square root of (SUM(1,n) Bi^2)]}

    :param question_vector: The question's vector.
    :type question_vector: collections.Counter
    :param sentence_vector: The sentence's vector.
    :type sentence_vector: collections.Counter
    :return: The similarity score of question & sentence vectors(from -1 to 1)
    :rtype: float
    """

    # Create intersection ('tomi').
    intersection = set(question_vector.keys()) & set(sentence_vector.keys())

    # Create numerator ('arithmitis').
    # [SUM(1,n) of Ai * Bi]
    numerator = sum([question_vector[x] * sentence_vector[x] for x in intersection])

    # Create denominator ('paronomastis').
    question_vector_sum = sum([question_vector[x]**2 for x in question_vector.keys()])  # (SUM(1,n) of  Ai^2)
    sentence_vector_sum = sum([sentence_vector[x]**2 for x in sentence_vector.keys()])  # (SUM(1,n) of Bi^2)
    denominator = math.sqrt(question_vector_sum) * math.sqrt(sentence_vector_sum)  # [Square root of (question_vector_sum)] * [Square root of (sentence_vector_sum)]

    # Calculate similarity score.
    if not denominator:
        return 0.0
    else:
        return float(numerator) / denominator