<commit_before>//
// Drawphone Words
//

var fs = require('fs');
var path = require('path');

function WordPacks () {
	this.wordPacks = [];
}

WordPacks.prototype.loadAll = function () {
	var self = this;
	//reads each txt file in the words folder
	//the name of the file will be the name of the pack
	fs.readdirSync(path.join(__dirname, '../words')).forEach(function(file) {
		//removes the .txt from the end of the filename
		var packName = file.substring(0, file.length - 4);

		var pathToTxt = __dirname + '/../words/' + file;
		var arrayOfWords = fs.readFileSync(pathToTxt).toString().split(/\r?\n/);

		//remove all blank lines
		var newArrayOfWords = [];
		for (var i = 0; i < arrayOfWords.length; i++) {
			var thisWord = arrayOfWords[i].trim();
			if (thisWord !== '') {
				newArrayOfWords.push(thisWord);
			}
		}
		arrayOfWords = newArrayOfWords;

		//can't have an empty list!
		if (arrayOfWords.length === 0) {
			console.log(packName + '.txt is empty. Please add some words, or delete it.');
			process.exit(1);
		}

		self.wordPacks.push(new WordPack(packName, arrayOfWords));
	});
};

WordPacks.prototype.get = function (packName) {
	for (var i = 0; i < this.wordPacks.length; i++) {
		var thisPack = this.wordPacks[i];
		if (thisPack.name === packName) {
			return thisPack;
		}
	}
	return false;
};

WordPacks.prototype.getRandomWord = function (packName) {
	var thisPack = this.get(packName);
	if (thisPack) {
		return thisPack.getRandomWord();
	} else {
		console.err('Pack ' + packName + ' does not exist.');
		process.exit(1);
	}
};

WordPacks.getAllPackNames = function () {
	var names = [];
	fs.readdirSync(path.join(__dirname, '../words')).forEach(function(file) {
		//removes the .txt from the end of the filename
		var packName = file.substring(0, file.length - 4);
		names.push(packName);
	});
	return names;
};


function WordPack (name, words) {
	this.name = name;
	this.words = words;
}

WordPack.prototype.getRandomWord = function () {
	return this.words[Math.floor(Math.random() * this.words.length)];
};


module.exports = WordPacks;
<commit_msg>Fixed crash on bad word pack name<commit_after>//
// Drawphone Words
//

var fs = require('fs');
var path = require('path');

function WordPacks () {
	this.wordPacks = [];
}

WordPacks.prototype.loadAll = function () {
	var self = this;
	//reads each txt file in the words folder
	//the name of the file will be the name of the pack
	fs.readdirSync(path.join(__dirname, '../words')).forEach(function(file) {
		//removes the .txt from the end of the filename
		var packName = file.substring(0, file.length - 4);

		var pathToTxt = __dirname + '/../words/' + file;
		var arrayOfWords = fs.readFileSync(pathToTxt).toString().split(/\r?\n/);

		//remove all blank lines
		var newArrayOfWords = [];
		for (var i = 0; i < arrayOfWords.length; i++) {
			var thisWord = arrayOfWords[i].trim();
			if (thisWord !== '') {
				newArrayOfWords.push(thisWord);
			}
		}
		arrayOfWords = newArrayOfWords;

		//can't have an empty list!
		if (arrayOfWords.length === 0) {
			console.log(packName + '.txt is empty. Please add some words, or delete it.');
			process.exit(1);
		}

		self.wordPacks.push(new WordPack(packName, arrayOfWords));
	});
};

WordPacks.prototype.get = function (packName) {
	for (var i = 0; i < this.wordPacks.length; i++) {
		var thisPack = this.wordPacks[i];
		if (thisPack.name === packName) {
			return thisPack;
		}
	}
	return false;
};

WordPacks.prototype.getRandomWord = function (packName) {
	var thisPack = this.get(packName);
	if (thisPack) {
		return thisPack.getRandomWord();
	} else {
		console.error('Wordpack ' + packName + ' does not exist.');
		return this.wordPacks[0].getRandomWord();
	}
};

WordPacks.getAllPackNames = function () {
	var names = [];
	fs.readdirSync(path.join(__dirname, '../words')).forEach(function(file) {
		//removes the .txt from the end of the filename
		var packName = file.substring(0, file.length - 4);
		names.push(packName);
	});
	return names;
};


function WordPack (name, words) {
	this.name = name;
	this.words = words;
}

WordPack.prototype.getRandomWord = function () {
	return this.words[Math.floor(Math.random() * this.words.length)];
};


module.exports = WordPacks;

{'fraction_non_alphanumeric': 0.15784476619985757, 'fraction_numerical': 0.004272489912176596, 'mean_word_length': 3.6256860592755213, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6145809', 'n_tokens_mistral': 1565, 'n_tokens_neox': 1450, 'n_words': 367}