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

Finished dictionary reader #38

Open
wants to merge 18 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
61 changes: 61 additions & 0 deletions fileSaves/dummyFile.txt
Original file line number Diff line number Diff line change
@@ -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
155 changes: 155 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -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();
30 changes: 30 additions & 0 deletions lib/dictionary.js
Original file line number Diff line number Diff line change
@@ -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;
36 changes: 36 additions & 0 deletions lib/loader.js
Original file line number Diff line number Diff line change
@@ -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;
52 changes: 52 additions & 0 deletions lib/search.js
Original file line number Diff line number Diff line change
@@ -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;
Loading