Skip to content

Instantly share code, notes, and snippets.

@felquis
Created January 22, 2026 20:38
Show Gist options
  • Select an option

  • Save felquis/902fe9e2a0da901d65cae6aa155f0d47 to your computer and use it in GitHub Desktop.

Select an option

Save felquis/902fe9e2a0da901d65cae6aa155f0d47 to your computer and use it in GitHub Desktop.
Predict the next letter in `O, T, T, F, F, S, S, E, ?` using bigram model of `onetwothreefourfivesixseveneightnineteneleventwelvethirteen`
// Hi my name is Ofelquis and I've been studying equations and algorithms with Grok,
// I didn't write this code, it's just food for thought enjoy and read more about the works of shannon claude
// Bigram-based next-letter predictor with probability distribution
// Trains on a corpus, computes P(next | current), logs probs for ALL A-Z letters, then the highest
function buildBigramModel(corpus) {
const counts = {};
const totals = {};
for (let i = 0; i < corpus.length - 1; i++) {
const current = corpus[i].toUpperCase();
const next = corpus[i + 1].toUpperCase();
// Only include letter-to-letter transitions (A-Z)
if (/[A-Z]/.test(current) && /[A-Z]/.test(next)) {
if (!counts[current]) {
counts[current] = {};
totals[current] = 0;
}
counts[current][next] = (counts[current][next] || 0) + 1;
totals[current] += 1;
}
}
// Normalize to probabilities
const model = {};
for (const current in counts) {
model[current] = {};
for (const next in counts[current]) {
model[current][next] = counts[current][next] / totals[current];
}
}
return model;
}
function predictNextLetters(model, prefix) {
const lastChar = prefix[prefix.length - 1].toUpperCase();
if (!model[lastChar]) {
return []; // No predictions
}
const probs = model[lastChar];
// Get all A-Z letters
const alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'.split('');
// Build array with probs (0 if missing), sorted by descending probability
const predictions = alphabet
.map(letter => ({ letter, probability: probs[letter] || 0 }))
.sort((a, b) => b.probability - a.probability);
return predictions;
}
// Example usage:
// Train on concatenated number words (for puzzle relevance)
const corpus = "onetwothreefourfivesixseveneightnineteneleventwelvethirteen".toUpperCase(); // Extended for more data
const model = buildBigramModel(corpus);
// Predict and log probabilities after the sequence prefix
const sequencePrefix = "OTTFFSSE";
const predictions = predictNextLetters(model, sequencePrefix);
console.log(`Probability distribution for next letter after "${sequencePrefix}":`);
// Log each letter's probability
predictions.forEach(({ letter, probability }) => {
console.log(`${letter}: ${probability.toFixed(4)}`);
});
// Find and log the one with highest probability
const highest = predictions[0];
console.log(`Highest probability next letter: ${highest.letter} (${highest.probability.toFixed(4)})`);
@felquis
Copy link
Author

felquis commented Jan 22, 2026

Here's an example of the out:
image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment