"""Section 4b of the diagnostics report: dialect groups and contrasts with crawl membership decided per work (share of sentences overlapping the hypotactic crawl >= 0.15; the scores are bimodal), from the per-work figures in stats_v2.json (analyze_v2.py). Usage: python regroup_crawl_by_work.py stats_v2.json OUT_JSON""" import json, sys import numpy as np from scipy import stats r = json.load(open(sys.argv[1])); W = r['works'] for w in W: w['in_crawl'] = w['frac_sent_in_crawl'] >= 0.15 order = ['Epic', 'Doric/Aeolic lyric and bucolic', 'Ionic prose', 'Attic drama', 'Classical Attic prose', 'Koine', 'Atticist / Imperial and late literary prose', 'other'] out = {'threshold': 0.15, 'groups': [], 'contrasts': []} def pooled(ws): D = sum(w['dichrona'] for w in ws) if not D: return None return dict(works=len(ws), dichrona=D, cov=1 - sum(w['dichrona'] * (1 - w['cov']) for w in ws) / D, cov_nohyp=1 - sum(w['dichrona'] * (1 - w['cov_nohyp']) for w in ws) / D) for g in order: ws = [w for w in W if w['group'] == g] out['groups'].append(dict(group=g, all=pooled(ws), in_crawl=pooled([w for w in ws if w['in_crawl']]), outside=pooled([w for w in ws if not w['in_crawl']]))) DIAL = {'Epic', 'Doric/Aeolic lyric and bucolic', 'Ionic prose'}; ATT = {'Attic drama', 'Classical Attic prose'} LAT = {'Koine', 'Atticist / Imperial and late literary prose'} def samp(G, key, crawl): return np.array([w[key] for w in W if w['group'] in G and w['dichrona'] >= 2000 and (crawl is None or w['in_crawl'] == crawl)]) for label, A, B in [('dialectal vs Attic', DIAL, ATT), ('dialectal vs Koine/Atticist', DIAL, LAT), ('Attic vs Koine/Atticist', ATT, LAT), ('Epic vs Attic', {'Epic'}, ATT), ('Doric/Aeolic vs Attic', {'Doric/Aeolic lyric and bucolic'}, ATT), ('Ionic prose vs Classical Attic prose', {'Ionic prose'}, {'Classical Attic prose'})]: for key in ('cov', 'cov_nohyp'): for crawl in (None, False): a, b = samp(A, key, crawl), samp(B, key, crawl) if len(a) >= 3 and len(b) >= 3: t = stats.ttest_ind(a, b, equal_var=False); u = stats.mannwhitneyu(a, b) d = (a.mean() - b.mean()) / np.sqrt((a.var(ddof=1) + b.var(ddof=1)) / 2) out['contrasts'].append(dict(contrast=label, measure=key, works='outside crawl only' if crawl is False else 'all', n_a=len(a), n_b=len(b), mean_a=a.mean(), mean_b=b.mean(), diff_pp=100 * (a.mean() - b.mean()), t=t.statistic, p_welch=t.pvalue, p_mw=u.pvalue, cohens_d=d)) for lab, ws in (('in_crawl', [w for w in W if w['in_crawl']]), ('outside', [w for w in W if not w['in_crawl']])): D = sum(w['dichrona'] for w in ws) out['hypotactic_share_' + lab] = sum(w['dichrona'] * w['hyp_share'] for w in ws) / D out['crawl_work_list'] = [dict(work=w['work'], author=w['author'], frac=w['frac_sent_in_crawl'], cov=w['cov'], cov_nohyp=w['cov_nohyp'], hyp_share=w['hyp_share']) for w in sorted([w for w in W if w['in_crawl']], key=lambda x: -x['frac_sent_in_crawl'])] json.dump(out, open(sys.argv[2], 'w'), ensure_ascii=False, indent=1, default=float)