# Path: util/aux.py
# def cosine(vector_a, vector_b):
#   """ Calculates the cosine similarity between two vectors in the same
#       n-dimensional space.
# 
#       Args:
#         vector_a: a numpy array with one vector.
#         vector_b: a numpy array with the other vector.
# 
#       Returns:
#         A float with the cosine similarity value, in range [-1, 1].
#   """
#   if (vector_a.size != vector_b.size):
#     return nan 
#   if (vector_a.size == 0):
#     return 0.0
#   v_a = vector_a.reshape(vector_a.size)
#   v_b = vector_b.reshape(vector_b.size)
#   cos = v_a.dot(v_b)
#   norms = (sqrt(v_a.dot(v_a)) * sqrt(v_b.dot(v_b)))
#   if norms == 0:
#     return 0.0
#   cos = cos / norms
#   return cos


import unittest
from numpy import array, nan, isnan
from util.aux import cosine
from methods.cf.user_based import UserBasedModel
        {'review': 'r1', 'author': 'a1', 'voter': 'v2', 'vote': 2},
        {'review': 'r1', 'author': 'a1', 'voter': 'v4', 'vote': 5},
        {'review': 'r2', 'author': 'a2', 'voter': 'v2', 'vote': 4},
        {'review': 'r2', 'author': 'a2', 'voter': 'v3', 'vote': 4},
        {'review': 'r2', 'author': 'a2', 'voter': 'v4', 'vote': 5},
        {'review': 'r3', 'author': 'a3', 'voter': 'v1', 'vote': 5},
        {'review': 'r3', 'author': 'a3', 'voter': 'v3', 'vote': 4},
        {'review': 'r3', 'author': 'a3', 'voter': 'v4', 'vote': 5},
    ]
    self.test = [
        {'review': 'r1', 'author': 'a1', 'voter': 'v3', 'vote': 3},
        {'review': 'r2', 'author': 'a1', 'voter': 'v1', 'vote': 5},
        {'review': 'r3', 'author': 'a3', 'voter': 'v2', 'vote': 1},
        {'review': 'r4', 'author': 'a2', 'voter': 'v3', 'vote': 4},
        {'review': 'r1', 'author': 'a1', 'voter': 'v5', 'vote': 4},
    ]
    self.pred = [
      # v3
      (cosine(array([0, 4, 4]), array([2, 4, 0])) * 2 + # v2 
      cosine(array([0, 4, 4]), array([5, 5, 5])) * 5) /  # v4
      (cosine(array([0, 4, 4]), array([2, 4, 0])) + 
      cosine(array([0, 4, 4]), array([5, 5, 5]))),
      # v1
      (cosine(array([4, 0, 5]), array([5, 5, 5])) * 5 + # v4
      cosine(array([4, 0, 5]), array([0, 4, 4])) * 4) / # v3
      (cosine(array([4, 0, 5]), array([5, 5, 5])) + 
      cosine(array([4, 0, 5]), array([0, 4, 4]))),  
      # v2
      (cosine(array([2, 4, 0]), array([5, 5, 5])) * 5 + # v4
      cosine(array([2, 4, 0]), array([0, 4, 4])) * 4) /  # v3

# Path: util/aux.py
# def cosine(vector_a, vector_b):
#   """ Calculates the cosine similarity between two vectors in the same
#       n-dimensional space.
# 
#       Args:
#         vector_a: a numpy array with one vector.
#         vector_b: a numpy array with the other vector.
# 
#       Returns:
#         A float with the cosine similarity value, in range [-1, 1].
#   """
#   if (vector_a.size != vector_b.size):
#     return nan 
#   if (vector_a.size == 0):
#     return 0.0
#   v_a = vector_a.reshape(vector_a.size)
#   v_b = vector_b.reshape(vector_b.size)
#   cos = v_a.dot(v_b)
#   norms = (sqrt(v_a.dot(v_a)) * sqrt(v_b.dot(v_b)))
#   if norms == 0:
#     return 0.0
#   cos = cos / norms
#   return cos


# Path: test/test_user_based.py
import unittest
from numpy import array, nan, isnan
from util.aux import cosine
from methods.cf.user_based import UserBasedModel

        {'review': 'r1', 'author': 'a1', 'voter': 'v2', 'vote': 2},
        {'review': 'r1', 'author': 'a1', 'voter': 'v4', 'vote': 5},
        {'review': 'r2', 'author': 'a2', 'voter': 'v2', 'vote': 4},
        {'review': 'r2', 'author': 'a2', 'voter': 'v3', 'vote': 4},
        {'review': 'r2', 'author': 'a2', 'voter': 'v4', 'vote': 5},
        {'review': 'r3', 'author': 'a3', 'voter': 'v1', 'vote': 5},
        {'review': 'r3', 'author': 'a3', 'voter': 'v3', 'vote': 4},
        {'review': 'r3', 'author': 'a3', 'voter': 'v4', 'vote': 5},
    ]
    self.test = [
        {'review': 'r1', 'author': 'a1', 'voter': 'v3', 'vote': 3},
        {'review': 'r2', 'author': 'a1', 'voter': 'v1', 'vote': 5},
        {'review': 'r3', 'author': 'a3', 'voter': 'v2', 'vote': 1},
        {'review': 'r4', 'author': 'a2', 'voter': 'v3', 'vote': 4},
        {'review': 'r1', 'author': 'a1', 'voter': 'v5', 'vote': 4},
    ]
    self.pred = [
      # v3
      (cosine(array([0, 4, 4]), array([2, 4, 0])) * 2 + # v2 
      cosine(array([0, 4, 4]), array([5, 5, 5])) * 5) /  # v4
      (cosine(array([0, 4, 4]), array([2, 4, 0])) + 
      cosine(array([0, 4, 4]), array([5, 5, 5]))),
      # v1
      (cosine(array([4, 0, 5]), array([5, 5, 5])) * 5 + # v4
      cosine(array([4, 0, 5]), array([0, 4, 4])) * 4) / # v3
      (cosine(array([4, 0, 5]), array([5, 5, 5])) + 
      cosine(array([4, 0, 5]), array([0, 4, 4]))),  
      # v2
      (cosine(array([2, 4, 0]), array([5, 5, 5])) * 5 + # v4
      cosine(array([2, 4, 0]), array([0, 4, 4])) * 4) /  # v3

      (cosine(array([2, 4, 0]), array([5, 5, 5])) +