<commit_before>from myfedora.plugin import Tool
from turbogears import expose

import os
import koji

from myfedora.urlhandler import KojiURLHandler
from locale import strcoll

FULL_WEIGHT=100
MEDIUM_WEIGHT=50
LIGHT_WEIGHT=25

# sort based on weight, each element is of the form [package_name, weight]
def weighted_sort(a, b):
    result = 0
    (a_name, a_weight) = a
    (b_name, b_weight) = b

    result = -cmp(a_weight, b_weight)
    if result == 0:
        result = cmp(a_name, b_name)

    return result

class PackageSearchTool(Tool):
    def __init__(self, parent_resource):
        Tool.__init__(self, parent_resource,
                                   'Packages',
                                   'packages',
                                   'Fedora package search',
                                   '''Searches the Fedora package database''',
                                   ['search'],
                                   [])

    @expose(template='myfedora.tools.searchtools.templates.index', allow_json=True)
    def index(self, search_string='', **kwargs):
        result = self.get_parent_resource().get_template_globals(search_string)

        return result

    def search(self, search_str):
        # use koji for now but we really should use pkgdb 
        # or better yet set up a search db 
        cs = koji.ClientSession(KojiURLHandler().get_xml_rpc_url())
        search_terms = search_str.split()
        search_results = {} 
        for term in search_terms:
            search = cs.search('*' + term + '*', 'package', 'glob')
            cmp_term = term.upper()
            for pkg_hash in search:
                pkg = pkg_hash['name']
                cmp_pkg = pkg.upper()

                result = search_results.get(pkg, None)

                if not result:
                    result = [pkg, 0]

                relevance = result[1]

                # if we found this more than once 
                # then add weight to it's relevance 
                if relevance > 0:
                    relevance += MEDIUM_WEIGHT

                # if all search terms are in then
                # add weight to the relevance
                for check_term in search_terms:
                    count = 0
                    cmp_check_term = check_term.upper()
                    if cmp_pkg.count(cmp_term):
                        count += 1

                    if count > 1:
                        relevance += FULL_WEIGHT * (count / len(search_terms))

                # if we start with one of the search terms
                # add weight to the relevance
                if cmp_pkg.startswith(cmp_term):
                    relevance += LIGHT_WEIGHT

                # if we end with one of the seach terms
                # add weight to relevance
                if cmp_pkg.endswith(cmp_term):
                    relevance += LIGHT_WEIGHT

                result[1] = relevance
                search_results[pkg] = result

        weighted_pkg_list = search_results.values()
        weighted_pkg_list.sort(weighted_sort)
        
        pkg_list = []
        # put it into the form we want
        # FIXME: we can optimize above so we don't have to do this
        for pkg in weighted_pkg_list:
            item = {'name': pkg[0],
                    'url': self.get_parent_resource().url('/package/' + pkg[0]),
                    'summary': '' }# none for now
            pkg_list.append(item)
<commit_msg>fix typo so search links actually go to the correct packages page<commit_after>from myfedora.plugin import Tool
from turbogears import expose

import os
import koji

from myfedora.urlhandler import KojiURLHandler
from locale import strcoll

FULL_WEIGHT=100
MEDIUM_WEIGHT=50
LIGHT_WEIGHT=25

# sort based on weight, each element is of the form [package_name, weight]
def weighted_sort(a, b):
    result = 0
    (a_name, a_weight) = a
    (b_name, b_weight) = b

    result = -cmp(a_weight, b_weight)
    if result == 0:
        result = cmp(a_name, b_name)

    return result

class PackageSearchTool(Tool):
    def __init__(self, parent_resource):
        Tool.__init__(self, parent_resource,
                                   'Packages',
                                   'packages',
                                   'Fedora package search',
                                   '''Searches the Fedora package database''',
                                   ['search'],
                                   [])

    @expose(template='myfedora.tools.searchtools.templates.index', allow_json=True)
    def index(self, search_string='', **kwargs):
        result = self.get_parent_resource().get_template_globals(search_string)

        return result

    def search(self, search_str):
        # use koji for now but we really should use pkgdb 
        # or better yet set up a search db 
        cs = koji.ClientSession(KojiURLHandler().get_xml_rpc_url())
        search_terms = search_str.split()
        search_results = {} 
        for term in search_terms:
            search = cs.search('*' + term + '*', 'package', 'glob')
            cmp_term = term.upper()
            for pkg_hash in search:
                pkg = pkg_hash['name']
                cmp_pkg = pkg.upper()

                result = search_results.get(pkg, None)

                if not result:
                    result = [pkg, 0]

                relevance = result[1]

                # if we found this more than once 
                # then add weight to it's relevance 
                if relevance > 0:
                    relevance += MEDIUM_WEIGHT

                # if all search terms are in then
                # add weight to the relevance
                for check_term in search_terms:
                    count = 0
                    cmp_check_term = check_term.upper()
                    if cmp_pkg.count(cmp_term):
                        count += 1

                    if count > 1:
                        relevance += FULL_WEIGHT * (count / len(search_terms))

                # if we start with one of the search terms
                # add weight to the relevance
                if cmp_pkg.startswith(cmp_term):
                    relevance += LIGHT_WEIGHT

                # if we end with one of the seach terms
                # add weight to relevance
                if cmp_pkg.endswith(cmp_term):
                    relevance += LIGHT_WEIGHT

                result[1] = relevance
                search_results[pkg] = result

        weighted_pkg_list = search_results.values()
        weighted_pkg_list.sort(weighted_sort)
        
        pkg_list = []
        # put it into the form we want
        # FIXME: we can optimize above so we don't have to do this
        for pkg in weighted_pkg_list:
            item = {'name': pkg[0],
                    'url': self.get_parent_resource().url('/packages/' + pkg[0]),
                    'summary': '' }# none for now
            pkg_list.append(item)

{'fraction_non_alphanumeric': 0.07839195979899498, 'fraction_numerical': 0.005168700646087581, 'mean_word_length': 1.5220854453294714, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, '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': '668461', 'n_tokens_mistral': 1987, 'n_tokens_neox': 1784, 'n_words': 545}