<commit_before>#!/usr/bin/env python

'''
Sisrec.py
Movies recomendation engine
'''

from typing import List  # noqa: F401
from scipy.spatial.distance import cosine
from concurrent.futures import ProcessPoolExecutor
import sys
import math
import json

# Importance of each column
DEFAULT_RATIO = {'director': 1,
                 'genres': 2,
                 'imdb_keywords': 3,
                 'lsa': 10}


def union(list_a: list, list_b: list) -> list:
    '''Return the union of two lists'''
    if list_a is None:
        list_a = [None]
    if list_b is None:
        list_b = [None]
    return list(set(list_a) | set(list_b))


def ordenar(comparacion: dict, length: int = 10) -> dict:
    '''Sort the results'''

    # Save 10 films ordered by similarity
    orden_similitud = sorted(comparacion['pels'],
                             key=lambda x: x['similitud'],
                             reverse=True)[:length]

    comparacion['pels'] = orden_similitud
    #print([x['pel']['title'] for x in comparacion['pels']])
    return comparacion


def compare(mov: dict, pel: dict, ratio: dict = DEFAULT_RATIO) -> float:
    '''Compute similarity between two movies'''

    similitudtotal = 0.0

    # Compare each element of each column which has a ratio assigned. For every
    # match the similarity increases. Basically Jaccard index of every column.
    try:
        # LSA compared separately!
        for campo_a, campo_b in [
                (x, y) for x, y
                in zip(mov.items(), pel.items()) if x[0]
                in ratio and x[0] is not 'lsa'
        ]:
            total = len(union(campo_a[1], campo_b[1]))
            if total > 0:
                coincidente = len(
                    [(x, y) for x in campo_a[1] for y in campo_b[1] if x == y]
                )
                similitudtotal += (coincidente/total) * ratio[campo_a[0]]

        # Only compare LSA if configured to do so
        if 'lsa' in ratio:
            # LSA is comapred using cosine distance
            syn_similarity = (
                1 - cosine(
                    pel['lsa'],<commit_msg>Use != instead of is not in recommender.py<commit_after>#!/usr/bin/env python

'''
Sisrec.py
Movies recomendation engine
'''

from typing import List  # noqa: F401
from scipy.spatial.distance import cosine
from concurrent.futures import ProcessPoolExecutor
import sys
import math
import json

# Importance of each column
DEFAULT_RATIO = {'director': 1,
                 'genres': 2,
                 'imdb_keywords': 3,
                 'lsa': 10}


def union(list_a: list, list_b: list) -> list:
    '''Return the union of two lists'''
    if list_a is None:
        list_a = [None]
    if list_b is None:
        list_b = [None]
    return list(set(list_a) | set(list_b))


def ordenar(comparacion: dict, length: int = 10) -> dict:
    '''Sort the results'''

    # Save 10 films ordered by similarity
    orden_similitud = sorted(comparacion['pels'],
                             key=lambda x: x['similitud'],
                             reverse=True)[:length]

    comparacion['pels'] = orden_similitud
    #print([x['pel']['title'] for x in comparacion['pels']])
    return comparacion


def compare(mov: dict, pel: dict, ratio: dict = DEFAULT_RATIO) -> float:
    '''Compute similarity between two movies'''

    similitudtotal = 0.0

    # Compare each element of each column which has a ratio assigned. For every
    # match the similarity increases. Basically Jaccard index of every column.
    try:
        # LSA compared separately!
        for campo_a, campo_b in [
                (x, y) for x, y
                in zip(mov.items(), pel.items()) if x[0]
                in ratio and x[0] != 'lsa'
        ]:
            total = len(union(campo_a[1], campo_b[1]))
            if total > 0:
                coincidente = len(
                    [(x, y) for x in campo_a[1] for y in campo_b[1] if x == y]
                )
                similitudtotal += (coincidente/total) * ratio[campo_a[0]]

        # Only compare LSA if configured to do so
        if 'lsa' in ratio:
            # LSA is comapred using cosine distance
            syn_similarity = (
                1 - cosine(
                    pel['lsa'],
{'fraction_non_alphanumeric': 0.10748877864398772, 'fraction_numerical': 0.010866997401370187, 'mean_word_length': 2.0156695156695155, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6561486', 'n_tokens_mistral': 1352, 'n_tokens_neox': 1287, 'n_words': 417}