Skip to content

Instantly share code, notes, and snippets.

@bedwards
Created November 3, 2021 13:03
Show Gist options
  • Save bedwards/a8230aeb636906a236fb6362c59ecc07 to your computer and use it in GitHub Desktop.
Save bedwards/a8230aeb636906a236fb6362c59ecc07 to your computer and use it in GitHub Desktop.
CMU pronouncing dictionary in Typescript

cmudict-0.7b.txt

$ npm install
$ npx ts-node cmudict.ts hello world
hh-ah0-l-ow1 w-er1-l-d
const assert = require('assert')
const fs = require('fs')
const AVLTree = require('binary-search-tree').AVLTree
const minimist = require('minimist')
const split = require('split')
const load = async (): Promise<any> => {
const cmudict = new AVLTree({ unique: true })
return new Promise<any>((resolve: any) => {
fs.createReadStream('cmudict-0.7b.txt')
.pipe(split())
.on('data', (line: string) => {
if (/(^$|^[^A-Z']|\(\d\))/.test(line)) return
var [word, pronunciation] = line.split(' ', 2)
pronunciation = pronunciation.replace(/ /g, '-')
cmudict.insert(word.toLowerCase(), pronunciation.toLowerCase())
})
.on('end', () => resolve(cmudict))
})
}
const main = async (words: string[]) => {
const cmudict = await load()
for (const word of words) {
process.stdout.write(cmudict.search(word.toLowerCase())[0])
process.stdout.write(' ')
}
console.log()
}
if (require.main === module) {
var argv = minimist(process.argv.slice(2))
assert(argv._.length > 0)
main(argv._)
}
{
"devDependencies": {
"@types/minimist": "^1.2.2",
"@types/split": "^1.0.0",
"npx": "^10.2.2",
"ts-node": "^10.4.0"
},
"dependencies": {
"binary-search-tree": "^0.2.6",
"minimist": "^1.2.5",
"split": "^1.0.1"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment