def get_theme_suggestions(search_core: SolrCollection, in_context: str) -> list:
    """
    Get theme suggestions within a given context and use the number of
    occurrences of a theme within the context as weight

    :param search_core: The search core to get theme suggestions from
    :param in_context: The context
    :return: The list of theme suggestions
    """
    context_entities = search_core.select_all_documents(
        'sys_type:"{0}"'.format(in_context), ['theme'], id_field='sys_id'
    )

    counts = {}

    for context_entity in context_entities:
        if 'theme' not in context_entity:
            continue

        for theme in context_entity['theme']:
            if theme not in counts:
                counts[theme] = 0

            counts[theme] += 1

    synonyms_uri_nl = search_core.select_managed_synonyms('uri_nl')

    return [{
        'theme': synonyms_uri_nl[theme] if theme in synonyms_uri_nl else theme,
        'weight': count,
        'payload': theme,
        'type': 'theme',
        'language': ['nl', 'en'],
        'in_context_of': in_context
    } for theme, count in counts.items()]
def manage_uri_synonyms(collection: SolrCollection) -> None:
    logging.info('> managing uri_synonyms resources')

    uri_synonyms = {'uri_nl': {}, 'uri_en': {}}
    legacy_license_files = ['ckan_license.json', 'overheid_license.json']

    for valuelist in os.listdir(os.getenv('VALUELIST_DIR')):
        if not valuelist.endswith('.json'):
            continue

        if valuelist in legacy_license_files:
            continue

        filepath = os.path.join(os.getenv('VALUELIST_DIR'), valuelist)

        for uri, properties in utils.load_json_file(filepath).items():
            uri_synonyms['uri_nl'][uri] = properties['labels']['nl-NL']
            uri_synonyms['uri_en'][uri] = properties['labels']['en-US']

    for lang in uri_synonyms.keys():
        logging.info('managed resource: synonyms|%s', lang)

        current_uri_synonyms = collection.select_managed_synonyms(lang)

        if not current_uri_synonyms:
            current_uri_synonyms = {}

        logging.info(' current: %s synonyms', len(current_uri_synonyms))

        synonyms_to_add = {
            key: value
            for key, value in uri_synonyms[lang].items()
            if key not in current_uri_synonyms.keys()
        }

        logging.info(' adding: %s synonyms', len(synonyms_to_add))

        if len(synonyms_to_add) > 0:
            collection.add_managed_synonyms(lang, synonyms_to_add)
def manage_label_synonyms(collection: SolrCollection,
                          language_code: str) -> None:
    logging.info('> managing label resource')

    current_label_synonyms = collection.select_managed_synonyms(
        'label_{0}'.format(language_code))

    logging.info(' current: %s synonyms', len(current_label_synonyms))

    label_synonyms = utils.load_resource('labels_{0}'.format(language_code))

    synonyms_to_add = {
        key: value
        for key, value in label_synonyms.items()
        if key not in current_label_synonyms.keys()
    }

    logging.info(' adding: %s synonyms', len(synonyms_to_add))

    if len(synonyms_to_add) > 0:
        collection.add_managed_synonyms('label_{0}'.format(language_code),
                                        synonyms_to_add)
def manage_hierarchy_theme(collection: SolrCollection) -> None:
    logging.info('> managing hierarchy_theme resources')

    for hierarchy_item in ['hierarchy_theme', 'hierarchy_theme_query']:
        logging.info('managed resource: synonyms|{0}'.format(hierarchy_item))

        hierarchy_data = utils.load_resource(hierarchy_item)
        current_data = collection.select_managed_synonyms(hierarchy_item)

        if not current_data:
            current_data = {}

        logging.info(' current: %s synonyms', len(current_data))

        data_to_add = {
            key: value
            for key, value in hierarchy_data.items()
            if key not in current_data.keys()
        }

        logging.info(' adding: %s synonyms', len(data_to_add))

        if len(data_to_add) > 0:
            collection.add_managed_synonyms(hierarchy_item, data_to_add)