"""Exact finite-universe minwise demonstration; no probabilistic hash implementation."""
from itertools import permutations
from fractions import Fraction as F
import random,json
def shingles(text):
    tokens=text.split();result=set(zip(tokens,tokens[1:]))
    if not result:raise ValueError('at least two split tokens required')
    return result
def signature(items,orders):
    if not items or not orders:raise ValueError('nonempty set and permutations required')
    universe=set(orders[0])
    if not items<=universe or any(len(o)!=len(universe) or set(o)!=universe for o in orders):raise ValueError('same complete universe required')
    return [next(x for x in order if x in items) for order in orders]
def demo():
    a=shingles('این کتاب خوب است');b=shingles('این کتاب خوب بود');u=sorted(a|b)
    orders=list(permutations(u));sa=signature(a,orders);sb=signature(b,orders)
    exact=F(len(a&b),len(a|b));allmatch=F(sum(x==y for x,y in zip(sa,sb)),len(orders))
    sampled=random.Random(17).sample(orders,8);aa=signature(a,sampled);bb=signature(b,sampled)
    estimate=F(sum(x==y for x,y in zip(aa,bb)),len(sampled))
    assert len(u)==4 and exact==allmatch==F(1,2) and len(orders)==24
    assert signature(a,orders)==signature(set(a),orders)
    rejected=0
    for operation in [lambda:shingles('کتاب'),lambda:signature(set(),orders),lambda:signature(a,[tuple(u[:-1])])]:
        try:operation()
        except ValueError:rejected+=1
        else:raise AssertionError('invalid domain accepted')
    return {'authored_text_A':'این کتاب خوب است','authored_text_B':'این کتاب خوب بود','shingles_A':sorted(a),'shingles_B':sorted(b),'universe':u,'exact_jaccard':str(exact),'all_permutations':24,'all_matching_slots':12,'full_minwise_match_fraction':str(allmatch),'eight_uniformly_sampled_orders_seed':17,'sampled_orders':sampled,'eight_slot_estimate':str(estimate),'invalid_domain_rejections':rejected,'cryptographic_hash_or_LSH_executed':False,'semantic_duplicate_detection_claimed':False}
if __name__=='__main__':print(json.dumps(demo(),ensure_ascii=False,indent=2))
