All source files

scripts/generate-five-letter-wordlist.mjs

Download raw file

import {execFileSync} from 'node:child_process';
import {writeFile} from 'node:fs/promises';
import {dirname,join,resolve} from 'node:path';
import {fileURLToPath} from 'node:url';
import {OFFENSIVE_WORDS} from '../src/word-data.mjs';

export function parseFiveLetterWords(dictionaryText){
  const entries=dictionaryText.split(/\r?\n/).slice(1).map(line=>line.split('/')[0].trim());
  return [...new Set(entries.filter(word=>/^[a-z]{5}$/.test(word)))].sort();
}

export function applyDenylist(words,denyList=OFFENSIVE_WORDS){
  const blocked=new Set(denyList);
  return {
    words:words.filter(word=>!blocked.has(word)),
    removed:words.filter(word=>blocked.has(word))
  };
}

export function buildWordlist(dictionaryText){
  return applyDenylist(parseFiveLetterWords(dictionaryText));
}

const invokedDirectly=process.argv[1]&&resolve(process.argv[1])===fileURLToPath(import.meta.url);
if(invokedDirectly){
  const archive=process.argv[2];
  if(!archive)throw new Error('Usage: node scripts/generate-five-letter-wordlist.mjs /path/to/hunspell-en_US-2020.12.07.zip');
  const text=execFileSync('unzip',['-p',archive,'en_US.dic'],{encoding:'utf8',maxBuffer:10*1024*1024});
  const {words,removed}=buildWordlist(text);
  const root=join(dirname(fileURLToPath(import.meta.url)),'..');
  const body=`// Generated from SCOWL en_US 2020.12.07 with the offensive-word denylist applied; see data/scowl/SOURCE.md.\nexport const FIVE_LETTER_WORDS = ${JSON.stringify(words,null,2)};\n`;
  await writeFile(join(root,'src/five-letter-words.mjs'),body);
  console.log(`Generated ${words.length} lowercase five-letter SCOWL words; removed ${removed.length} denylisted words${removed.length?`: ${removed.join(', ')}`:''}`);
}