Advertisement
Guest User

Untitled

a guest
Oct 15th, 2019
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.40 KB | None | 0 0
  1. def filter_items(match_a: MenuItem, match_b: MenuItem) -> bool:
  2. """
  3. Filter items which are possible matches using three scoring methods.
  4.  
  5. :param match_a:
  6. :param match_b:
  7. :return:
  8. """
  9. ratio_score, partial_score, token_set_score = score_items(match_a, match_b)
  10.  
  11. if ratio_score > 90:
  12. return True
  13. elif token_set_score > 85 and (ratio_score + partial_score > 120):
  14. return True
  15. elif ratio_score > 85 and partial_score > 85:
  16. return True
  17. elif ratio_score > 77 and partial_score > 77 and token_set_score > 80:
  18. return True
  19. else:
  20. return False
  21.  
  22.  
  23. def item_price_name(item: MenuItem) -> str:
  24. prices = ' '.join(['{} ({})'.format(i.value, i.type) if i.type else i.value for i in item.prices])
  25. return item.name + ' ' + prices if prices else item.name
  26.  
  27.  
  28. def _ratio(item_a: MenuItem, item_b: MenuItem) -> int:
  29. return fuzz.ratio(item_price_name(item_a), item_price_name(item_b))
  30.  
  31.  
  32. def _partial_ratio(item_a: MenuItem, item_b: MenuItem) -> int:
  33. return fuzz.partial_ratio(item_price_name(item_a), item_price_name(item_b))
  34.  
  35.  
  36. def _token_set_ratio(item_a: MenuItem, item_b: MenuItem) -> int:
  37. return fuzz.token_set_ratio(item_price_name(item_a), item_price_name(item_b))
  38.  
  39.  
  40. def score_items(item_a: MenuItem, item_b: MenuItem) -> ItemScore:
  41. return ItemScore((_ratio(item_a, item_b), _partial_ratio(item_a, item_b), _token_set_ratio(item_a, item_b)))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement