<commit_before>from collections import OrderedDict
from random import choice, randint, sample
from django.db import transaction

from django.db.models import Sum
from multiprocessing.dummy import Pool as ThreadPool

from apps.CONSTANTS import (
    MAX_SCORE,
    MIN_SCORE,
    MAX_THREAD,
    RECOMMENDATION_LIMIT
)
from apps.data.users.models import User
from apps.data.songs.models import Song
from apps.data.userPlaySong.models import UserPlaySong
from .models import UserAverage_Recommendations, UserAverage_Life

import copy
import logging
logger = logging.getLogger(__name__)

SONGSET_LIMIT = 0
SONGDB_IDS = None


def setSongLimitOnMap(songSetLimit):
    global SONGSET_LIMIT
    SONGSET_LIMIT = songSetLimit


def setAllSongsIdOnMemory():
    global SONGDB_IDS
    SONGDB_IDS = [song.id for song in Song.objects.all()]


def getUserAverageRecommendations(user):
    logger.info("[Start Get User Recommendation] - id: " + str(user.id))
    global SONGDB_IDS
    recommendations = {}
    userModel = []
    songs_ids_list = copy.deepcopy(SONGDB_IDS)
    userPlayed_list = UserPlaySong.objects.filter(user_id=user.id)
    for played in userPlayed_list:
        songs_ids_list.remove(played.song_id)
    try:
        userModel = sample(set(songs_ids_list), SONGSET_LIMIT)
    except ValueError:
        userModel = sample(set(songs_ids_list), len(songs_ids_list))
    for songPlayed in userPlayed_list:
        similaresSide = songPlayed.song.getSimilaries(songIDList=userModel)
        for songSimi in similaresSide:
            if songSimi.similarity == 0.0:
                continue
            if songSimi.songCompare in recommendations:
                recommendations[songSimi.songCompare].append(songSimi.similarity)
                continue
            if songSimi.songBase in recommendations:
                recommendations[songSimi.songBase].append(songSimi.similarity)
                continue
            if songSimi.songCompare is not songPlayed.song and songSimi.songCompare not in recommendations:
                recommendations.setdefault(songSimi.songCompare, [])
                recommendations[songSimi.songCompare].append(songSimi.similarity)
                continue
            if songSimi.songBase is not songPlayed.song and songSimi.songBase not in recommendations:
                recommendations.setdefault(songSimi.songBase, [])
                recommendations[songSimi.songBase].append(songSimi.similarity)
                continue
    rec = {}
    for (song, values) in recommendations.items():
        rec.setdefault(song, sum(values)/len(values))
    with transaction.atomic():
        for (song, similarity) in (OrderedDict(
            sorted(rec.items(), key=lambda t: t[1], reverse=True)
        ).items())[:RECOMMENDATION_LIMIT]:
            try:
                UserAverage_Recommendations.objects.create(
                        song_id=song.id,
                        user_id=user.id,
                        life_id=UserAverage_Life.objects.last().id,
                        similarity=similarity,
                        iLike=bool(choice([True, False])),
                        score=randint(MIN_SCORE, MAX_SCORE))
            except Exception:
                logger.info("-----Error Alert-----")
                continue


def UserAverage(songSetLimit=Song.objects.count()):
    logger.info("[Start User Average]")<commit_msg>limitando a recomendação<commit_after>from collections import OrderedDict
from random import choice, randint, sample
from django.db import transaction

from django.db.models import Sum
from multiprocessing.dummy import Pool as ThreadPool

from apps.CONSTANTS import (
    MAX_SCORE,
    MIN_SCORE,
    MAX_THREAD,
    RECOMMENDATION_LIMIT
)
from apps.data.users.models import User
from apps.data.songs.models import Song
from apps.data.userPlaySong.models import UserPlaySong
from .models import UserAverage_Recommendations, UserAverage_Life

import copy
import logging
logger = logging.getLogger(__name__)

SONGSET_LIMIT = 0
SONGDB_IDS = None


def setSongLimitOnMap(songSetLimit):
    global SONGSET_LIMIT
    SONGSET_LIMIT = songSetLimit


def setAllSongsIdOnMemory():
    global SONGDB_IDS
    SONGDB_IDS = [song.id for song in Song.objects.all()]


def getUserAverageRecommendations(user):
    logger.info("[Start Get User Recommendation] - id: " + str(user.id))
    global SONGDB_IDS
    recommendations = {}
    userModel = []
    songs_ids_list = copy.deepcopy(SONGDB_IDS)
    userPlayed_list = UserPlaySong.objects.filter(user_id=user.id)
    for played in userPlayed_list:
        songs_ids_list.remove(played.song_id)
    try:
        userModel = sample(set(songs_ids_list), SONGSET_LIMIT)
    except ValueError:
        userModel = sample(set(songs_ids_list), len(songs_ids_list))
    for songPlayed in userPlayed_list:
        similaresSide = songPlayed.song.getSimilaries(songIDList=userModel)
        for songSimi in similaresSide:
            if songSimi.similarity == 0.0:
                continue
            if songSimi.songCompare in recommendations:
                recommendations[songSimi.songCompare].append(songSimi.similarity)
                continue
            if songSimi.songBase in recommendations:
                recommendations[songSimi.songBase].append(songSimi.similarity)
                continue
            if songSimi.songCompare is not songPlayed.song and songSimi.songCompare not in recommendations:
                recommendations.setdefault(songSimi.songCompare, [])
                recommendations[songSimi.songCompare].append(songSimi.similarity)
                continue
            if songSimi.songBase is not songPlayed.song and songSimi.songBase not in recommendations:
                recommendations.setdefault(songSimi.songBase, [])
                recommendations[songSimi.songBase].append(songSimi.similarity)
                continue
    rec = {}
    for (song, values) in recommendations.items():
        rec.setdefault(song, sum(values)/len(values))
    orderedRecomendation = OrderedDict(sorted(rec.items(), key=lambda t: t[1], reverse=True)[:RECOMMENDATION_LIMIT]).items()
    with transaction.atomic():
        for (song, similarity) in orderedRecomendation:
            try:
                UserAverage_Recommendations.objects.create(
                        song_id=song.id,
                        user_id=user.id,
                        life_id=UserAverage_Life.objects.last().id,
                        similarity=similarity,
                        iLike=bool(choice([True, False])),
                        score=randint(MIN_SCORE, MAX_SCORE))
            except Exception:
                logger.info("-----Error Alert-----")
                continue


def UserAverage(songSetLimit=Song.objects.count()):
    logger.info("[Start User Average]")
{'fraction_non_alphanumeric': 0.0825364131234368, 'fraction_numerical': 0.001176989848462557, 'mean_word_length': 2.674594594594595, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '671451', 'n_tokens_mistral': 2052, 'n_tokens_neox': 1933, 'n_words': 395}