"""old_to_translation.py RAW JAHR -> translations/_alt.json + Pruefbericht Wandelt Olivers alte edwiki-Uebersetzungen (aus match.json) ins Paketformat und vergleicht sie mit dem Englischen: Zahlen, Eigennamen, Absatzzahl, Laenge. Auffaellige Meldungen landen im Bericht zur Durchsicht.""" import json, re, sys, csv from pathlib import Path R = Path(sys.argv[1]); year = int(sys.argv[2]) EN, DE = {}, {} for L, D in (('EN', EN), ('DE', DE)): for f in (R / L).glob('*.json'): if int(f.stem[:4]) - 1286 == year: for a in json.load(open(f, encoding='utf-8'))['articles']: D[a['id']] = a # Meldungen mit echtem Frontier-DE brauchen keine eigene Uebersetzung def german_share(t): w = re.findall(r'\b\w+\b', (t or '').lower()) return sum(x in ('der', 'die', 'und', 'das', 'ist', 'nicht', 'mit', 'den', 'von', 'zu', 'ein', 'eine', 'wurde', 'sich') for x in w) / max(1, len(w)) def frontier_de(fid): # echter deutscher Frontier-Text (nicht der englische unter DE) return fid in DE and german_share(DE[fid]['body']) >= 0.02 and (fid not in EN or DE[fid]['body'][:150] != EN[fid]['body'][:150]) idx = [r for r in csv.DictReader(open(R / 'index.csv', encoding='utf-8'), delimiter=';') if int(r['date'][:4]) == year] _m = json.load(open('match.json', encoding='utf-8')) # Nachtraegliche Korrekturen der Zuordnung (von Hand geprueft): "Datum|alter Titel" -> Frontier-ID if Path('match_fix.json').exists(): FIX = json.load(open('match_fix.json', encoding='utf-8')) for o in _m: k = o['date'] + '|' + o['title'] if k in FIX: o['fid'] = FIX[k] M = {o['fid']: o for o in _m if o.get('fid')} SECTOR_DE = {'Plejaden': 'Pleiades', 'Hyaden': 'Hyades', 'Witch-Head': 'Witch Head'} def fix_sector(t): """Systemnamen bleiben englisch: 'Plejaden-Sektor JC-U B3-2' / 'IC 2391 Sektor GW-V b2-4' -> '... Sector JC-U b3-2'""" def rep(m): name = SECTOR_DE.get(m.group(1), m.group(1).replace('-', ' ')) cls = m.group(2) return f"{name} Sector {cls[:5]}{cls[5].lower()}{cls[6:]}" t = re.sub(r"\b([A-ZÄÖÜ][\w’']*(?:[ -][A-ZÄÖÜ][\w’']*)*)-Sektor ([A-Z]{2}-[A-Z] [a-hA-H]\d[\w-]*)", rep, t) t = re.sub(r"\b((?:[A-Z][\w'’]*|\d+)(?: (?:[A-Z][\w'’]*|\d+))*) Sektor ([A-Z]{2}-[A-Z] [a-hA-H]\d[\w-]*)", rep, t) return t def plain(t): out = [] for line in t.split('\n'): line = line.rstrip() if re.fullmatch(r'>\s*', line) or re.fullmatch(r'\s*]*>\s*', line): continue line = re.sub(r']*>\s*', '', line) line = re.sub(r'^[<>]\s+', '', line) line = re.sub(r'^=+\s*(.*?)\s*=+$', r'\1', line) line = re.sub(r'^>\s?', '', line) line = re.sub(r'^\s+[*-]\s+', '• ', line) line = line.replace('**', '') line = re.sub(r'__(.+?)__', r'\1', line) line = re.sub(r'//(.+?)//', r'*\1*', line) line = re.sub(r'\\\\\s*$', '', line) # Systemnamen bleiben englisch: "IC 2391 Sektor GW-V b2-4" -> "IC 2391 Sector GW-V b2-4" line = fix_sector(line) if line.strip(): out.append(line.strip()) return '\n'.join(out) NUM = re.compile(r'\d[\d,.]*') def nums(t): return {re.sub(r'[,.]', '', n).lstrip('0') for n in NUM.findall(t)} - {''} def names(t): c = set(re.findall(r"\b([A-Z][a-z’'-]+(?: [A-Z][a-z’'-]+)+)", t)) return {x for x in c if not x.split()[0] in ('The', 'A', 'An', 'In', 'This', 'Pilots', 'At', 'Some', 'Many', 'As', 'We', 'It', 'If')} out, rep = {}, [] for i, r in enumerate(idx): if frontier_de(r['frontier_id']): continue e = EN.get(r['frontier_id']) o = M.get(r['frontier_id']) if e is None: # Meldung ohne englische Fassung: Olivers Text uebernehmen, Abgleich nur von Hand moeglich if o: out[r['frontier_id']] = dict(source='eigene', title=o['title'].strip(), body=plain(o['body'])) rep.append(f'#{i} KEIN EN (nur {r["languages"]}), ' + ('Olivers Text übernommen, nicht abgeglichen' if o else 'FEHLT (neu übersetzen)') + f': {r["title_en"]}') continue if not o: rep.append(f'#{i} FEHLT (neu übersetzen): {e["title"]}'); continue de = plain(o['body']); out[r['frontier_id']] = dict(source='eigene', title=o['title'].strip(), body=de) flags = [] mn = sorted(n for n in nums(e['body']) - nums(de) if len(n) > 1) if mn: flags.append('Zahlen fehlen: ' + ' '.join(mn)) # Genitiv-s weg; Institutionen/Titel werden uebersetzt und zaehlen nicht INST = r'^(Federal|Imperial|Alliance|Galactic|Pleiades|Witch|Interstellar|Pilots|Advanced|Reactive|Military|Battle|Performance|New|Senate|Defence|Fleet|Civilian|Other|Although|While|Obviously|Prime|President|Councillor|Admiral|Emperor|Imperator|Commander|Senator|Lady|Mr|Ms|Agronomic|Onion|Cerberus|Alliance)\b' def core(n): return re.sub(r"[’']s?$", '', n) nm = sorted({core(n) for n in names(e['body'])} - {''}) nm = [n for n in nm if not re.match(INST, n) and n not in de and n.split()[-1] not in de] if nm: flags.append('Namen fehlen: ' + '; '.join(nm)) pe, pd = len([p for p in e['body'].split('\n') if p.strip()]), len(de.split('\n')) if abs(pe - pd) > 1: flags.append(f'Absätze EN {pe} / DE {pd}') q = len(de) / max(1, len(e['body'])) if q < 0.95 or q > 1.6: flags.append(f'Länge DE/EN {q:.2f}') if flags: rep.append(f'#{i} {r["date"]} {e["title"]} | ' + ' | '.join(flags)) Path('translations').mkdir(exist_ok=True) json.dump(out, open(f'translations/{year}_alt.json', 'w', encoding='utf-8'), ensure_ascii=False, indent=1) print(len(out), 'alte Übersetzungen;', len(rep), 'auffällig/fehlend') print('\n'.join(rep))