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 dicitonary reader #40

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
Binary file added .DS_Store
Binary file not shown.
Empty file added data/dictionarytest.json
Empty file.
Binary file added lib/.cli_user.js.swp
Binary file not shown.
Binary file added lib/.dictionary_data.js.swp
Binary file not shown.
Binary file added lib/.loading.js.swp
Binary file not shown.
Binary file added lib/.search.js.swp
Binary file not shown.
113 changes: 113 additions & 0 deletions lib/cli_user.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
let path = require('path');

console.log(`
Welcome to the node dictionary reader
=====================================
Type q to quit
`)

let loader = require('./loading.js');


function one() {

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

// Inline function to handle
// message output

var dictarr;

var showMessage = (err) => {
console.log('Select from available dictionaries');
loader.availableDictionaries().then((result) => {
let i = 0;
result.forEach((item) => {
console.log(i + ". " + item);
i++;
})

dictarr = result
});
}
/* console.log(item)}
console.log("1: " + result[0])});
loader.availableDictionaries().then(
if (err) {
console.error(err);
}
};
*/
// Display message
showMessage();


// 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);

// ----------------------------------------
// Go to next view here
// ----------------------------------------

} else if (Number(data) != NaN) {

let num = Number(data)

//console.log('this is the output of the array' + ' ' + dictarr[num]);
var pathname = path.join(__dirname, "..", "/data" + `/${dictarr[num]}`)

//loader.loadFile(pathname)

loader.loadFile(pathname).then((result) => {
let letterCount = (words) => {
letter_count = {};
for (let i = 0; i < words.length; i++) {
if (letter_count[words[i][0].toLowerCase()] === undefined) {
letter_count[words[i][0].toLowerCase()] = 1;
} else {
letter_count[words[i][0].toLowerCase()] += 1;
}
}
return letter_count;
}
// Logging letter counts
console.log(letterCount(Object.keys(result)));

//creating variable for the dictionary's data

var dictionaryData = result;

let search = require('./search.js')

//console.log(search.exactMatch(dictionaryData, 'abate'));

console.log(search.beginMatch(dictionaryData, 'ab'));
//return result
})


//console.log(search.exactMatch(dictionaryData, 'abate'))
} else {

// All other input is invalid
showMessage(`Invalid: ${ data }`);
}
};

// Set the listener
process.stdin.on('data', onData);
}

// Start the app
one();
22 changes: 22 additions & 0 deletions lib/dictionary_data.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@

// Dictionary data module

let letterCount = (words) => {
letter_count = {};
for (let i = 0; i < words.length; i++) {
if (letter_count[words[i][0].toLowerCase()] === undefined) {
letter_count[words[i][0].toLowerCase()] = 1;
} else {
letter_count[words[i][0].toLowerCase()] += 1;
}
}
return letter_count;
}




module.exports.loadInformation = (data) => {
console.log("Successfully loaded data");
console.log("Word count: ");
}
50 changes: 50 additions & 0 deletions lib/loading.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
var fs = require('fs')
let path = require('path');

//let dictionaryPath = path.join(__dirname, "..", "/data/dictionary.json");

module.exports.loadFile = (chosenPath) => {
/*
if(num == 1){
var chosenPath = path.join(__dirname, "..", "/data/dictionary.json")
}
else if(num == 2){
var chosenPath = path.join(__dirname, "..", "/data/dictionarytest.json")
}
else
*/
return new Promise((resolve, reject) => {
fs.readFile(chosenPath, 'utf8', (err, data) => {
//if(err){throw 'file not found'}
/*
return new Promise((resolve, reject) => {
resolve(JSON.parse(data))
})
*/
if(err){
reject('loadfile failed')
}
resolve(JSON.parse(data));
})
})
}

let dirPath = path.join(__dirname, "..", "/data");

module.exports.availableDictionaries = () => {
return new Promise((resolve, reject) => {
fs.readdir(dirPath, (err, data) => {
resolve(data)
//console.log(data);
});
})
}

// module.exports.loadFile = loadFile;
// module.exports.availableDictionaries = availableDictionaries;


// = {
// "loadFile": loadFile,
// "availableDictionaries": availableDictionaries
// }
21 changes: 21 additions & 0 deletions lib/search.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
const exactMatch = (dictionaryData, searchWord) => {
let keys = Object.keys(dictionaryData)
let match = new RegExp(`${searchWord}`)
return match.exec(keys)[0]
}

const beginMatch = (dictionaryData, beginsWith) => {
let keys = Object.keys(dictionaryData)
return keys.map((item) => {
if(item.indexOf(beginsWith) === 0){
return item
}
})
//return match.exec(keys)[0]

}

module.exports = {
beginMatch,
exactMatch
}
139 changes: 139 additions & 0 deletions warmup/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
function one() {

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

// Inline function to handle
// message output
var showMessage = (err) => {
console.log('State one');
console.log('Type "next" to continue');
if (err) {
console.error(err);
}
};

// Display message
showMessage();


// 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);

two();

} else {

// All other input is invalid
showMessage(`Invalid: ${ data }`);
}
};

// Set the listener
process.stdin.on('data', onData);
}

// Start the app
one();

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

// Inline function to handle
// message output
var showMessage = (err) => {
console.log('State two');
console.log('Type "next" to continue');
if (err) {
console.error(err);
}
};

showMessage();

// 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);

three();

} else {

// All other input is invalid
showMessage(`Invalid: ${ data }`);
}

};

// Set the listener
process.stdin.on('data', onData);

}


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

// Inline function to handle
// message output
var showMessage = (err) => {
console.log('State three');
console.log('Type "next" to continue');
if (err) {
console.error(err);
}
};

showMessage();

// 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);

console.log("Goodbye");
process.exit();

} else {

// All other input is invalid
showMessage(`Invalid: ${ data }`);
}

};

// Set the listener
process.stdin.on('data', onData);

}