"""Generate deterministic browser fixtures using the production iframe renderer.""" import json import sys from pathlib import Path ROOT = Path(__file__).resolve().parents[2] sys.path.insert(0, str(ROOT)) from visualizer.template import PARSER_URL, render_conllu_visualizer_html, render_side_by_side_visualizers_html ENGLISH = '# sentence-label English\n# visual-style 2 1 nsubj color:red\n1\tThey\tthey\tPRON\t_\t_\t2\tnsubj\t_\t_\n2\tbuy\tbuy\tVERB\t_\t_\t0\troot\t_\t_\n3\tbooks\tbook\tNOUN\t_\t_\t2\tobj\t_\t_\n' GERMAN = '# sentence-label German\n1\tBücher\tBuch\tNOUN\t_\t_\t2\tnsubj\t_\t_\n2\tüberzeugen\tüberzeugen\tVERB\t_\t_\t0\troot\t_\t_\n' ESCAPING = '1\t&\t_\tNOUN\t_\t_\t0\troot\t_\t_\n' MULTIWORD = '1-2\tcan’t\t_\t_\t_\t_\t_\t_\t_\t_\n1\tca\tcan\tAUX\t_\t_\t3\taux\t_\t_\n2\tn’t\tnot\tPART\t_\t_\t3\tadvmod\t_\t_\n3\tgo\tgo\tVERB\t_\t_\t0\troot\t_\t_\n' EMPTY = '1\tHello\thello\tINTJ\t_\t_\t0\troot\t_\t_\n1.1\tworld\tworld\tNOUN\t_\t_\t_\t_\t1:dep\t_\n' MULTISENTENCE = ENGLISH + '\n' + GERMAN WRAPPED = '\n'.join( f'{i}\tword{i}\t_\tNOUN\t_\t_\t{i - 1}\t{"root" if i == 1 else "conj"}\t_\t_' for i in range(1, 31) ) + '\n' CROWDED = "\n".join( "\t".join(map(str, [i, word, "_", pos, "_", "_", head, rel, "_", "_"])) for i, (word, pos, head, rel) in enumerate([ ("The", "DET", 2, "det"), ("dog", "NOUN", 7, "nsubj"), ("that", "PRON", 4, "nsubj"), ("chased", "VERB", 2, "acl:relcl"), ("the", "DET", 6, "det"), ("cat", "NOUN", 4, "obj"), ("barked", "VERB", 0, "root"), ("loudly", "ADV", 7, "advmod"), (".", "PUNCT", 7, "punct"), ], 1) ) + "\n" fixtures = dict(english=ENGLISH, german=GERMAN, escaping=ESCAPING, multiword=MULTIWORD, empty=EMPTY) def build(): preview = 'Production renderer fixtures' preview += render_side_by_side_visualizers_html(ENGLISH, 'English', GERMAN, 'German') preview += render_conllu_visualizer_html(ESCAPING, 'escaping', 'Escaping') preview += render_conllu_visualizer_html(MULTIWORD, 'multiword', 'Multiword') preview += render_conllu_visualizer_html(EMPTY, 'empty', 'Empty node') preview += render_conllu_visualizer_html(CROWDED, 'crowded', 'Crowded label markers', show_help=True) preview += render_conllu_visualizer_html(MULTISENTENCE, 'multisentence', 'Multiple sentences') preview += '
' + render_conllu_visualizer_html(WRAPPED, 'wrapped', 'Wrapped sentence') + '
' output = ROOT / 'output' / 'playwright' output.mkdir(parents=True, exist_ok=True) (output / 'preview.html').write_text(preview) api = '''Deployed parser API
Loading
'''.replace('PARSER_URL', PARSER_URL).replace('FIXTURES', json.dumps(fixtures).replace('<', '\\u003c')) (output / 'api.html').write_text(api) print(output) if __name__ == "__main__": build()