Skip to content

Commit

Permalink
fix: Use process.extract to get the corresponding party doc name of…
Browse files Browse the repository at this point in the history
… the result

- rapidfuzz accepts an iterable or a dict. dict input gives the dict key and value in the result

(cherry picked from commit e0a0378)
  • Loading branch information
marination authored and mergify[bot] committed Nov 4, 2023
1 parent 21b430a commit b0a184e
Showing 1 changed file with 4 additions and 7 deletions.
11 changes: 4 additions & 7 deletions erpnext/accounts/doctype/bank_transaction/auto_match_party.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from typing import Tuple, Union

import frappe
from frappe.core.utils import find
from frappe.utils import flt
from rapidfuzz import fuzz, process

Expand Down Expand Up @@ -135,16 +134,14 @@ def fuzzy_search_and_return_result(self, party, names, field) -> Union[Tuple, No
skip = False
result = process.extract(
query=self.get(field),
choices=[name.get("party_name") for name in names],
choices={row.get("name"): row.get("party_name") for row in names},
scorer=fuzz.token_set_ratio,
)
party_name, skip = self.process_fuzzy_result(result)

if not party_name:
return None, skip

# Get Party Docname from the list of dicts
party_name = find(names, lambda x: x["party_name"] == party_name).get("name")
return (
party,
party_name,
Expand All @@ -157,14 +154,14 @@ def process_fuzzy_result(self, result: Union[list, None]):
Returns: Result, Skip (whether or not to discontinue matching)
"""
PARTY, SCORE, CUTOFF = 0, 1, 80
SCORE, PARTY_ID, CUTOFF = 1, 2, 80

if not result or not len(result):
return None, False

first_result = result[0]
if len(result) == 1:
return (first_result[PARTY] if first_result[SCORE] > CUTOFF else None), True
return (first_result[PARTY_ID] if first_result[SCORE] > CUTOFF else None), True

second_result = result[1]
if first_result[SCORE] > CUTOFF:
Expand All @@ -173,7 +170,7 @@ def process_fuzzy_result(self, result: Union[list, None]):
if first_result[SCORE] == second_result[SCORE]:
return None, True

return first_result[PARTY], True
return first_result[PARTY_ID], True
else:
return None, False

Expand Down

0 comments on commit b0a184e

Please sign in to comment.