Lab2 / index.html
Mauro1455's picture
Upload index.html
ea14abc verified
Raw History Blame Contribute Delete
2.41 kB
<!DOCTYPE html>
<html lang="sv">
<head>
<meta charset="UTF-8">
<title>Selma Lagerlöf — Predicción de palabra (trigramas)</title>
<style>
body { font-family: sans-serif; max-width: 600px; margin: 40px auto; padding: 0 20px; }
input { width: 100%; padding: 10px; font-size: 16px; box-sizing: border-box; }
#result { margin-top: 20px; font-size: 18px; min-height: 1.5em; }
#status { color: #888; font-size: 14px; }
</style>
</head>
<body>
<h1>Selma Lagerlöf — Predicción de la siguiente palabra</h1>
<p>Modelo de trigramas entrenado sobre el corpus de Selma Lagerlöf
(Assignment 2, Language Technology, Lund University).</p>
<p>Escribe al menos dos palabras en sueco (p. ej. "det var"):</p>
<input id="textbox" type="text" placeholder="det var" autocomplete="off">
<div id="status">Cargando trigramas...</div>
<div id="result"></div>
<script>
let trigrams = new Map(); // key "w1|w2" -> array of [w3, count]
fetch('trigrams.jsonl')
.then(r => r.text())
.then(text => {
const lines = text.split('\n').filter(l => l.trim().length > 0);
for (const line of lines) {
const data = JSON.parse(line);
if (data.ngram.length === 3) {
const key = data.ngram[0] + '|' + data.ngram[1];
if (!trigrams.has(key)) trigrams.set(key, []);
trigrams.get(key).push([data.ngram[2], data.count]);
}
}
document.getElementById('status').textContent =
'Trigramas cargados: ' + lines.length.toLocaleString();
})
.catch(err => {
document.getElementById('status').textContent =
'Error cargando trigrams.jsonl: ' + err;
});
document.getElementById('textbox').addEventListener('input', function () {
const words = this.value.toLowerCase().trim().split(/\s+/);
const resultDiv = document.getElementById('result');
if (words.length < 2 || words[0] === '') {
resultDiv.textContent = '';
return;
}
const w1 = words[words.length - 2];
const w2 = words[words.length - 1];
const candidates = trigrams.get(w1 + '|' + w2) || [];
const sorted = candidates.slice().sort((a, b) => {
if (b[1] !== a[1]) return b[1] - a[1]; // frequency desc
return a[0].localeCompare(b[0]); // then alphabetical
});
const top5 = sorted.slice(0, 5).map(c => c[0]);
resultDiv.textContent = top5.length > 0
? 'Predicciones: ' + top5.join(', ')
: 'No hay predicciones para ese contexto.';
});
</script>
</body>
</html>