Changed to difflib instead of Lavenshtein

This commit is contained in:
Jeison Yehuda Amihud (Blender Dumbass) 2022-03-30 14:54:49 +00:00
parent df98e6db69
commit 46a9130e41

View file

@ -1,94 +1,99 @@
# THIS SOFTWARE IS A PART OF FREE COMPETITOR PROJECT # THIS SOFTWARE IS A PART OF FREE COMPETITOR PROJECT
# THE FOLLOWING SOURCE CODE I UNDER THE GNU # THE FOLLOWING SOURCE CODE I UNDER THE GNU
# AGPL LICENSE V3 OR ANY LATER VERSION. # AGPL LICENSE V3 OR ANY LATER VERSION.
# This project is not for simple users, but for # This project is not for simple users, but for
# web-masters and a like, so we are counting on # web-masters and a like, so we are counting on
# your ability to set it up and running. # your ability to set it up and running.
import os import os
import json import json
import Levenshtein from difflib import SequenceMatcher
def similar(a, b):
def search_app(name): # I guess it simpifies the syntax for SequenceMatcher
# In the previous version we use Lavenshtain but it made
# This function output a json of an app that's the closest # it an issue for some people to install.
# match to the requested name. return SequenceMatcher(None, a, b).ratio()
closest = {} def search_app(name):
match = 0
# This function output a json of an app that's the closest
all_apps = [] # match to the requested name.
for i in os.listdir("apps"):
if i.endswith(".json"): closest = {}
match = 0
try:
with open("apps/"+i) as json_file: all_apps = []
idata = json.load(json_file) for i in os.listdir("apps"):
except Exception as e: if i.endswith(".json"):
print("Error!", i, e)
idata = {} try:
with open("apps/"+i) as json_file:
all_apps.append(idata) idata = json.load(json_file)
# Round 1. By the name except Exception as e:
print("Error!", i, e)
for i in all_apps: idata = {}
for n in i.get("names",[]):
m = Levenshtein.jaro_winkler(n.lower(), name.lower()) all_apps.append(idata)
if m > match and m > 0.49: # Round 1. By the name
closest = i
match = m for i in all_apps:
for n in i.get("names",[]):
if closest: m = similar(n.lower(), name.lower())
return closest if m > match and m > 0.49:
closest = i
# Round 2. By Generic name match = m
for i in all_apps: if closest:
for n in i.get("generic_name",[]): return closest
m = Levenshtein.jaro_winkler(n.lower(), name.lower())
if m > match: # Round 2. By Generic name
closest = i
match = m for i in all_apps:
for n in i.get("generic_name",[]):
if closest: m = similar(n.lower(), name.lower())
return closest if m > match:
closest = i
def suggest(json_data): match = m
# This function will suggest if closest:
return closest
found = [] def suggest(json_data):
all_apps = []
for i in os.listdir("apps"): # This function will suggest
if i.endswith(".json"):
try: found = []
with open("apps/"+i) as json_file: all_apps = []
idata = json.load(json_file) for i in os.listdir("apps"):
except Exception as e: if i.endswith(".json"):
print("Error!", i, e)
idata = {} try:
with open("apps/"+i) as json_file:
all_apps.append(idata) idata = json.load(json_file)
except Exception as e:
for i in all_apps: print("Error!", i, e)
score = 0 idata = {}
for c in ["generic_name", "networks_read", "networks_write", "formats_read", "formats_write"]:
all_apps.append(idata)
for b in json_data.get(c, []):
if b in i.get(c, []): for i in all_apps:
score += 1 score = 0
print(score, i.get("names",["no name"])[0], "SCORE") for c in ["generic_name", "networks_read", "networks_write", "formats_read", "formats_write"]:
found.append([score, i])
for b in json_data.get(c, []):
try: if b in i.get(c, []):
found.sort(key=lambda x: x[0]) score += 1
found = list(reversed(found)) print(score, i.get("names",["no name"])[0], "SCORE")
except Exception as e: found.append([score, i])
print("Found problem:", e)
fount = [] try:
found.sort(key=lambda x: x[0])
return found found = list(reversed(found))
except Exception as e:
print("Found problem:", e)
fount = []
return found