extract 1/ calculation into function
This commit is contained in:
parent
385dbe450e
commit
6e159ad5c4
1 changed files with 10 additions and 9 deletions
|
@ -11,6 +11,10 @@ from part import Part
|
|||
import part_list_io
|
||||
|
||||
|
||||
def antilen(string: str):
|
||||
"""Return a bigger number for shorter strings, except 0 for ""."""
|
||||
return 1 / len(string) if string else 0
|
||||
|
||||
async def find_parts(parts: list[Part], search_string: str, max_number: int=10) -> list[Part]:
|
||||
"""Gives parts that the user might have searched for.
|
||||
|
||||
|
@ -32,7 +36,7 @@ async def find_parts(parts: list[Part], search_string: str, max_number: int=10)
|
|||
part.name_alt_en if hasattr(part, "name_alt_en") else [])
|
||||
return (
|
||||
# first prio: name at begin. Prefer short
|
||||
1 / len(part.name) if part.name.startswith(search_string) else 0,
|
||||
antilen(part.name) if part.name.startswith(search_string) else 0
|
||||
# second prio: part.name_en and name_de at begin. Prefer short
|
||||
max(1 / len(part.name_en)
|
||||
if hasattr(part, "name_en") and part.name_en.startswith(search_string)
|
||||
|
@ -46,15 +50,12 @@ async def find_parts(parts: list[Part], search_string: str, max_number: int=10)
|
|||
(hasattr(part, "name_en") and bool(fuzzy.match(part.name_en)))
|
||||
or (hasattr(part, "name_de") and bool(fuzzy.match(part.name_de))),
|
||||
# fith prio: alternative name at begin
|
||||
max(max(1 / len(alt_name) if alt_name.startswith(search_string) else 0
|
||||
for alt_name in part.name_alt_en) if hasattr(part, "name_alt_en") else 0,
|
||||
max(1 / len(alt_name) if alt_name.startswith(search_string) else 0
|
||||
for alt_name in part.name_alt_de) if hasattr(part, "name_alt_de") else 0),
|
||||
max((1 / len(alt_name) for alt_name in alt_names if alt_name.startswith(search_string)),
|
||||
default=0),
|
||||
# sixth prio: alternative name fuzzy match
|
||||
max(max(1 / len(alt_name) if fuzzy.match(alt_name) else 0
|
||||
for alt_name in part.name_alt_en) if hasattr(part, "name_alt_en") else 0,
|
||||
max(1 / len(alt_name) if fuzzy.match(alt_name) else 0
|
||||
for alt_name in part.name_alt_de) if hasattr(part, "name_alt_de") else 0),
|
||||
max(map(lambda string: 1/len(string), filter(fuzzy.match, alt_names)),
|
||||
default=0),
|
||||
# seventh prio: description
|
||||
max(int(hasattr(part, "description_de") and search_string in part.description_de) * 3,
|
||||
int(hasattr(part, "description_de") and bool(fuzzy.match(part.description_de))),
|
||||
int(hasattr(part, "description_en") and search_string in part.description_en) * 3,
|
||||
|
|
Loading…
Reference in a new issue