Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def filter_items(match_a: MenuItem, match_b: MenuItem) -> bool:
- """
- Filter items which are possible matches using three scoring methods.
- :param match_a:
- :param match_b:
- :return:
- """
- ratio_score, partial_score, token_set_score = score_items(match_a, match_b)
- if ratio_score > 90:
- return True
- elif token_set_score > 85 and (ratio_score + partial_score > 120):
- return True
- elif ratio_score > 85 and partial_score > 85:
- return True
- elif ratio_score > 77 and partial_score > 77 and token_set_score > 80:
- return True
- else:
- return False
- def item_price_name(item: MenuItem) -> str:
- prices = ' '.join(['{} ({})'.format(i.value, i.type) if i.type else i.value for i in item.prices])
- return item.name + ' ' + prices if prices else item.name
- def _ratio(item_a: MenuItem, item_b: MenuItem) -> int:
- return fuzz.ratio(item_price_name(item_a), item_price_name(item_b))
- def _partial_ratio(item_a: MenuItem, item_b: MenuItem) -> int:
- return fuzz.partial_ratio(item_price_name(item_a), item_price_name(item_b))
- def _token_set_ratio(item_a: MenuItem, item_b: MenuItem) -> int:
- return fuzz.token_set_ratio(item_price_name(item_a), item_price_name(item_b))
- def score_items(item_a: MenuItem, item_b: MenuItem) -> ItemScore:
- 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