"""Count majority structure among mixed inherited Y/N tags, never factual truth.
Usage: python upgrade-analysis.py /path/to/QA_Beauty.json.gz /path/to/QA_Clothing_Shoes_and_Jewelry.json.gz
"""
import ast,gzip,json,sys
from collections import Counter
out=[]
for source,category in zip(sys.argv[1:],['Beauty','Clothing Shoes and Jewelry']):
 c=Counter()
 with gzip.open(source,'rt') as f:
  for line in f:
   product=ast.literal_eval(line)
   for q in product['questions']:
    if q.get('questionType')!='yes/no':continue
    labels=[a.get('answerType') for a in q['answers'] if a.get('answerType') in ('Y','N')]
    if len(labels)<2:continue
    c['eligible']+=1
    if {'Y','N'}<=set(labels):
     y,n=labels.count('Y'),labels.count('N');c['mixed']+=1;c['tied']+=y==n;c['strict_majority']+=y!=n;c['majority_at_least_75_percent']+=max(y,n)/len(labels)>=.75
 assert c['eligible']==(1922 if category=='Beauty' else 990)
 assert c['mixed']==(444 if category=='Beauty' else 202)
 out.append({'category':category,**c})
assert len(out)==2
print(json.dumps(out,indent=2))
