<commit_before>"""Tools to help with searching and sorting API data."""

from messages import EventProperties, CalendarProperties

__author__ = "<NAME>"
__copyright__ = "Copyright (C) 2015 DHS Developers Club"


class NullSearchError(Exception):
    def __init__(self):
        super(NullSearchError, self).__init__(
                "Insufficient matches found for data item.")


def _get_event_kw_score(event, keywords, narrow=False):
    """
    Get a relevance score for an event based on keyword matches.

    :param EventProperties event: Event to be scored.
    :param str keywords: Search terms separated by spaces.
    :param bool narrow:
        If true, throw NullSearchError for insufficient keyword matches.

    :rtype: float
    :raise NullSearchError:
        If narrow=True and insufficient keyword matches are found.
    """
    event_string_data = event.name
    search_set = set(keywords.split())
    matches = 0.0
    for keyword in search_set:
        if keyword in event_string_data:
            matches += 1.0
    if narrow and matches < len(search_set) // 2:
        raise NullSearchError()
    return matches


def _get_calendar_kw_score(calendar, keywords, narrow=False):
    """
    Get a relevance score for a calendar based on keyword matches.

    :param CalendarProperties calendar: Event to be scored.
    :param str keywords: Search terms separated by spaces.
    :param bool narrow:
        If true, throw NullSearchError for insufficient keyword matches.

    :rtype: int
    :raise NullSearchError:
        If narrow=True and insufficient keyword matches are found.
    """
    calendar_string_data = calendar.name
    search_set = set(keywords.split())
    matches = 0
    for keyword in search_set:
        if keyword in calendar_string_data:
            matches += 1
    if narrow and matches < len(search_set) // 2:
        raise NullSearchError()
    return matches


def event_starred(e):
    return not e.starred


def event_start_date(e):
    return e.startDate


def event_kw_score(kw, narrow):
    return lambda e: _get_event_kw_score(e, kw, narrow)


def calendar_kw_score(kw, narrow):
    return lambda c: _get_calendar_kw_score(c, kw, narrow)


def calendar_alpha_score(c):
    return c.name


def event_chronological_order():
    return [event_starred, event_start_date]


def event_kw_chron_order(kw, narrow):
    return [event_starred, event_kw_score(kw, narrow), event_start_date]


def calendar_kw_alpha_order(kw, narrow):
    return [calendar_kw_score(kw, narrow), calendar_alpha_score]

<commit_msg>Add alpha sorting for events, at lowest weight.<commit_after>"""Tools to help with searching and sorting API data."""

from messages import EventProperties, CalendarProperties

__author__ = "<NAME>"
__copyright__ = "Copyright (C) 2015 DHS Developers Club"


class NullSearchError(Exception):
    def __init__(self):
        super(NullSearchError, self).__init__(
                "Insufficient matches found for data item.")


def _get_event_kw_score(event, keywords, narrow=False):
    """
    Get a relevance score for an event based on keyword matches.

    :param EventProperties event: Event to be scored.
    :param str keywords: Search terms separated by spaces.
    :param bool narrow:
        If true, throw NullSearchError for insufficient keyword matches.

    :rtype: float
    :raise NullSearchError:
        If narrow=True and insufficient keyword matches are found.
    """
    event_string_data = event.name
    search_set = set(keywords.split())
    matches = 0.0
    for keyword in search_set:
        if keyword in event_string_data:
            matches += 1.0
    if narrow and matches < len(search_set) // 2:
        raise NullSearchError()
    return matches


def _get_calendar_kw_score(calendar, keywords, narrow=False):
    """
    Get a relevance score for a calendar based on keyword matches.

    :param CalendarProperties calendar: Event to be scored.
    :param str keywords: Search terms separated by spaces.
    :param bool narrow:
        If true, throw NullSearchError for insufficient keyword matches.

    :rtype: int
    :raise NullSearchError:
        If narrow=True and insufficient keyword matches are found.
    """
    calendar_string_data = calendar.name
    search_set = set(keywords.split())
    matches = 0
    for keyword in search_set:
        if keyword in calendar_string_data:
            matches += 1
    if narrow and matches < len(search_set) // 2:
        raise NullSearchError()
    return matches


def event_starred(e):
    return not e.starred


def event_start_date(e):
    return e.startDate


def event_name(e):
    return e.name


def event_kw_score(kw, narrow):
    return lambda e: _get_event_kw_score(e, kw, narrow)


def calendar_kw_score(kw, narrow):
    return lambda c: _get_calendar_kw_score(c, kw, narrow)


def calendar_alpha_score(c):
    return c.name


def event_chronological_order():
    return [event_starred, event_start_date, event_name]


def event_kw_chron_order(kw, narrow):
    return [event_starred, event_kw_score(kw, narrow), event_start_date,
            event_name]


def calendar_kw_alpha_order(kw, narrow):
    return [calendar_kw_score(kw, narrow), calendar_alpha_score]


{'fraction_non_alphanumeric': 0.0762598198888676, 'fraction_numerical': 0.0045985821038513125, 'mean_word_length': 3.393939393939394, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3467790', 'n_tokens_mistral': 1536, 'n_tokens_neox': 1456, 'n_words': 501}