<commit_before>import echonest
from artists.models import Artist
from echonest.models import SimilarResponse
from users.models import User
from .models import (GeneralArtist, UserSimilarity, Similarity,
                     update_similarities)


def add_new_similarities(artist, force_update=False):
    similarities = []
    responses = SimilarResponse.objects.filter(
        normalized_name=artist.normalized_name)
    if responses.exists() and not force_update:
        return  # Echo Nest similarities already added
    user = User.objects.get(email='echonest')
    artist_names = echonest.get_similar(artist.name)
    cc_artists = Artist.objects.filter(name__in=artist_names)
    for cc_artist in cc_artists:
        kwargs = dict(
            cc_artist=cc_artist,
            other_artist=artist,
        )
        UserSimilarity.objects.get_or_create(defaults={'weight': 1},
                                             user=user, **kwargs)
        similarities.append(Similarity.objects.get_or_create(**kwargs)[0])
    update_similarities(similarities)


def get_similar(name):
    artist, _ = GeneralArtist.objects.get_or_create(
        normalized_name=name.upper(), defaults={'name': name})
    add_new_similarities(artist)
    return Artist.objects.filter(similarity__other_artist=artist,
                                 similarity__weight__gt=0)<commit_msg>Order similar artist results properly<commit_after>from django.db.models import Q

import echonest
from artists.models import Artist
from echonest.models import SimilarResponse
from users.models import User
from .models import (GeneralArtist, UserSimilarity, Similarity,
                     update_similarities)


def add_new_similarities(artist, force_update=False):
    similarities = []
    responses = SimilarResponse.objects.filter(
        normalized_name=artist.normalized_name)
    if responses.exists() and not force_update:
        return  # Echo Nest similarities already added
    user = User.objects.get(email='echonest')
    artist_names = echonest.get_similar(artist.name)
    cc_artists = Artist.objects.filter(name__in=artist_names)
    for cc_artist in cc_artists:
        kwargs = dict(
            cc_artist=cc_artist,
            other_artist=artist,
        )
        UserSimilarity.objects.get_or_create(defaults={'weight': 1},
                                             user=user, **kwargs)
        similarities.append(Similarity.objects.get_or_create(**kwargs)[0])
    update_similarities(similarities)


def get_similar(name):
    artist, _ = GeneralArtist.objects.get_or_create(
        normalized_name=name.upper(), defaults={'name': name})
    add_new_similarities(artist)
    similar = Q(similarity__other_artist=artist, similarity__weight__gt=0)
    return Artist.objects.filter(similar).order_by('-similarity__weight')
{'fraction_non_alphanumeric': 0.08598726114649681, 'fraction_numerical': 0.0021231422505307855, 'mean_word_length': 3.335889570552147, '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': 5, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6316639', 'n_tokens_mistral': 838, 'n_tokens_neox': 803, 'n_words': 149}