<commit_before>"""
Search helper for interfacing web with OCL API.
"""
from django.http import QueryDict
#from urllib import quote
import logging

logger = logging.getLogger('oclweb')


class SearchFilterOption(object):
    """
    Defines a specific search filter option (e.g. English).
    """
    def __init__(
            self, search_filter=None, option_value='',
            option_name='', option_num=0, selected=False):
        self.search_filter = search_filter
        self.option_value = option_value
        self.option_name = option_name
        self.option_num = option_num
        self.selected = selected

    def __str__(self):
        return "%s: %s [%s] %s" % (self.search_filter.filter_name,
                                   self.option_name,
                                   self.option_num,
                                   self.selected)

    def __unicode__(self):
        return u"%s: %s [%s] %s" % (self.search_filter.filter_name,
                                    self.option_name,
                                    self.option_num,
                                    self.selected)



class SearchFilter(object):
    """
    A specific search filter (e.g. Locale) and its options (e.g. English).

    options is a dictionary of SearchFilterOption instances
    """
    def __init__(
            self,
            filter_id='', filter_name='', filter_widget='',
            facet_id='', facet_results=None, minimized=False, attrs=None):
        self.filter_id = filter_id          # unique ID for query etc
        self.filter_name = filter_name      # for display
        self.filter_widget = filter_widget
        self.facet_id = facet_id
        self.facet_results = facet_results
        self.minimized = minimized
        if not attrs:
            attrs = {}
        self.attrs = attrs
        self.options = []                   # list of search filter options

        if self.facet_results:
            self.build_options_from_facets(facet_results=facet_results)


    def build_options_from_facets(self, facet_results=None):
        """ Creates filter options using the facet results """
        for facet_option in facet_results:
            facet_option_name = facet_option[0]
            facet_option_num = facet_option[1]
            self.add_option(option_value=facet_option_name,
                            option_name=facet_option_name,
                            option_num=facet_option_num)


    def add_option(self, option_value='', option_name='', option_num=0, selected=False):
        """ Add SearchFilterOption to the SearchFilter. """
        self.options.append(SearchFilterOption(
            search_filter=self, option_value=option_value,
            option_name=option_name, option_num=option_num, selected=selected))


    def select_option(self, option_values):
        """
        Mark option(s) as selected according to the value or list of string values passed.
        :param option_values: Value or list of values to select
        """
        if not isinstance(option_values, list):
            option_values = [option_values]
        for opt in self.options:
            if opt.option_value in option_values:<commit_msg>Implemented search filter option definitions<commit_after>"""
Search helper for interfacing web with OCL API.
"""
from django.http import QueryDict
#from urllib import quote
import logging

logger = logging.getLogger('oclweb')


class SearchFilterOption(object):
    """
    Defines a specific search filter option (e.g. English).
    """
    def __init__(
            self, search_filter=None, option_value='',
            option_name='', option_num=0, selected=False):
        self.search_filter = search_filter
        self.option_value = option_value
        self.option_name = option_name
        self.option_num = option_num
        self.selected = selected

    def __str__(self):
        return "%s: %s [%s] %s" % (self.search_filter.filter_name,
                                   self.option_name,
                                   self.option_num,
                                   self.selected)

    def __unicode__(self):
        return u"%s: %s [%s] %s" % (self.search_filter.filter_name,
                                    self.option_name,
                                    self.option_num,
                                    self.selected)



class SearchFilter(object):
    """
    A specific search filter (e.g. Locale) and its options (e.g. English).

    options is a dictionary of SearchFilterOption instances
    """
    def __init__(
            self,
            filter_id='', filter_name='', filter_widget='', options=None,
            facet_id='', facet_results=None, minimized=False, attrs=None):
        self.filter_id = filter_id          # unique ID for query etc
        self.filter_name = filter_name      # for display
        self.filter_widget = filter_widget
        self.facet_id = facet_id
        self.facet_results = facet_results
        self.minimized = minimized
        if not attrs:
            attrs = {}
        self.attrs = attrs
        self.option_defs = options
        self.options = []                   # list of search filter options

        if self.option_defs:
            self.build_options(option_defs=self.option_defs)

        if self.facet_results:
            self.build_options_from_facets(facet_results=facet_results)


    def build_options(self, option_defs=None):
        """ Creates filter options from option definitions """
        for option_def in option_defs:
            self.add_option(**option_def)


    def build_options_from_facets(self, facet_results=None):
        """ Creates filter options using the facet results """
        for facet_option in facet_results:
            facet_option_name = facet_option[0]
            facet_option_num = facet_option[1]
            self.add_option(option_value=facet_option_name,
                            option_name=facet_option_name,
                            option_num=facet_option_num)


    def add_option(self, option_value='', option_name='', option_num=0, selected=False):
        """ Add SearchFilterOption to the SearchFilter. """
        self.options.append(SearchFilterOption(
            search_filter=self, option_value=option_value,
            option_name=option_name, option_num=option_num, selected=selected))


    def select_option(self, option_values):
        """
        Mark option(s) as selected according to the value or list of string values passed.
        :param option_values: Value or list of values to select
        """
        if not isinstance(option_values, list):
            option_values = [option_values]
        for opt in self.options:
            if opt.option_value in option_values:
{'fraction_non_alphanumeric': 0.0834319526627219, 'fraction_numerical': 0.001183431952662722, 'mean_word_length': 2.084397810218978, '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': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5079044', 'n_tokens_mistral': 1862, 'n_tokens_neox': 1797, 'n_words': 449}