Skip to content

Instantly share code, notes, and snippets.

@franvarney
Created November 7, 2019 18:44
Show Gist options
  • Save franvarney/9b1f5516697555e77924916f279093e5 to your computer and use it in GitHub Desktop.
Save franvarney/9b1f5516697555e77924916f279093e5 to your computer and use it in GitHub Desktop.
const Fs = require('fs');
const Os = require('os');
const Path = require('path');
const FILTERED_WORDS_FILE = 'filtered.txt';
const STARTS_WITH = 'h';
const TOTAL_LENGTH = 12;
const WORDS_FILE = '20k.txt'; // https://github.com/first20hours/google-10000-english/blob/master/20k.txt
const addendPairs = [];
const addends = {};
for (let i = 1; i <= Math.floor(TOTAL_LENGTH / 2); ++i) {
const pair = [i, TOTAL_LENGTH - i];
addendPairs.push(pair);
addends[pair[0]] = [];
addends[pair[1]] = [];
}
const fileContents = Fs.readFileSync(Path.resolve(process.cwd(), WORDS_FILE), 'utf8');
const words = fileContents.split('\n');
const filteredByStartsWith = words.filter(word => word.startsWith(STARTS_WITH));
filteredByStartsWith.forEach(word => {
const length = word.length;
if (addends.hasOwnProperty(length)) {
addends[length.toString()].push(word);
}
});
const wordPairs = [];
addendPairs.forEach(pair => {
const left = pair[0];
const right = pair[1];
addends[left.toString()].forEach(leftWord => {
addends[right.toString()].forEach(rightWord => {
wordPairs.push([leftWord, rightWord]);
});
});
});
Fs.writeFileSync(Path.resolve(process.cwd(), FILTERED_WORDS_FILE), wordPairs.join('\n'), 'utf8');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment