Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Aaron Saloff Solution #26

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules/
3 changes: 3 additions & 0 deletions .jshintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"esversion": 6
}
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
# assignment_node_dictionary_reader
I CAN HAS SPELLZ IN "Node"? K THNX BYE

Aaron Saloff Solution

Use `node dictionary` to run.
6 changes: 6 additions & 0 deletions data/ther.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"brothel": "A house of lewdness or ill fame; a house frequented byprostitutes; a bawdyhouse.",
"ethel": "Noble. [Obs.]",
"herschel": "See Uranus.",
"satchel": "A little sack or bag for carrying papers, books, or smallarticles of wearing apparel; a hand bag. [Spelled also sachel.]The whining schoolboy with his satchel. Shak."
}
46 changes: 46 additions & 0 deletions data/ther_end.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
{
"anther": "That part of the stamen containing the pollen, or fertilizingdust, which, when mature, is emitted for the impregnation of theovary.-- An\"ther*al, a.",
"bather": "One who bathes.",
"blather": "To talk foolishly, or nonsensically. G. Eliot.",
"bother": "To annoy; to trouble; to worry; to perplex. See Pother.",
"brother": "To make a brother of; to call or treat as a brother; to admitto a brotherhood. Sir W. Scott.",
"either": "precedes two, or more, coördinate words or phrases, and isintroductory to an alternative. It is correlative to or.Either he is talking, or he is pursuing, or he is in a journey, orperadventure he sleepeth. 1 Kings xviii. 27.Few writers hesitate to use either in what is called a triplealternative; such as, We must either stay where we are, proceed, orrecede. Latham.",
"ether": "A medium of great elasticity and extreme tenuity, supposed topervade all space, the interior of solid bodies not excepted, and tobe the medium of transmission of light and heat; hence often calledluminiferous ether.",
"farther": "To help onward. [R.] See Further.",
"feather": "A longitudinal strip projecting as a fin from an object, tostrengthen it, or to enter a channel in another object and therebyprevent displacement sidwise but permit motion lengthwise; a spline.",
"forefather": "One who precedes another in the line of genealogy in anydegree, but usually in a remote degree; an ancestor.Respecting your forefathers, you would have been taught to respectyourselves. Burke.Forefathers' Day, the anniversary of the day (December 21) on whichthe Pilgrim Fathers landed at Plymouth, Massachusetts (1620). Onaccount of a mistake in reckoning the change from Old Style to NewStyle, it has generally been celebrated on the 22d.",
"foregather": "Same as Forgather.",
"forgather": "To convene; to gossip; to meet accidentally. [Scot.] Jamieson.Within that circle he forgathered with many a fool. Wilson.",
"further": "To a greater distance; in addition; moreover. See Farther.Carries us, I know not how much further, into familiar company. M.Arnold.They sdvanced us far as Eleusis and Thria; but no further. Jowett(Thucyd. ).Further off, not so near; apart by a greater distance.",
"gather": "To bring together, or nearer together, in masonry, as where thewidth of a fireplace is rapidly diminished to the width of the flue,or the like.",
"godfather": "A man who becomes sponsor for a child at baptism, and makeshimself a surety for its Christian training and instruction.There shall be for every Male-child to be baptized, when they can behad, two Godfathers and one Godmother; and for every Female, oneGodfather and two Godmothers; and Parents shall be admitted asSponsors, if it is desired. Book of Common Prayer (Prot. Episc. Ch.,U. S. ).",
"godmother": "A woman who becomes sponsor for a child in baptism. SeeGodfather",
"grandfather": "A father's or mother's father; an ancestor in the next degreeabove the father or mother in lineal ascent. Grandfather longlegs.(Zoöl.) See Dady longlegs.",
"grandmother": "The mother of one's father or mother.",
"heather": "Heath. [Scot.]Gorse and grass And heather, where his footsteps pass, The brighterseem. Longfellow.Heather bell (Bot.), one of the pretty subglobose flowers of twoEuropean kinds of heather (Erica Tetralix, and E. cinerea).",
"lather": "To spread over with lather; as, to lather the face.",
"leather": "To beat, as with a thong of leather. [Obs. or Colloq.] G.Eliot.",
"loather": "One who loathes.",
"mather": "See Madder.",
"mother": "Received by birth or from ancestors; native, natural; as,mother language; also acting the part, or having the place of amother; producing others; originating.It is the mother falsehood from which all idolatry is derived. T.Arnold.Mother cell (Biol.), a cell which, by endogenous divisions, givesrise to other cells (daughter cells); a parent cell.-- Mother church, the original church; a church from which otherchurches have sprung; as, the mother church of a diocese.-- Mother country, the country of one's parents or ancestors; thecountry from which the people of a colony derive their origin.-- Mother liquor (Chem.), the impure or complex residual solutionwhich remains after the salts readily or regularly crystallizing havebeen removed.-- Mother queen, the mother of a reigning sovereign; a queen mother.-- Mother tongue. (a) A language from which another language has hadits origin. (b) The language of one's native land; native tongue.-- Mother water. See Mother liquor (above).-- Mother wit, natural or native wit or intelligence.",
"neither": "Not either; not the one or the other.Which of them shall I take Both one or neither Neither can beenjoyed, If both remain alive. Shak.He neither loves, Nor either cares for him. Shak.",
"nether": "Situated down or below; lying beneath, or in the lower part;having a lower position; belonging to the region below; lower; under;-- opposed to upper.'Twixt upper, nether, and surrounding fires. Milton.This darksome nether world her light Doth dim with horror anddeformity. Spenser.All my nether shape thus grew transformed. Milton.",
"norther": "A wind from the north; esp., a strong and cold north wind inTexas and the vicinity of the Gulf of Mexico.",
"other": "Either; -- used with other or or for its correlative (as either. . . or are now used). [Obs.]Other of chalk, other of glass. Chaucer.",
"panther": "A large dark-colored variety of the leopard, by some zoölogistsconsidered a distinct species. It is marked with large ringlikespots, the centers of which are darker than the color of the body.",
"pinfeather": "A feather not fully developed; esp., a rudimentary feather justemerging through the skin.",
"pother": "Bustle; confusion; tumult; flutter; bother. [Written alsopotter, and pudder.] \"What a pother and stir!\" Oldham. \"Coming onwith a terrible pother.\" Wordsworth.",
"rather": "Prior; earlier; former. [Obs.]Now no man dwelleth at the rather town. Sir J. Mandeville.",
"regather": "To gather again.",
"slither": "To slide; to glide. [Prov. Eng.]",
"smoother": "One who, or that which, smooths.",
"soother": "One who, or that which, soothes.",
"stepbrother": "A brother by the marriage of one's father with the mother ofanother, or of one's mother with the father of another.",
"stepfather": "The husband of one's mother by a subsequent marriage.",
"stepmother": "The wife of one's father by a subsequent marriage.",
"tether": "A long rope or chain by which an animal is fastened, as to astake, so that it can range or feed only within certain limits.",
"weather": "To sail or pass to the windward of; as, to weather a cape; toweather another ship.",
"wether": "A castrated ram.",
"whether": "Which (of two); which one (of two); -- used interrogatively andrelatively. [Archaic]Now choose yourself whether that you liketh. Chaucer.One day in doubt I cast for to compare Whether in beauties' glory didexceed. Spenser.Whether of them twain did the will of his father Matt. xxi. 31.",
"zither": "An instrument of music used in Austria and Germany. It has fromthirty to forty wires strung across a shallow sounding-board, whichlies horizontally on a table before the performer, who uses bothhands in playing on it."
}
7 changes: 7 additions & 0 deletions data/thou.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"thou": "The second personal pronoun, in the singular number, denotingthe person addressed; thyself; the pronoun which is used inaddressing persons in the solemn or poetical style.Art thou he that should come Matt. xi. 3.",
"though": "Granting, admitting, or supposing that; notwithstanding that;if.Though he slay me, yet will I trust in him. Job xiii. 15.Not that I so affirm, though so it seem. Milton.",
"thought": "imp. & p. p. of Think.",
"thousandfold": "Multiplied by a thousand.",
"thousandth": "The quotient of a unit divided by a thousand; one of a thousandequal parts into which a unit is divided."
}
3 changes: 3 additions & 0 deletions data/welcome.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Welcome to the Node Dictionary Reader!
======================================
Enter q to quit
39 changes: 39 additions & 0 deletions dictionary/dictionary.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
var fs = require('fs');
var chalk = require('chalk');

module.exports = {
load: (file) => {
return new Promise((resolve, reject) => {
fs.readFile(`./data/${ file }`, 'utf8', (err, data) => {
if (err) throw err;

var dictionary = JSON.parse(data);
var alphabetString = 'abcdefghijklmnopqrstuvwxyz';
var alphabet = alphabetString.toUpperCase().split('');
var dictionaryWords = Object.keys(dictionary);

console.log(chalk.green(`Successfully loaded: ${ file }`));
console.log(chalk.blue(`Word Count: ${ dictionaryWords.length }`));

console.log('Word frequency by starting letter:');
for (var i = 0; i < alphabet.length; i++) {
var letter = alphabet[i].toLowerCase();

var applicableWords = [];

dictionaryWords.forEach((word) => {
if (word.startsWith(`${ letter }`)) {
applicableWords.push(word);
}
});

console.log(`${ alphabet[i] }: ${ applicableWords.length }`);

if (i === alphabet.length - 1) { // If last letter, resolve promise
resolve(dictionary);
}
}
});
});
}
};
67 changes: 67 additions & 0 deletions dictionary/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
var fs = require('fs');
var chalk = require('chalk');
var loader = require('./loader');
var dictionary = require('./dictionary');
var searcher = require('./searcher');

var dictionaries = loader.dictionaries;

var welcomeUser = () => {
return new Promise((resolve, reject) => {
fs.readFile('./data/welcome.txt', 'utf8', (err, data) => {
if (err) throw err;
// Display welcome message
console.log(data);
resolve();
});
});
};

var promptForDictionary = () => {
// Give JSON dictionary options
console.log("Select a dictionary to load:");

// display numbered file names
for (i = 0; i < dictionaries.length; i++) {
console.log(`${ i + 1 }. ${dictionaries[i]}`);
}

process.stdout.write('> ');

// start listening to STDIN
process.stdin.resume();
process.stdin.setEncoding('utf8');

process.stdin.once('data', (data) => {
data = data.trim();

// Find dictionary
var file = dictionaries[parseInt(data) - 1];

// Exit program when user inputs 'q'
if (data === 'q') {
process.exit();
} else if (file) { // user enters a valid number for a dictionary file

// load the file and get word information
dictionary.load(file)
.then((dictionary) => {
// Start searcher
searcher.initSearch(dictionary);
});
} else if (parseInt(data)) { // user enters a number that is not a dictionary
console.log(chalk.red(`\nInvalid dictionary number: ${ data }\n\n`));
// Ask for dictionary again
promptForDictionary();
} else {
console.log(chalk.red(`\nInvalid input: ${ data }\n\n`));
// Ask for dictionary again
promptForDictionary();
}
});
};

welcomeUser()
.then(() => {
promptForDictionary();
});
17 changes: 17 additions & 0 deletions dictionary/loader.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
var fs = require('fs');

var dictionaries = [];

// Push all JSON files to dictionaries
fs.readdir('./data', 'utf8', (err, files) => {
if (err) throw err;
files.forEach((file) => {
if (file.endsWith('.json')) {
dictionaries.push(file);
}
});
});

module.exports = {
dictionaries: dictionaries
};
87 changes: 87 additions & 0 deletions dictionary/saver.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
var fs = require('fs');
var chalk = require('chalk');

var handleDataWrite = (dictionary, results, path) => {
var obj = {};

for(var i = 0; i < results.length; i++) {
obj[`${ results[i] }`] = dictionary[results[i]];
}

var json = JSON.stringify(obj, null, ' ');

fs.writeFile(`./data/${ path }`, json, 'utf8', (err) => {
if (err) throw err;
console.log(chalk.green('The file has been saved!\nThanks for using Node Dictionary!'));
process.exit();
});
};

var handleOverwrite = (dictionary, results, path) => {
// ask if overwrite
process.stdout.write(chalk.blue("\nThat file exists, overwrite? y/n? 'q' quits.\n> "));

process.stdin.once('data', (answer) => {
answer = answer.toLowerCase().trim();

if (answer === 'q' || answer === 'quit') {
process.exit();
} else if (answer === 'n' || answer === 'no') {
getFilePathAnswer(dictionary, results);
} else if (answer === 'y' || answer === 'yes') {
// Overwrite data
handleDataWrite(dictionary, results, path);
} else {
process.stdout.write(chalk.red(`\n${ answer } is not a valid option.\n`));
// Ask again
handleOverwrite(dictionary, results, path);
}
});
};

var getFilePathAnswer = (dictionary, results) => {
process.stdout.write(chalk.blue("\nWhat filepath should we write results to?\n> "));

process.stdin.once('data', (path) => {
path = path.toLowerCase().trim();

if (path === 'q' || path === 'quit') {
process.exit();
} else if (!path.endsWith('.json')) {
process.stdout.write(chalk.red('\nFilename must end in .json\n'));
getFilePathAnswer(dictionary, results);
} else {
if (fs.existsSync(`./data/${ path }`)) {
handleOverwrite(dictionary, results, path);
} else {
handleDataWrite(dictionary, results, path);
}
}
});
};

var getSaveAnswer = (dictionary, results) => {
process.stdout.write(chalk.green("\nDo you want to save results? y/n? 'q' quits.\n> "));

process.stdin.once('data', (saveAnswer) => {
saveAnswer = saveAnswer.toLowerCase().trim();

if (saveAnswer === 'q' || saveAnswer === 'quit') {
process.exit();
} else if (saveAnswer === 'y' || saveAnswer === 'yes') {
getFilePathAnswer(dictionary, results);
} else if (saveAnswer === 'n' || saveAnswer === 'no') {
process.stdout.write(chalk.green('\nThank you for using Dictionary. Have a nice day!\n'));
process.exit();
} else {
process.stdout.write(chalk.red(`\n${ saveAnswer } is not a valid option.\n`));
getSaveAnswer(dictionary, results);
}
});
};

module.exports = {
init: (dictionary, results) => {
getSaveAnswer(dictionary, results);
}
};
101 changes: 101 additions & 0 deletions dictionary/searcher.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
var fs = require('fs');
var chalk = require('chalk');
var saver = require('./saver');

var processWords = (dictionary, matches, searchType) => {
if (matches.length === 0) {
console.log(chalk.red('\nNo matches found.'));
search(searchType, dictionary);
} else {
console.log(chalk.green(`\nFound ${ matches.length } matches:`));
matches.forEach((word) => {
console.log(word);
});
}
};

var searchPhrase = (dictionary, phrase, searchType) => {

phrase = phrase.toLowerCase();
var words = Object.keys(dictionary);
var searchResults;

switch(searchType) {
// When exact
case '1':
searchResults = words.filter(word => phrase === word);
break;
// When partial
case '2':
searchResults = words.filter(word => word.includes(phrase));
break;
// When begins with
case '3':
searchResults = words.filter(word => word.startsWith(phrase));
break;
// When ends with
case '4':
searchResults = words.filter(word => word.endsWith(phrase));
break;
}

processWords(dictionary, searchResults, searchType);
return searchResults;
};

var search = (searchType, dictionary) => {

process.stdout.write('\nEnter the search term:\n> ');

process.stdin.once('data', (searchTerm) => {
searchTerm = searchTerm.trim();

if (searchTerm === 'q') {
process.exit();
} else if (/[^a-zA-Z]+/g.test(searchTerm)) {

// Log error
console.log(chalk.red('\nYou must enter a valid search with only letters.'));

// re-ask for input
search(searchType, dictionary);

} else { // Perform search on valid input
var searchResults = searchPhrase(dictionary, searchTerm, searchType);

if (searchResults.length !== 0) {
saver.init(dictionary, searchResults);
}
}
});
};

var Searcher = {
initSearch: (dictionary) => {
process.stdin.resume();

// Prompt user for search type
process.stdout.write('\n');
console.log("What kind of search?");
console.log('1: Exact');
console.log('2: Partial');
console.log('3: Starts with');
console.log('4: Ends with');
process.stdout.write('> ');

process.stdin.once('data', (searchType) => {
searchType = searchType.trim();

if (searchType === 'q') {
process.exit();
} else if (!['1','2','3','4'].includes(searchType)) { // Is not a number 1 - 4
process.stdout.write(chalk.red('\n\nYou must enter a number for the search type.\n\n'));
Searcher.initSearch(dictionary);
} else {
search(searchType, dictionary);
}
});
}
};

module.exports = Searcher;
Loading