import pymorphy2 morph = pymorphy2.MorphAnalyzer() def counter(s: str) -> dict: d = {} for i in s: if i not in d: d[i] = 0 d[i] += 1 return d def sweet_check(s1: str, s2: str): STOP_PUNCT = list(',./!@#$%^&*()_+=-<>?\|{}[]`~/') STOP = set( ["скидка", "скидкой", "скидки", "скидке", "скидкой", "скидке", "недорого", "дешево", "в", "на", "для", "о", "у", "и", "с", "из"] + STOP_PUNCT) s1 = s1.lower() s2 = s2.lower() set_s1 = set(s1.split(' ')) - STOP set_s2 = set(s2.split(' ')) - STOP if set_s1 == set_s2: return False diff_s1 = ' '.join(list(set_s1 - set_s2)) diff_s2 = ' '.join(list(set_s2 - set_s1)) if len(diff_s1) == 0: # return diff_s2 return True if len(diff_s2) == 0: return False return True def check(s1: str, s2: str, debag=False, morph=morph) -> float: STOP_PUNCT = list(',./!@#$%^&*()_+=-<>?\|{}[]`~/') STOP = set( ["скидка", "скидкой", "скидки", "скидке", "скидкой", "скидке", "недорого", "дешево", "в", "на", "для", "о", "у", "и", "с", "из"] + STOP_PUNCT) s1 = s1.lower() s2 = s2.lower() s1 = [morph.parse(i)[0].normal_form for i in s1.split(' ')] s2 = [morph.parse(i)[0].normal_form for i in s2.split(' ')] set_s1 = set(s1) - STOP set_s2 = set(s2) - STOP if set_s1 == set_s2: return False diff_s1 = ' '.join(list(set_s1 - set_s2)) diff_s2 = ' '.join(list(set_s2 - set_s1)) if debag: print(s1) print(s2) if len(diff_s1) == 0: return True if len(diff_s2) == 0: return False dt = {len(diff_s1): diff_s1, len(diff_s2): diff_s2} c = 0 max_s, min_s = dt[max(len(diff_s1), len(diff_s2))], dt[min(len(diff_s1), len(diff_s2))] c_s1 = counter(min_s) c_s2 = counter(max_s) for i in min_s: if i in c_s2 and c_s2[i] > 0: c += 1 c_s2[i] -= 1 else: c -= 1 if len(diff_s2) == len(diff_s1): c -= 1 if debag: print(c / len(min_s)) if c / len(min_s) < 1.0: return True return False