diff --git a/fileSaves/dummyFile.txt b/fileSaves/dummyFile.txt new file mode 100644 index 0000000..4c066fc --- /dev/null +++ b/fileSaves/dummyFile.txt @@ -0,0 +1,61 @@ +afresh +alfresco +comfrey +confrere +freak +freakish +freckle +freckled +freckly +fred +free +freebooter +freeborn +freedman +freehold +freeholder +freely +freemason +freemasonry +freeness +freestone +freethinker +freethinking +freewheel +freewill +freezable +freeze +freezer +freezing +freight +french +frenchman +frenetic +frenetical +frenum +frenzied +frenzy +frequentative +frequenter +frequently +frequentness +fresco +fresh +freshen +freshly +freshman +freshness +fret +fretful +fretted +fretter +fretwork +freya +infrequent +infrequently +refresh +refresher +refreshing +unfree +unfreeze +unfrequented \ No newline at end of file diff --git a/index.js b/index.js new file mode 100644 index 0000000..079e083 --- /dev/null +++ b/index.js @@ -0,0 +1,155 @@ +var dictionaries = require("./lib/loader.js"); +var dictionary = require("./lib/dictionary.js"); +var search = require("./lib/search.js"); + +var found = []; +// Start listening to STDIN +process.stdin.resume(); +process.stdin.setEncoding("utf8"); + +let dicArray = [""]; +let dictPath = ""; + +function start() { + // Start listening to STDIN + process.stdin.resume(); + process.stdin.setEncoding("utf8"); + + // Set the listener + + // Inline function to handle + // message output + let showMessage = err => { + console.log("Welcome to the Node Dictionary Reader!"); + console.log("======================================"); + console.log("Enter q to quit"); + dicArray = dictionaries.arrayDictionaries; + dictionaries.loaderFunction(); + if (err) { + console.error(err); + } + }; + + // Display message + showMessage(); + + // Handler for starting input + let onData = data => { + data = data.trim(); + + if (data === "q") { + process.exit(); + } else if (!isNaN(data) && dicArray[Number(data) - 1]) { + dictPath = "./data/" + dicArray[Number(data) - 1]; + dictionary(dicArray[Number(data) - 1], () => { + console.log("What kind of search would you like to perform?"); + console.log("1. Exact"); + console.log("2. Partial"); + console.log("3. Begins With"); + console.log("4. Ends With"); + process.stdin.removeAllListeners("data"); + process.stdin.on("data", onSearch); + }); + } else { + console.log("Invalid choice"); + } + }; + + //Enter search term + + let onSearch = data => { + let searchType = data.trim(); + console.log("Please enter your search term:"); + process.stdin.removeAllListeners("data"); + process.stdin.on("data", data => { + let searchTerm = data.trim(); + dictionaries.loadDictionary(dictPath).then(loadedDictionary => { + switch (searchType) { + case "1": + found = search(loadedDictionary, "exact", searchTerm); + break; + case "2": + found = search(loadedDictionary, "partial", searchTerm); + break; + case "3": + found = search(loadedDictionary, "begins", searchTerm); + break; + case "4": + found = search(loadedDictionary, "ends", searchTerm); + break; + default: + console.log("Invalid choice"); + } + process.stdin.removeAllListeners("data"); + console.log("Do you want to save the results to a file? y/n?"); + process.stdin.on("data", onSaveChoice); + }); + }); + }; + + //do they want to save + + const onSaveChoice = data => { + data = data.trim(); + if (data === "y") { + console.log("What filepath should we write results to?"); + process.stdin.removeAllListeners("data"); + process.stdin.on("data", onSave); + } else if (data === "n") { + process.stdin.removeAllListeners("data"); + start(); + } else if (data === "q") { + process.exit(); + } + }; + + //determines if existing file + + let onSave = data => { + data = data.trim(); + let savePath = "./fileSaves/" + data; + if (fs.existsSync(savePath)) { + console.log("That file exists, overwrite? y/n?"); + process.stdin.removeAllListeners("data"); + process.stdin.on("data", data => { + data = data.trim(); + saveOptions(data, savePath); + }); + } else { + saveFile(savePath); + } + }; + + //determine if you want to overwrite + + let saveOptions = (data, savePath) => { + console.log("In saveOptions"); + if (data == "y") { + saveFile(savePath); + } else if (data == "n") { + process.stdin.removeAllListeners("data"); + start(); + } else if (data == "q") { + process.exit(); + } + }; + + //saves file + + let saveFile = savePath => { + console.log(savePath); + fs.writeFile(savePath, found.join("\n"), "utf8", err => { + if (err) { + throw err; + } + console.log("File saved."); + }); + process.stdin.removeAllListeners("data"); + start(); + }; + + process.stdin.on("data", onData); +} + +// Start the app +start(); diff --git a/lib/dictionary.js b/lib/dictionary.js new file mode 100644 index 0000000..8b15167 --- /dev/null +++ b/lib/dictionary.js @@ -0,0 +1,30 @@ +var fs = require("fs"); + +function loadDictionary(path, callback) { + fs.readFile("./data/" + path, "utf8", (err, data) => { + if (err) { + throw err; + } + console.log(`Successfully loaded ${path}!`); + data = JSON.parse(data); + console.log("number of keys " + Object.keys(data).length); + console.log("Word frequency by starting letter: "); + var alphabet = "abcdefghijklmnopqrstuvwxyz".split(""); + var letterCount = new Array(26).fill(0); + var keys = Object.keys(data); + for (let i = 0; i < alphabet.length; i++) { + let letter = alphabet[i]; + let regex = "^" + letter + "+"; + regex = new RegExp(regex, "gi"); + for (let n = 0; n < keys.length; n++) { + if (regex.test(keys[n])) { + letterCount[i] += 1; + } + } + console.log(alphabet[i] + ": " + letterCount[i]); + } + callback(); + }); +} + +module.exports = loadDictionary; diff --git a/lib/loader.js b/lib/loader.js new file mode 100644 index 0000000..cefed4e --- /dev/null +++ b/lib/loader.js @@ -0,0 +1,36 @@ +fs = require("fs"); + +let dictionaries = { + arrayDictionaries: fs.readdirSync("./data/", (err, data) => { + if (err) { + throw err; + } + return data; + }), + loaderFunction: function loader() { + fs.readdir("./data/", (err, data) => { + if (err) { + throw err; + } + console.log("Select a dictionary to load:"); + for (let i = 0; i < data.length; i++) { + console.log(i + 1 + ". " + data[i]); + } + return data; + }); + }, + loadDictionary: path => { + let dictContents = new Promise(function(resolve, reject) { + fs.readFile(path, "utf8", (err, data) => { + if (err) { + throw err; + } + data = JSON.parse(data); + resolve(data); + }) + }) + return dictContents; + } +}; + +module.exports = dictionaries; diff --git a/lib/search.js b/lib/search.js new file mode 100644 index 0000000..b3b63b1 --- /dev/null +++ b/lib/search.js @@ -0,0 +1,52 @@ +function search(dictionary, userChoice, searchTerm) { + var keys = Object.keys(dictionary); + let found = []; + const findMatches = (keys, regex) => { + for (let i = 0; i < keys.length; i++) { + if (regex.test(keys[i])) { + found.push(keys[i]); + } + } + return found; + }; + const printMatches = found => { + console.log("Found " + found.length + " Matches:"); + for (let i = 0; i < found.length; i++) { + console.log(found[i]); + } + }; + var searchType = { + exact: (keys, searchTerm) => { + for (let i = 0; i < keys.length; i++) { + if (keys[i] === searchTerm) { + console.log("Found a match"); + console.log(searchTerm); + return [searchTerm]; + } + } + }, + partial: (keys, searchTerm) => { + let regex = new RegExp(searchTerm); + let found = findMatches(keys, regex); + printMatches(found); + return found; + }, + begins: (keys, searchTerm) => { + searchTerm = "^" + searchTerm; + let regex = new RegExp(searchTerm); + let found = findMatches(keys, regex); + printMatches(found); + return found; + }, + ends: (keys, searchTerm) => { + searchTerm = searchTerm + "$"; + let regex = new RegExp(searchTerm); + let found = findMatches(keys, regex); + printMatches(found); + return found; + } + }; + return searchType[userChoice](keys, searchTerm); +} + +module.exports = search; diff --git a/warmup/index.js b/warmup/index.js new file mode 100644 index 0000000..1a09a16 --- /dev/null +++ b/warmup/index.js @@ -0,0 +1,66 @@ +// Start listening to STDIN +process.stdin.resume(); +process.stdin.setEncoding('utf8'); + +let state = "State one"; + +function one(state) { + + // Start listening to STDIN + process.stdin.resume(); + process.stdin.setEncoding('utf8'); + + // Inline function to handle + // message output + var showMessage = (state, err) => { + console.log(state); + console.log('Type "next" to continue'); + if (err) { + console.error(err); + } + }; + + // Display message + showMessage(state); + + + // Handler for STDIN data + // event + var onData = (data) => { + data = data.trim(); + + // If user input "next" + // let's go to the next + // state + if (data === 'next') { + process.stdin.pause(); + process.stdin.removeListener('data', onData); + + if (state === "State one") { + two(state); + } else if (state === "State two") { + three(state); + } + + } else { + + // All other input is invalid + showMessage(`Invalid: ${ data }`); + } + }; + + // Set the listener + process.stdin.on('data', onData); +} + +function two() { + state = "State two"; + one(state); +} + +function three() { + console.log("Goodbye"); +} + +// Start the app +one(state);