Skip to content

Instantly share code, notes, and snippets.

@well1791
Last active June 11, 2023 22:29
Show Gist options
  • Save well1791/64c37fa933d6942588a90cee82b939c9 to your computer and use it in GitHub Desktop.
Save well1791/64c37fa933d6942588a90cee82b939c9 to your computer and use it in GitHub Desktop.
get bigrams
// see: http://norvig.com/mayzner.html
all = [...document.querySelectorAll('tbody td')]
.filter((td) => td.title.includes(": "))
.map((td) => {
const [bi, val] = td.title
.split('%')[0]
.split(': ')
return [bi.toLowerCase(), Number(val)]
})
.filter(([bi]) => !/(.)\1/.test(bi))
.sort(([, a], [, b]) => a > b ? -1 : 1)
allR = Object.entries(all.reduce((z, [bi, valToAdd]) => {
const [left, right] = bi.split('')
const triLR = [right + bi, bi + left]
const [triL, triR] = triLR
const tri = triLR.every((triX) => z[triX] === undefined)
? triL
: (z[triL] !== undefined ? triL : triR)
return {
...z,
[tri]: (z[tri] || 0 ) + valToAdd,
}
}, {}))
.sort(([, a], [, b]) => a > b ? -1 : 1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment