45 lines
3.7 KiB
Python
45 lines
3.7 KiB
Python
"""How often random card pairs share cheap relations. Usage: python3 -I connected.py <card-sample.json>"""
|
|
import json, sys, re, itertools, collections, importlib.util, os
|
|
cards = json.load(open(sys.argv[1]))['cards']
|
|
spec = importlib.util.spec_from_file_location('ty', os.path.join(os.path.dirname(os.path.abspath(__file__)), 'types.py'))
|
|
ty = importlib.util.module_from_spec(spec); spec.loader.exec_module(ty)
|
|
GENERIC = re.compile(r'stubs|living people|\d+ (births|deaths)$|articles|wikidata|pages', re.I)
|
|
COUNTRIES = ['United States','American','England','English','British','France','French','Germany','German','Japan','Japanese','China','Chinese',
|
|
'India','Indian','Brazil','Poland','Polish','Italy','Italian','Spain','Spanish','Austria','Canada','Canadian','Australia','Australian','Russia','Russian',
|
|
'Mexico','Mexican','Iran','Pakistan','Bangladesh','Sweden','Swedish','Norway','Estonia','Ukraine','Croatia','Philippines','Cuba','Israel','Portugal','Scotland','Wales','Ireland','Kazakhstan','Malaysia','Argentin','Belgi','Iceland','Bermuda','Thailand','Singapore','Sri Lanka','Slovenia','Moldova','Cyprus','Macedonia','Mauritania','Congo','Gibraltar','Hong Kong','New Zealand','Korea','Indonesia','Aceh']
|
|
NORM = {'American':'United States','English':'England','British':'United Kingdom','England':'United Kingdom','Scotland':'United Kingdom','Wales':'United Kingdom',
|
|
'French':'France','German':'Germany','Japanese':'Japan','Chinese':'China','Indian':'India','Polish':'Poland','Italian':'Italy','Spanish':'Spain',
|
|
'Canadian':'Canada','Australian':'Australia','Russian':'Russia','Mexican':'Mexico','Swedish':'Sweden'}
|
|
Y = re.compile(r'\b(1[0-9]{3}|20[0-2][0-9])')
|
|
def feats(c):
|
|
txt = (c['description'] or '') + ' | ' + ' | '.join(c['categories'])
|
|
ctry = {NORM.get(k, k) for k in COUNTRIES if k in txt}
|
|
cents = {int(y) // 100 for y in Y.findall(c['title'] + ' ' + txt)}
|
|
cats = {x for x in c['categories'] if not GENERIC.search(x)}
|
|
p31 = {i['id'] for i in c['instanceOf']}
|
|
return dict(ctry=ctry, cent=cents, cats=cats, allcats=set(c['categories']), p31=p31, type=ty.classify(c)[0])
|
|
F = [feats(c) for c in cards]; pairs = list(itertools.combinations(range(len(cards)), 2)); N = len(pairs)
|
|
def rate(f):
|
|
k = sum(1 for i, j in pairs if f(F[i], F[j])); return f'{k}/{N} ({100*k/N:.2f}%)'
|
|
print('pairs', N)
|
|
print('share any category (incl. generic)', rate(lambda a, b: a['allcats'] & b['allcats']))
|
|
print('share specific category', rate(lambda a, b: a['cats'] & b['cats']))
|
|
print('share P31 class', rate(lambda a, b: a['p31'] & b['p31']))
|
|
print('share P31 excl human/taxon', rate(lambda a, b: a['p31'] & b['p31'] - {'Q5', 'Q16521'}))
|
|
print('share country (text proxy for P17/P27)', rate(lambda a, b: a['ctry'] & b['ctry']))
|
|
print('share century (text proxy for dates)', rate(lambda a, b: a['cent'] & b['cent']))
|
|
print('share type', rate(lambda a, b: a['type'] == b['type']))
|
|
print('country OR century OR specific cat', rate(lambda a, b: (a['ctry'] & b['ctry']) or (a['cent'] & b['cent']) or (a['cats'] & b['cats'])))
|
|
print('cards with >=1 country', sum(1 for f in F if f['ctry']), 'with >=1 century', sum(1 for f in F if f['cent']))
|
|
# deck-level: chance that a random 10-card hand-pair (10 vs 10, own deck) has >=1 connected pair within a deck
|
|
import random; random.seed(1)
|
|
def deck_hits(pred, size=10, trials=2000):
|
|
h = 0
|
|
for _ in range(trials):
|
|
s = random.sample(range(len(F)), size)
|
|
if any(pred(F[i], F[j]) for i, j in itertools.combinations(s, 2)): h += 1
|
|
return f'{100*h/trials:.0f}%'
|
|
pc = lambda a, b: (a['ctry'] & b['ctry'])
|
|
print('10-card deck has >=1 same-country pair', deck_hits(pc), '| specific category', deck_hits(lambda a, b: a['cats'] & b['cats']),
|
|
'| country-or-century', deck_hits(lambda a, b: (a['ctry'] & b['ctry']) or (a['cent'] & b['cent'])))
|