File size: 2,411 Bytes
ea14abc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
218c8fa
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
<!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>