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

Stephanie #22

Open
wants to merge 21 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
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
# assignment_node_dictionary_reader
I CAN HAS SPELLZ IN "Node"? K THNX BYE

Eric Glover
Stephanie Barker
Zhazira Zhunussova
206 changes: 206 additions & 0 deletions lib/cli_ui.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,206 @@
const loader = require("./load.js");
const search = require('./search.js');
const save_mod = require('./save.js');
const fs = require("fs");


//listen for user_input
function startMessage() {

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

// Inline function to handle message output
function showMessage() {
console.log('Welcome to the fabulous Dictionary Reader!');
console.log("=========================================");
console.log("Enter q to quit\n");
console.log("Select a dictionary:");

loader.getDicts( function (data) { // start of loader.getDicts
for (let i = 0; i < data.length; i++) {
let dictString = `${i + 1}. ${data[i]}`;
console.log(dictString);
}

let repeat = true;
let dictionaryMonster = function (input_data) { // start of process.stdin
input_data = parseInt(input_data.trim());

//not number
if (isNaN(input_data)) {
console.log ("It's not a number! Please, choose another.");
repeat = true;
}
else if (input_data < 1 || input_data > data.length) {
//range check
console.log ("It's not in a range from 1 to " + data.length + ". Please, choose another.");
repeat = true;
} else {
//read file
process.stdin.pause();
process.stdin.removeListener('data', dictionaryMonster);
loader.getFile( data[ input_data - 1], function(dict) {
searchFormat(dict);
}); // end of loader.getFile
}
}
while (repeat) { // start of while loop
repeat = false;
///get user input

process.stdin.on('data', dictionaryMonster); // end of process.stdin
} //end of while loop

}); // end of loader.getDicts
} // end of showMessage

showMessage();

}//end of start

function searchFormat(dict) {
//print user message
console.log('What kind of search??');
console.log('1: Exact\n2: Partial\n3: Begins With\n4: Ends With');
process.stdin.resume();

let repeat = true;
let searchMonster = function (input_data) { // start of process.stdin
input_data = parseInt(input_data.trim());
//not number
if (isNaN(input_data)) {
console.log ("It's not a number! Please, choose another.");
repeat = true;
}
else if (input_data < 1 || input_data > 4) {
//range check
console.log ("It's not in a range from 1 to 4. Please, choose another.");
repeat = true;
} else {

//search
process.stdin.pause();
process.stdin.removeListener('data', searchMonster);
const searchTypes = ["", "exactMatch", "partialMatches", "beginsWith", "endsWith"];

searchWord(dict, searchTypes[input_data]);
}
}

while (repeat) { // start of while loop
repeat = false;
///get user input
process.stdin.on('data', searchMonster); // end of process.stdin
} //end of while loop
}

function searchWord(dict, searchType) {
console.log("Type the string to search.")
process.stdin.resume();

process.stdin.on("data", function(data) {
// console.log("Type: " + searchType);
// console.log("Data: " + data);
// console.log(search);
var result = search[searchType](dict, data);
console.log( result );
//call
process.stdin.pause();
process.stdin.removeAllListeners('data');
save( result, dict );
});
}

function save( results, dict ){
console.log ("Do you want to save results? y/n?");
//user input
process.stdin.resume();
let invalidInput = true;
while ( invalidInput ){
invalidInput = false;
process.stdin.on('data', function( data ){
data = data.trim();
//error check

if( data != 'y' && data != 'n'){
//errors
console.log("ERROR, y or n please.");
invalidInput = true;
}else{
process.stdin.pause();
process.stdin.removeAllListeners('data');
if ( data === 'y'){
console.log('What filepath should we write to?');
getFileName(results, dict );
}else{
console.log("Goodbye.");
process.exit();
}
}
})
}
}
function getFileName( results, dict ){
process.stdin.resume();
//if you want error checking add it here later

process.stdin.on('data', function( data ){
data = data.trim();
//writing to data
fs.readFile( '../files/' + data, function( err, fileData ) {
if ( err ){
//save the data
save_mod.save( dict, results, data, function() {
console.log("File saved!");
process.exit();
});
//check error code later

}else{
//ask question
process.stdin.pause();
process.stdin.removeAllListeners('data');
fileOverwrite(results, dict, data);
}
})
})
}

function fileOverwrite(results, dict, fileName) {
console.log("That file exists, overwrite? y/n?");
process.stdin.resume();
let invalidInput = true;
while ( invalidInput ){
invalidInput = false;
process.stdin.on('data', function( data ){
data = data.trim();
//error check

if( data != 'y' && data != 'n'){
//errors
console.log("ERROR, y or n please.");
invalidInput = true;
}else{
process.stdin.pause();
process.stdin.removeAllListeners('data');
if ( data === 'y'){
save_mod.save (dict, results, fileName, function() {
console.log ("The file is overwritten!");
process.exit();
});
}else{
console.log("Goodbye!");
process.exit();
}
}
})
}
}




// Start the app
startMessage();
Empty file added lib/dictionary_data.js
Empty file.
Empty file added lib/index.js
Empty file.
32 changes: 32 additions & 0 deletions lib/load.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
const fs = require("fs");

let dicts = {
getDicts: function(callback) {
fs.readdir('../data', function(err, data) {
if (err) throw err;
callback(data);
});
},

getFile: function (fileName, callback) {
const dict = require('../data/' + fileName);
console.log ("Successfully loaded: " + fileName);

let wordCount = 0;
let wordCountStarting = {};

for (let word in dict) {
wordCount ++;
if (word[0] in wordCountStarting) wordCountStarting[word[0]] ++;
else wordCountStarting[word[0]] = 1;
}
console.log ("Word count: " + wordCount);
console.log ("Word frequency by starting letter: ");
for (let letter in wordCountStarting) {
console.log (letter.toUpperCase() + ": " + wordCountStarting[letter]);
}
callback(dict);
}
}

module.exports = dicts;
22 changes: 22 additions & 0 deletions lib/save.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
const fs = require("fs");

saveStuff = {
save: function(dict, results, file_name, callback) {
//if error

let formattedResult = "";

for (let i = 0; i < results.length; i++) {
formattedResult += `${results[i]}: ${dict[results[i]]}\n\n`;
}

// console.log(formattedResult);

fs.writeFile('../files/' + file_name, formattedResult, "utf8", function(err) {
if (err) throw err;
callback();
});
}
}

module.exports = saveStuff;
57 changes: 57 additions & 0 deletions lib/search.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
let search = {
exactMatch: function (dict, str) {
str = str.trim();
let keys = Object.keys(dict);
//let matches = [];
let count = 0;
for (let i = 0; i < keys.length; i ++ ) {
if (str === keys[i]){
count++;
}
}
return (count > 0) ? [str] : [];
},
partialMatches: function (dict, str) {
str = str.trim();
let keys = Object.keys( dict );
let matches = [];
let reg = new RegExp( str, 'i' );
for ( let i = 0; i < keys.length; i++ ){
//str is regex
if ( reg.test( keys[i]) ){
matches.push( keys[i] );
}
}
return matches;
},
beginsWith: function( dict, str ){
str = str.trim();
let keys = Object.keys( dict );
let matches = [];
let reg = new RegExp( '^' + str , 'i' );
for ( let i = 0; i < keys.length; i++ ){
//str is regex
if ( reg.test( keys[i]) ){
matches.push( keys[i] );
}
}
return matches;
},
endsWith: function( dict, str ){
str = str.trim();
let keys = Object.keys( dict );
let matches = [];
let reg = new RegExp( str + '$' , 'i' );
for ( let i = 0; i < keys.length; i++ ){
//str is regex
if ( reg.test( keys[i]) ){
matches.push( keys[i] );
}
}
return matches;
}

}


module.exports = search;
Loading