Simon Clematide
Add tree resize handle, pedagogical guides, and UI/comparison enhancements
115fba1
Raw History Blame Contribute Delete
4.35 kB
"""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</textarea></script>&\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 = '<!doctype html><meta charset="utf-8"><title>Production renderer fixtures</title>'
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 += '<div style="width:500px">' + render_conllu_visualizer_html(WRAPPED, 'wrapped', 'Wrapped sentence') + '</div>'
output = ROOT / 'output' / 'playwright'
output.mkdir(parents=True, exist_ok=True)
(output / 'preview.html').write_text(preview)
api = '''<!doctype html><meta charset="utf-8"><title>Deployed parser API</title><pre id="status">Loading</pre>
<script src="PARSER_URL"></script><script>
window.results = [];
const fixtures = FIXTURES;
function check(ok, name) { if (!ok) throw Error(name); results.push(name); }
try {
check(!!window.ConllU.Document, 'browser API');
for (const [name, input] of Object.entries(fixtures)) {
const doc = new ConllU.Document(); doc.parse(input, () => {});
const data = doc.toBrat();
check(!doc.error && !data.error, name + ' parses');
const expected = {english:3, german:2, escaping:1, multiword:3, empty:1}[name];
check(data.entities.length === expected, name + ' entity count');
check(data.entities.every(e => e[2].every(s => s[0] >= 0 && s[1] <= data.text.length)), name + ' spans');
if (name === 'english') check(data.relations.some(r => r[1] === 'nsubj'), 'nsubj relation');
if (name === 'empty') check(doc.toBrat(undefined, true).entities.length === 2, 'include empty');
if (name === 'escaping') check(data.text.includes(fixtures.escaping.split('\\t')[1]), 'literal text');
}
const bad = new ConllU.Document(); bad.parse('not conllu', () => {});
check(bad.error || bad.toBrat().error, 'invalid input reported');
document.getElementById('status').textContent = 'PASS ' + results.length;
} catch (error) { document.getElementById('status').textContent = 'FAIL ' + error.message; }
</script>'''.replace('PARSER_URL', PARSER_URL).replace('FIXTURES', json.dumps(fixtures).replace('<', '\\u003c'))
(output / 'api.html').write_text(api)
print(output)
if __name__ == "__main__":
build()