### Load Kyouro Semantics Input from URL on Page Load Source: https://github.com/nihongotopics/kyouro/blob/master/tools/semcalc.html This JavaScript snippet executes when the page loads. It checks for an 'input' parameter in the URL's query string. If found, it decodes the input, populates the user input field, and then triggers the parsing and display functions (parseAndShowLatex, parseAndShowTree, parseAndShowRoot) to process the loaded input. ```javascript window.onload = function() { const urlParams = new URLSearchParams(window.location.search); const input = urlParams.get('input'); if (input) { document.getElementById("userInput").value = decodeURIComponent(input); parseAndShowLatex(); parseAndShowTree(); parseAndShowRoot(); } }; ``` -------------------------------- ### Generate and Display Kyouro Semantics Root and LaTeX Preview Source: https://github.com/nihongotopics/kyouro/blob/master/tools/semcalc.html This function processes the user's input, parses it, and then uses the 'toRoot' function to extract the root structure. This root is displayed in the HTML element with the ID "root". Additionally, it generates a LaTeX preview image using 'latex.codecogs.com' by encoding the root structure and setting it as the source for an image element. ```javascript function parseAndShowRoot() { const input = document.getElementById("userInput").value; const parsed = parser(input); document.getElementById("root").innerHTML = toRoot(parsed); const img = document.getElementById('latex-preview'); img.src = 'https://latex.codecogs.com/png.latex?' + encodeURIComponent(toRoot(parsed)); img.alt = 'LaTeX preview'; } ``` -------------------------------- ### JavaScript RPN to LaTeX Converter for Linguistics Trees Source: https://github.com/nihongotopics/kyouro/blob/master/tools/RPN-LaTeX.html This JavaScript code implements the core logic for converting Reverse Polish Notation (RPN) strings into LaTeX representations of linguistics trees. It includes functions for retrieving and updating URL parameters, escaping text for LaTeX, and the main conversion logic that processes RPN tokens to build the LaTeX string. The converted LaTeX is then used to generate a preview image via an external service. ```javascript function getInputFromUrl() { const params = new URLSearchParams(window.location.search); return params.get('input') || ''; } function updateUrl(text) { const params = new URLSearchParams(window.location.search); if (text) params.set('input', text); else params.delete('input'); const newUrl = window.location.pathname + '?' + params.toString(); window.history.replaceState(null, '', newUrl); } function escapeLatexText(str) { return str.replace(/(\[#$%&\_{}~^\\\\])/g, "\\\\$1"); } function convert() { const textarea = document.getElementById('input'); const text = textarea.value.trim(); const rawTokens = text.match(/\\\\|\\[\\[^\\\\]\\]+\\]|\\[^\\\\\\s\\[\\]+/g) || []; const stack = []; rawTokens.forEach(tok => { const labelMatch = tok.match(/^\\[(\\[^\\\\]\\]+)\\]$/); if (labelMatch) { if (stack.length === 0) return; const lbl = labelMatch[1]; let latexLabel; if (lbl.includes('\\\\') || lbl.includes('/') || lbl.includes('|')) { latexLabel = lbl.split('').map(ch => { if (ch === '\\\\') return ' \\\\backslash '; if (ch === '/') return '/'; if (ch === '|') return '|'; return ch; }).join(''); } else { latexLabel = '\\\\text{' + escapeLatexText(lbl) + '}'; } const item = stack.pop(); stack.push('\\\\overset{' + latexLabel + '}{' + item + '}'); } else if (tok === '\\\\') { const right = stack.pop() || ''; const left = stack.pop() || ''; stack.push('\\\\overline{' + left + '\\\\text{ }' + right + '}'); } else { stack.push('\\\\text{' + escapeLatexText(tok) + '}'); } }); const latex = stack.length === 1 ? stack[0] : ''; document.getElementById('output').textContent = latex || '⚠️ malformed RPN'; const img = document.getElementById('latex-preview'); if (latex) { img.src = 'https://latex.codecogs.com/png.latex?' + encodeURIComponent(latex); img.alt = 'LaTeX preview'; } else { img.src = ''; img.alt = 'No preview'; } updateUrl(text); } window.onload = () => { const initial = getInputFromUrl(); const textarea = document.getElementById('input'); textarea.value = initial || 'I[N] love[V] dogs[N]\\\\\\[VP]\\\\\\[S]'; convert(); }; ``` -------------------------------- ### Parse Kyouro Semantics Input and Generate LaTeX Output Source: https://github.com/nihongotopics/kyouro/blob/master/tools/semcalc.html This function takes the user's input, attempts to parse it using a 'parser' function, and then maps the parsed objects to their LaTeX representation. It also updates the browser's URL with the encoded input, allowing for sharing. Error handling is included to catch parsing issues. The final LaTeX output is displayed on the page. ```javascript function parseAndShowLatex() { const input = document.getElementById("userInput").value; let resultLatex = []; try { const parsed = parser(input); resultLatex = parsed.map(obj => obj.latex()); // Save input to URL search params const newUrl = new URL(window.location); newUrl.searchParams.set('input', encodeURIComponent(input)); window.history.pushState({}, '', newUrl); } catch (e) { resultLatex = ["Error: " + e.message]; } document.getElementById("output").innerHTML = resultLatex.join(" "); } ``` -------------------------------- ### Generate and Display Kyouro Semantics Derivative Tree Source: https://github.com/nihongotopics/kyouro/blob/master/tools/semcalc.html This function retrieves the user's input, parses it, and then uses the 'toTree' function to convert the parsed structure into a derivative tree representation. The resulting tree is then displayed in the HTML element with the ID "tree". ```javascript function parseAndShowTree() { const input = document.getElementById("userInput").value; document.getElementById("tree").innerHTML = toTree(parser(input)); } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.