From 46a9130e419939190ad17c7cf0c9e464b9ff4900 Mon Sep 17 00:00:00 2001 From: "Jeison Yehuda Amihud (Blender Dumbass)" Date: Wed, 30 Mar 2022 14:54:49 +0000 Subject: [PATCH] Changed to difflib instead of Lavenshtein --- modules/search.py | 193 ++++++++++++++++++++++++---------------------- 1 file changed, 99 insertions(+), 94 deletions(-) diff --git a/modules/search.py b/modules/search.py index 0297812..c9da638 100644 --- a/modules/search.py +++ b/modules/search.py @@ -1,94 +1,99 @@ -# THIS SOFTWARE IS A PART OF FREE COMPETITOR PROJECT -# THE FOLLOWING SOURCE CODE I UNDER THE GNU -# AGPL LICENSE V3 OR ANY LATER VERSION. - -# This project is not for simple users, but for -# web-masters and a like, so we are counting on -# your ability to set it up and running. - -import os -import json -import Levenshtein - - -def search_app(name): - - # This function output a json of an app that's the closest - # match to the requested name. - - closest = {} - match = 0 - - all_apps = [] - for i in os.listdir("apps"): - if i.endswith(".json"): - - try: - with open("apps/"+i) as json_file: - idata = json.load(json_file) - except Exception as e: - print("Error!", i, e) - idata = {} - - all_apps.append(idata) - # Round 1. By the name - - for i in all_apps: - for n in i.get("names",[]): - m = Levenshtein.jaro_winkler(n.lower(), name.lower()) - if m > match and m > 0.49: - closest = i - match = m - - if closest: - return closest - - # Round 2. By Generic name - - for i in all_apps: - for n in i.get("generic_name",[]): - m = Levenshtein.jaro_winkler(n.lower(), name.lower()) - if m > match: - closest = i - match = m - - if closest: - return closest - -def suggest(json_data): - - # This function will suggest - - - found = [] - all_apps = [] - for i in os.listdir("apps"): - if i.endswith(".json"): - - try: - with open("apps/"+i) as json_file: - idata = json.load(json_file) - except Exception as e: - print("Error!", i, e) - idata = {} - - all_apps.append(idata) - - for i in all_apps: - score = 0 - for c in ["generic_name", "networks_read", "networks_write", "formats_read", "formats_write"]: - - for b in json_data.get(c, []): - if b in i.get(c, []): - score += 1 - print(score, i.get("names",["no name"])[0], "SCORE") - found.append([score, i]) - - try: - found.sort(key=lambda x: x[0]) - found = list(reversed(found)) - except Exception as e: - print("Found problem:", e) - fount = [] - - return found +# THIS SOFTWARE IS A PART OF FREE COMPETITOR PROJECT +# THE FOLLOWING SOURCE CODE I UNDER THE GNU +# AGPL LICENSE V3 OR ANY LATER VERSION. + +# This project is not for simple users, but for +# web-masters and a like, so we are counting on +# your ability to set it up and running. + +import os +import json +from difflib import SequenceMatcher + +def similar(a, b): + # I guess it simpifies the syntax for SequenceMatcher + # In the previous version we use Lavenshtain but it made + # it an issue for some people to install. + return SequenceMatcher(None, a, b).ratio() + +def search_app(name): + + # This function output a json of an app that's the closest + # match to the requested name. + + closest = {} + match = 0 + + all_apps = [] + for i in os.listdir("apps"): + if i.endswith(".json"): + + try: + with open("apps/"+i) as json_file: + idata = json.load(json_file) + except Exception as e: + print("Error!", i, e) + idata = {} + + all_apps.append(idata) + # Round 1. By the name + + for i in all_apps: + for n in i.get("names",[]): + m = similar(n.lower(), name.lower()) + if m > match and m > 0.49: + closest = i + match = m + + if closest: + return closest + + # Round 2. By Generic name + + for i in all_apps: + for n in i.get("generic_name",[]): + m = similar(n.lower(), name.lower()) + if m > match: + closest = i + match = m + + if closest: + return closest + +def suggest(json_data): + + # This function will suggest + + + found = [] + all_apps = [] + for i in os.listdir("apps"): + if i.endswith(".json"): + + try: + with open("apps/"+i) as json_file: + idata = json.load(json_file) + except Exception as e: + print("Error!", i, e) + idata = {} + + all_apps.append(idata) + + for i in all_apps: + score = 0 + for c in ["generic_name", "networks_read", "networks_write", "formats_read", "formats_write"]: + + for b in json_data.get(c, []): + if b in i.get(c, []): + score += 1 + print(score, i.get("names",["no name"])[0], "SCORE") + found.append([score, i]) + + try: + found.sort(key=lambda x: x[0]) + found = list(reversed(found)) + except Exception as e: + print("Found problem:", e) + fount = [] + + return found