def test_levenshtein(): """ Test common.levenshtein. """ assert common.levenshtein('herp', 'herpderp') == 4 assert common.levenshtein('herpderp', 'herp') == 4 assert common.levenshtein('hederprp', 'herp') == 4 assert common.levenshtein('herp', 'hederprp') == 4 assert common.levenshtein('preherppost', 'preprehpost') == 4 assert common.levenshtein('abcdef', 'abdce') == 3 assert common.levenshtein('1234', '') == 4
def search(query, size): """ Search for plugins. This function will search for plugins on bukget by their slug and name. The results are ordered by a score calculated with their levenshtein edit distance from the search query and their popularity. Two parameters are required: query The query to search for. size The size of the returned results. It can be negative, and in that case the bottom of the results are returned. A list with the plugin dicts from BukGet is returned. It is sorted by the score system mentioned earlier. """ sorting = ('-' if size >= 0 else '')+'popularity.monthly' search_results = bukget.search( { 'field': 'plugin_name', 'action': 'like', 'value': query }, sort=sorting, fields='slug,plugin_name,description,popularity.monthly', size=abs(size)) search_results += bukget.search( { 'field': 'slug', 'action': 'like', 'value': query }, sort=sorting, fields='slug,plugin_name,description,popularity.monthly', size=abs(size)) search_results = utils.remove_duplicate_plugins(search_results) # Calculate scores results = list() for plugin in search_results: score = plugin['popularity']['monthly'] distance = common.levenshtein( query, plugin['plugin_name']) score -= distance results.append((score, plugin)) results.sort(key=lambda x: x[0], reverse=True) return [result[1] for result in results]
def search(query, size): """ Search for plugins. This function will search for plugins on bukget by their slug and name. The results are ordered by a score calculated with their levenshtein edit distance from the search query and their popularity. Two parameters are required: query The query to search for. size The size of the returned results. It can be negative, and in that case the bottom of the results are returned. A list with the plugin dicts from BukGet is returned. It is sorted by the score system mentioned earlier. """ sorting = ('-' if size >= 0 else '') + 'popularity.monthly' search_results = bukget.search( { 'field': 'plugin_name', 'action': 'like', 'value': query }, sort=sorting, fields='slug,plugin_name,description,popularity.monthly', size=abs(size)) search_results += bukget.search( { 'field': 'slug', 'action': 'like', 'value': query }, sort=sorting, fields='slug,plugin_name,description,popularity.monthly', size=abs(size)) search_results = utils.remove_duplicate_plugins(search_results) # Calculate scores results = list() for plugin in search_results: score = plugin['popularity']['monthly'] distance = common.levenshtein(query, plugin['plugin_name']) score -= distance results.append((score, plugin)) results.sort(key=lambda x: x[0], reverse=True) return [result[1] for result in results]