Updated batch.js for 10 words
Added import.js to import dictionary Updated license with WordNet info Updated puzzles.js and script for 10 words
This commit is contained in:
34
utils/README.md
Normal file
34
utils/README.md
Normal file
@@ -0,0 +1,34 @@
|
||||
# Utils
|
||||
|
||||
The utils directory contain Node.js Javascript files to create a word dictionary and generate a puzzle.
|
||||
|
||||
* import.js - Imports Princeton's WordNet open dictionary database files into a words.txt file
|
||||
* batch.js - Creates a new puzzle.js file containing 999 word puzzles from the words.txt file created above.
|
||||
|
||||
|
||||
|
||||
# import.js instructions
|
||||
|
||||
Imports and cleans dictionary files provided by WordNet 3.0
|
||||
|
||||
Download dictionary file (look for "DATABASE FILES ONLY" package):
|
||||
|
||||
http://wordnet.princeton.edu/wordnet/download/current-version/
|
||||
|
||||
Decompress and copy data.adj, data.adv, data.noun and data.verb files
|
||||
to same directory as this script, then run:
|
||||
|
||||
$ node import.js
|
||||
|
||||
This will produce a words.txt dictionary file of 4-10 letter words
|
||||
to be used by batch.js puzzle generator.
|
||||
|
||||
|
||||
|
||||
# batch.js instructions
|
||||
|
||||
After creating a words.txt dictionary, run:
|
||||
|
||||
$ node batch.js
|
||||
|
||||
This will create a puzzles.js file in the root directory of the project.
|
||||
136
utils/batch.js
Normal file
136
utils/batch.js
Normal file
@@ -0,0 +1,136 @@
|
||||
'use strict';
|
||||
var fs = require('fs');
|
||||
|
||||
// List out the possible ways to divide a word into squares with 2 or 3 letters.
|
||||
var squaresList = {};
|
||||
squaresList[2] = [
|
||||
{wordlen: 4, squares: [2,2] },
|
||||
{wordlen: 5, squares: [2,3] },
|
||||
{wordlen: 5, squares: [3,2] },
|
||||
{wordlen: 6, squares: [3,3] }
|
||||
];
|
||||
squaresList[3] = [
|
||||
{wordlen: 6, squares: [2,2,2] },
|
||||
{wordlen: 7, squares: [2,2,3] },
|
||||
{wordlen: 7, squares: [2,3,2] },
|
||||
{wordlen: 7, squares: [3,2,2] },
|
||||
{wordlen: 8, squares: [3,3,2] },
|
||||
{wordlen: 8, squares: [3,2,3] },
|
||||
{wordlen: 8, squares: [2,3,3] },
|
||||
{wordlen: 9, squares: [3,3,3] },
|
||||
];
|
||||
squaresList[4] = [
|
||||
{wordlen: 8, squares: [2,2,2,2] },
|
||||
{wordlen: 9, squares: [2,2,2,3] },
|
||||
{wordlen: 9, squares: [2,2,3,2] },
|
||||
{wordlen: 9, squares: [2,3,2,2] },
|
||||
{wordlen: 9, squares: [3,2,2,2] },
|
||||
{wordlen: 10, squares: [3,3,2,2]},
|
||||
{wordlen: 10, squares: [3,2,3,2]},
|
||||
{wordlen: 10, squares: [3,2,2,3]},
|
||||
{wordlen: 10, squares: [2,2,3,3]},
|
||||
{wordlen: 10, squares: [2,3,2,3]}
|
||||
];
|
||||
|
||||
// To fill 30 squares with
|
||||
var squaresWordLengths = [
|
||||
[4, 4, 3, 3, 3, 3, 3, 3, 2, 2],
|
||||
[4, 4, 4, 3, 3, 3, 3, 2, 2, 2]
|
||||
];
|
||||
|
||||
|
||||
|
||||
|
||||
// pull in the word list, divide into lists by word length
|
||||
var lines = fs.readFileSync('./words.txt').toString().split('\n');
|
||||
|
||||
var words = {};
|
||||
|
||||
for (var i = 0; i < lines.length; i++) {
|
||||
var line = lines[i].trim();
|
||||
if(line !== ''){
|
||||
var ln = line.split(' - ');
|
||||
var s = ln[0].length;
|
||||
if(!words[s]){
|
||||
words[s] = [];
|
||||
}
|
||||
words[s].push({
|
||||
word: ln[0],
|
||||
def : ln[1].charAt(0).toUpperCase() + ln[1].substr(1) + '.'
|
||||
});
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
// generate puzzles
|
||||
var puzzles = [];
|
||||
|
||||
for(var p = 1; p <= 1000; p++){
|
||||
|
||||
var puzzle = {};
|
||||
var cwords = [];
|
||||
var csquares = [];
|
||||
|
||||
var squaresNeeded = squaresWordLengths[Math.floor(Math.random() * squaresWordLengths.length)];
|
||||
|
||||
for (var i = 0; i < squaresNeeded.length; i++) {
|
||||
|
||||
var squareTypes = squaresList[squaresNeeded[i]];
|
||||
|
||||
var squareType = squareTypes[Math.floor((Math.random() * squareTypes.length))];
|
||||
|
||||
var wordlist = words[squareType.wordlen];
|
||||
|
||||
var entry = wordlist[Math.floor((Math.random() * wordlist.length))];
|
||||
|
||||
cwords.push({
|
||||
word: rot13(entry.word),
|
||||
def: entry.def
|
||||
});
|
||||
|
||||
var start = 0;
|
||||
|
||||
var squares = squareType.squares;
|
||||
|
||||
for (var x = 0; x < squares.length; x++) {
|
||||
var letnum = squares[x];
|
||||
|
||||
csquares.push(entry.word.substr(start, letnum));
|
||||
|
||||
start += letnum;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
cwords = shuffle(cwords);
|
||||
csquares = shuffle(csquares);
|
||||
|
||||
puzzle['clues'] = cwords;
|
||||
puzzle['squares'] = csquares;
|
||||
|
||||
puzzles.push(puzzle);
|
||||
|
||||
}
|
||||
|
||||
var json = JSON.stringify(puzzles);
|
||||
|
||||
var js = 'var puzzles = ' + json;
|
||||
|
||||
fs.writeFileSync('../puzzles.js', js);
|
||||
|
||||
|
||||
|
||||
|
||||
function rot13(str) {
|
||||
return str.replace(/[a-zA-Z]/g, function(c) {
|
||||
return String.fromCharCode((c <= 'Z' ? 90 : 122) >= (c = c.charCodeAt(0) + 13) ? c : c - 26);
|
||||
});
|
||||
}
|
||||
|
||||
function shuffle(arr) {
|
||||
for(var j, x, i = arr.length; i; j = parseInt(Math.random() * i), x = arr[--i], arr[i] = arr[j], arr[j] = x);
|
||||
return arr;
|
||||
}
|
||||
|
||||
|
||||
122
utils/import.js
Normal file
122
utils/import.js
Normal file
@@ -0,0 +1,122 @@
|
||||
/*
|
||||
import.js
|
||||
|
||||
Imports and cleans dictionary files provided by WordNet 3.0
|
||||
|
||||
Download dictionary file (look for "DATABASE FILES ONLY" package):
|
||||
|
||||
http://wordnet.princeton.edu/wordnet/download/current-version/
|
||||
|
||||
Decompress and copy data.adj, data.adv, data.noun and data.verb files
|
||||
to same directory as this script, then run:
|
||||
|
||||
$ node import.js
|
||||
|
||||
This will produce a words.txt dictionary file of 4-10 letter words
|
||||
to be used by batch.js puzzle generator.
|
||||
|
||||
*/
|
||||
|
||||
|
||||
|
||||
var fs = require('fs');
|
||||
var util = require('util');
|
||||
|
||||
var files = ['data.adj','data.adv','data.verb','data.noun'];
|
||||
|
||||
var text = '';
|
||||
|
||||
util.log('Starting import');
|
||||
|
||||
for(var f = 0; f < files.length; f++){
|
||||
text += fs.readFileSync('./' + files[f]).toString()
|
||||
}
|
||||
|
||||
var lines = text.split('\n');
|
||||
|
||||
var wordList = [];
|
||||
var defList = [];
|
||||
|
||||
var perc = Math.floor(lines.length / 100);
|
||||
|
||||
for (var i = 0; i < lines.length; i++) {
|
||||
if(lines[i].substr(0, 2) == ' '){
|
||||
continue;
|
||||
}
|
||||
|
||||
if(i % perc == 0){
|
||||
util.log(Math.floor((i/lines.length) * 100) + '%');
|
||||
}
|
||||
|
||||
var line = lines[i].trim();
|
||||
var ln = line.split('|');
|
||||
var wordPartStr = ln[0].trim();
|
||||
var wordPart = wordPartStr.split(' ');
|
||||
if(wordPart[4]){
|
||||
var word = wordPart[4].trim();
|
||||
var def = ln[1].trim();
|
||||
|
||||
if(wordList.indexOf(word) !== -1 || word.charAt(0).toUpperCase() == word.charAt(0) || word.indexOf('\'') !== -1 || word.indexOf('-') !== -1 || word.indexOf('_') !== -1 || word.indexOf('(') !== -1){
|
||||
continue;
|
||||
}
|
||||
|
||||
if(def.indexOf(';') !== -1){
|
||||
def = def.substr(0, def.indexOf(';'));
|
||||
}
|
||||
|
||||
def = def.charAt(0).toUpperCase() + def.substr(1) + '.';
|
||||
|
||||
wordList.push(word);
|
||||
defList.push(def);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
var words = '';
|
||||
|
||||
for(var x = 4; x < 11; x++){
|
||||
for(var w = 0; w < wordList.length; w++){
|
||||
if(wordList[w].length == x){
|
||||
//console.log(wordList[w] + ' - ' + defList[w]);
|
||||
words += wordList[w] + ' - ' + defList[w] + '\n';
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fs.writeFileSync('./words.txt', words);
|
||||
|
||||
|
||||
/*
|
||||
var words = {};
|
||||
|
||||
for(var x = 4; x < 11; x++){
|
||||
var wordList = [];
|
||||
for (var i = 0; i < lines.length; i++) {
|
||||
var line = lines[i].trim();
|
||||
if(line !== ''){
|
||||
var ln = line.split('|');
|
||||
var word = ln[0].trim();
|
||||
var def = ln[1].trim();
|
||||
|
||||
if(wordList.indexOf(word) !== -1){
|
||||
continue;
|
||||
}
|
||||
|
||||
if(def.indexOf(';') !== -1){
|
||||
def = def.substr(0, def.indexOf(';'));
|
||||
}
|
||||
|
||||
def = def.charAt(0).toUpperCase() + def.substr(1) + '.';
|
||||
var s = word.length;
|
||||
|
||||
if(s == x){
|
||||
wordList.push(word);
|
||||
console.log(word + ' - ' + def);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
*/
|
||||
33505
utils/words.txt
Normal file
33505
utils/words.txt
Normal file
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user