#!/usr/bin/env python3
"""python3 score.py raw/<file>.jsonl : brand mention / first-mention / own-site citation counts -> results.csv"""
import csv, json, re, sys, urllib.parse, collections
# brand: (regex over answer text, own domains)
B = {  # Chinese brands only; Kubota / John Deere / Iseki etc. are not counted
 'YTO (First Tractor / Dongfanghong)': (r'\bYTO\b|First Tractor|Dongfanghong|东方红|一拖', ['ytocorp.com', 'yto.com.cn']),
 'LOVOL (Weichai Lovol)': (r'Lovol|雷沃', ['lovolglobal.com', 'lovol.com', 'lovol.co.th', 'lovol.vn', 'lovolafrica.com', 'lovolbrasilcf.com']),
 'Zoomlion Agriculture': (r'Zoomlion|Zhonglian|中联', ['zoomlion-ag.com', 'zoomlion.com']),
 'Dongfeng (Changzhou Dongfeng)': (r'Dongfeng|东风', ['df-tractor.com', 'dftractor.com', 'dfamgc.com']),
 'Changfa': (r'Changfa|常发', ['changfanz.com']),
 'World (Jiangsu World / FMWORLD)': (r'Jiangsu World|FM ?WORLD|World Agricultur|World Group|WORLD Agriculture|沃得|\bWode\b', ['fmworldagri.com', 'worldnyjx.com']),
 'Shifeng (时风)': (r'Shifeng|时风', []),
 'Wuzheng (五征)': (r'Wuzheng|五征', ['wuzheng.com', 'wuzheng.com.cn', 'chinawuzheng.com']),
 'Yucheng Hengshing': (r'Hengshing', ['chinaplow.com']),  # site named under its heading in the answers
 'Shandong Tiansheng': (r'Tiansheng', ['cnharrow.com']),  # site named under its heading in the answers
 'Jinma': (r'Jinma|金马', []),
 'FOTMA': (r'FOTMA', ['fotma.com']),
 'Xingguang': (r'Xingguang|星光', []),
 'Yucheng Leyuan': (r'Leyuan', ['ycleyuan.com']),
 'Yucheng Yili': (r'Yucheng Yili', ['ycyl2013.en.made-in-china.com']),
 'Foton': (r'\bFoton\b', []),
 'DJI Agriculture': (r'\bDJI\b|大疆', []),
 'XAG': (r'\bXAG\b|极飞', []),
 'Shandong Juming': (r'Juming', ['sdjuming.com']),
 'Yongmeng': (r'Yongmeng', ['yongmengjixie.com']),
 'Yucheng Zeyi': (r'Zeyi', ['zeyimachine.com']),
 'Shandong Yuntai': (r'Yuntai', ['sdyuntai.com']),
 'Xuzhou Zhongyang': (r'Zhongyang', ['zhongyangharrow.com']),
 'Hebei Nonghaha': (r'Nonghaha', ['nonghaha.com']),
 'Linhai': (r'\bLinhai\b', ['linhaiagri.com']),
 'Hunan Sunfield': (r'Sunfield', []),
 'DAYU Irrigation': (r'\bDAYU\b', ['cndayu.com']),
 'Henan Qianli': (r'Qianli', ['qln-tractor.com']),
 'Shandong Dahua': (r'Shandong Dahua|Dahua Machinery', ['agrodahua.com']),
 'Henan Haofeng': (r'Haofeng', []),
 'Zhejiang Xinglaihe': (r'Xinglaihe', []),
 'Anhui Zhengyang': (r'Zhengyang', ['zhengyanggroup.com']),
 'Jiangsu SUNYA': (r'SUNYA', ['cnsunya.com']),
 'Nantong FLW': (r'Nantong FLW', []),
 'Weima': (r'\bWeima\b', ['weimapower.com']),
 'Linyi Wali': (r'Linyi Wali|\bWali\b', ['walijixie.cn']),
 'Zhengzhou Masely': (r'Masely', ['maselyagro.com']),
 'Chinadrip': (r'Chinadrip', ['chinadrip.com']),
 'SANGAO': (r'SANGAO', ['sangaoricemill.com']),
}
L = [json.loads(l) for l in open(sys.argv[1])]
n = len(L); rows = collections.defaultdict(lambda: {'mention': 0, 'first': 0, 'cited': 0, 'ids': []})
for d in L:
    a = d['answer']; first = None
    doms = {urllib.parse.urlsplit(u).netloc.replace('www.', '') for u in d['links'] or []}
    for b, (rx, ds) in B.items():
        m = re.search(rx, a, re.I if b not in ('SANY', 'ZENITH') else 0)
        if m:
            rows[b]['mention'] += 1; rows[b]['ids'].append(d['id'])
            if first is None or m.start() < first[1]: first = (b, m.start())
        if any(h == x or h.endswith('.' + x) for h in doms for x in ds): rows[b]['cited'] += 1  # subdomains count
    if first: rows[first[0]]['first'] += 1
out = sorted(rows.items(), key=lambda x: (-x[1]['mention'], -x[1]['first']))
with open('results.csv', 'w', newline='') as f:
    w = csv.writer(f); w.writerow(['rank', 'brand', 'mentioned_in', 'mention_rate', 'first_mentioned_in', 'first_rate', 'own_site_cited_in', 'prompt_ids'])
    for i, (b, r) in enumerate(out, 1):
        w.writerow([i, b, r['mention'], f"{r['mention']/n:.0%}", r['first'], f"{r['first']/n:.0%}", r['cited'], ' '.join(r['ids'])])
        print(f"{i:>2} {b:42} {r['mention']:>2}/{n} {r['mention']/n:>4.0%}  first {r['first']:>2}  cited {r['cited']:>2}")
