#!/usr/bin/env python3
# FreeDict/Arabeyes data converter, modified 2026-09-28 for New York News.
# This converter and its generated data are offered under GPL-2.0-or-later.
# Usage: python convert-dictionary.py eng-ara.tei eng-ar.json
import sys,json,xml.etree.ElementTree as E
from pathlib import Path
ns={'t':'http://www.tei-c.org/ns/1.0'}
tree=E.parse(sys.argv[1]);dictionary={}
for entry in tree.findall('.//t:entry',ns):
 forms=[x.text.strip().lower().replace('’',"'") for x in entry.findall('t:form/t:orth',ns) if x.text]
 meanings=[''.join(x.itertext()).strip() for x in entry.findall('.//t:cit[@type="trans"]/t:quote',ns)]
 for word in forms:
  out=dictionary.setdefault(word,[])
  for meaning in meanings:
   if meaning and meaning not in out:out.append(meaning)
Path(sys.argv[2]).write_text(json.dumps(dictionary,ensure_ascii=False,separators=(',',':'),sort_keys=True),encoding='utf-8')
