<commit_before># -*- encoding: utf-8 -*-
import random
import re
from abjad.tools import systemtools


urlify_pattern = re.compile(r"\s+", re.MULTILINE)


entity_type_names = {
    1: 'artist',
    2: 'label',
    }


entity_name_types = {
    'artist': 1,
    'label': 2,
    }


args_roles_pattern = re.compile(r'^roles(\[\d+\])?$')


def get_entity(entity_type, entity_id):
    import discograph
    where_clause = discograph.PostgresEntity.entity_id == entity_id
    where_clause &= discograph.PostgresEntity.entity_type == entity_type
    query = discograph.PostgresEntity.select().where(where_clause)
    found = list(query)
    if not found:
        return None
    return found[0]


def get_network(entity_id, entity_type, on_mobile=False, cache=True, roles=None):
    import discograph
    assert entity_type in ('artist', 'label')
    if cache:
        template = 'discograph:/api/{entity_type}/network/{entity_id}'
        if on_mobile:
            template = '{}/mobile'.format(template)
        cache_key = discograph.RelationGrapher.make_cache_key(
            template,
            entity_type,
            entity_id,
            roles=roles,
            )
        cache_key = cache_key.format(entity_type, entity_id)
        data = discograph.RelationGrapher.cache_get(cache_key)
        if data is not None:
            return data
    entity_type = entity_name_types[entity_type]
    entity = get_entity(entity_type, entity_id)
    if entity is None:
        return None
    if not on_mobile:
        max_nodes = 75
        degree = 12
    else:
        max_nodes = 25
        degree = 6
    relation_grapher = discograph.RelationGrapher2(
        center_entity=entity,
        degree=degree,
        max_nodes=max_nodes,
        roles=roles,
        )
    with systemtools.Timer(exit_message='Network query time:'):
        data = relation_grapher()
    if cache:
        discograph.RelationGrapher.cache_set(cache_key, data)
    return data


def get_random_entity(roles=None):
    import discograph
    relation = discograph.PostgresRelation.get_random(roles=roles)
    entity_choice = random.randint(1, 2)
    if entity_choice == 1:
        entity_type = relation.entity_one_type
        entity_id = relation.entity_one_id
    else:
        entity_type = relation.entity_two_type
        entity_id = relation.entity_two_id
    return entity_type, entity_id


def parse_request_args(args):
    from discograph.library import CreditRole
    year = None
    roles = set()
    for key in args:
        print('ARGS', key, args[key])
        if key == 'year':
            value = args[key]
            try:
                if '-' in year:
                    start, _, stop = year.partition('-')
                    year = tuple(sorted((int(start), int(stop))))
                else:
                    year = int(year)
            except:
                pass
        elif args_roles_pattern.match(key):
            value = args.getlist(key)
            for role in value:
                if role in CreditRole.all_credit_roles:
                    roles.add(role)
    roles = list(sorted(roles))
    return roles, year


def search_entities(search_string, cache=True):<commit_msg>Improved query string parsing.<commit_after># -*- encoding: utf-8 -*-
import random
import re
from abjad.tools import systemtools


urlify_pattern = re.compile(r"\s+", re.MULTILINE)


entity_type_names = {
    1: 'artist',
    2: 'label',
    }


entity_name_types = {
    'artist': 1,
    'label': 2,
    }


args_roles_pattern = re.compile(r'^roles(\[\d*\])?$')


def get_entity(entity_type, entity_id):
    import discograph
    where_clause = discograph.PostgresEntity.entity_id == entity_id
    where_clause &= discograph.PostgresEntity.entity_type == entity_type
    query = discograph.PostgresEntity.select().where(where_clause)
    found = list(query)
    if not found:
        return None
    return found[0]


def get_network(entity_id, entity_type, on_mobile=False, cache=True, roles=None):
    import discograph
    assert entity_type in ('artist', 'label')
    if cache:
        template = 'discograph:/api/{entity_type}/network/{entity_id}'
        if on_mobile:
            template = '{}/mobile'.format(template)
        cache_key = discograph.RelationGrapher.make_cache_key(
            template,
            entity_type,
            entity_id,
            roles=roles,
            )
        cache_key = cache_key.format(entity_type, entity_id)
        data = discograph.RelationGrapher.cache_get(cache_key)
        if data is not None:
            return data
    entity_type = entity_name_types[entity_type]
    entity = get_entity(entity_type, entity_id)
    if entity is None:
        return None
    if not on_mobile:
        max_nodes = 75
        degree = 12
    else:
        max_nodes = 25
        degree = 6
    relation_grapher = discograph.RelationGrapher2(
        center_entity=entity,
        degree=degree,
        max_nodes=max_nodes,
        roles=roles,
        )
    with systemtools.Timer(exit_message='Network query time:'):
        data = relation_grapher()
    if cache:
        discograph.RelationGrapher.cache_set(cache_key, data)
    return data


def get_random_entity(roles=None):
    import discograph
    relation = discograph.PostgresRelation.get_random(roles=roles)
    entity_choice = random.randint(1, 2)
    if entity_choice == 1:
        entity_type = relation.entity_one_type
        entity_id = relation.entity_one_id
    else:
        entity_type = relation.entity_two_type
        entity_id = relation.entity_two_id
    return entity_type, entity_id


def parse_request_args(args):
    from discograph.library import CreditRole
    year = None
    roles = set()
    for key in args:
        if key == 'year':
            value = args[key]
            try:
                if '-' in year:
                    start, _, stop = year.partition('-')
                    year = tuple(sorted((int(start), int(stop))))
                else:
                    year = int(year)
            except:
                pass
        elif args_roles_pattern.match(key):
            value = args.getlist(key)
            for role in value:
                if role in CreditRole.all_credit_roles:
                    roles.add(role)
    roles = list(sorted(roles))
    return roles, year


def search_entities(search_string, cache=True):
{'fraction_non_alphanumeric': 0.09424494276305473, 'fraction_numerical': 0.0053316606554806335, 'mean_word_length': 2.297828335056877, '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': '1510110', 'n_tokens_mistral': 1995, 'n_tokens_neox': 1938, 'n_words': 419}