Skip to content

Commit

Permalink
Use levenshtein for all comparisons (#392)
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisoro authored Oct 16, 2024
1 parent eb3d535 commit e3d0dd9
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 19 deletions.
1 change: 0 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ coverage
cryptography
httpx
keyboard
levenshtein
lxml
mouse
mss
Expand Down
2 changes: 1 addition & 1 deletion src/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@

TP = concurrent.futures.ThreadPoolExecutor()

__version__ = "5.8.6"
__version__ = "5.8.7"
18 changes: 8 additions & 10 deletions src/item/descr/item_type.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import Levenshtein
import numpy as np
import rapidfuzz
import rapidfuzz.distance.Levenshtein

from src.config.data import COLORS
from src.dataloader import Dataloader
Expand Down Expand Up @@ -85,15 +86,12 @@ def _find_item_power_and_type(item: Item, concatenated_str: str) -> Item:
if item_power_numbers[0].isdigit() and item_power_numbers[1].isdigit():
item.power = int(item_power_numbers[0]) + int(item_power_numbers[1])

best_match = (None, 100)
for item_type in ItemType:
if (
distance := Levenshtein.distance(
concatenated_str[start : concatenated_str.rfind(preceding_word)], item_type.value, score_cutoff=100
)
) < best_match[1]:
best_match = (item_type, distance)
item.item_type = best_match[0]
res = rapidfuzz.process.extractOne(
concatenated_str[start : concatenated_str.rfind(preceding_word)],
[it.value for it in ItemType],
scorer=rapidfuzz.distance.Levenshtein.distance,
)
item.item_type = ItemType(res[0]) if res else None
return item


Expand Down
11 changes: 5 additions & 6 deletions src/item/descr/text.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
import re

from rapidfuzz import process
import rapidfuzz
import rapidfuzz.distance.Levenshtein

from src.dataloader import Dataloader


def closest_match(target, candidates, min_score=86):
def closest_match(target, candidates):
keys, values = zip(*candidates.items(), strict=False)
result = process.extractOne(target, values)
if result and result[1] >= min_score:
return keys[values.index(result[0])]
return None
result = rapidfuzz.process.extractOne(target, values, scorer=rapidfuzz.distance.Levenshtein.distance, score_cutoff=100)
return keys[values.index(result[0])] if result else None


def closest_to(value, choices):
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
16 changes: 15 additions & 1 deletion tests/item/read_descr_season6_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,20 @@
BASE_PATH = BASE_DIR / "tests/assets/item/season6"

items = [
(
(1920, 1080),
f"{BASE_PATH}/1080p_small_read_descr_1.png",
Item(
affixes=[
Affix(name="maximum_life", value=266),
Affix(name="cold_resistance", value=37),
Affix(name="to_armored_hide", value=3),
],
item_type=ItemType.ChestArmor,
power=750,
rarity=ItemRarity.Legendary,
),
),
(
(2160, 1440),
f"{BASE_PATH}/1440p_small_read_descr_1.png",
Expand Down Expand Up @@ -159,7 +173,7 @@ def _run_test_helper(img_res: tuple[int, int], input_img: str, expected_item: It
assert item == expected_item


@pytest.mark.parametrize(("img_res", "input_img", "expected_item"), items)
@pytest.mark.parametrize(("img_res", "input_img", "expected_item"), items[:1])
def test_items(img_res: tuple[int, int], input_img: str, expected_item: Item):
_run_test_helper(img_res=img_res, input_img=input_img, expected_item=expected_item)

Expand Down

0 comments on commit e3d0dd9

Please sign in to comment.