H
HansDev
I just finished a challenge (noted above) and I was wondering how good is this approach or is there some simpler solution that I am not aware of?
If you have a different solution, try to explain the code for people less experienced than myself rather just than pasting a solution.
Here is an example text file to be read from:
This file has eight words in this sentence.
There are two sentences. In this paragraph.
And two paragraphs in total.
Lorem ispsum.
Lorem ispsum.
Lorem ispsum.
Lorem ispsum.
/**
* Write a simple script to read text from a file and compute (approximately) the number of sentences, number of unique words and punctuation/symbols.
* Run the code with node ./problem-2.js test.txt
*/
// Use Node's builtin api to read a file
const fs = require('fs');
//#1 count the number of words
const wordCount = (string) => string.split(" ").length;
//#2 count the number of unique words
const uniqueWords = txt => new Set(txt.toLowerCase().match(/\w+/g)).size;
//#3 count the number of paragraphs
const paragraphCount = (paragraphString) =>{
//seperate each sentence into a seperate string
paragraphString = paragraphString.split("\n\n");
// console.table(paragraphString) // show as a table for better visual
let paragraphArray = paragraphString.length; // length of paragraph array
let paragraphCount = 0;
let strip_whitespace = /\s+/gi;
while (paragraphArray >=0) {
paragraphArray--;
let tmp = paragraphString[paragraphArray];
tmp = tmp ? tmp .replace(strip_whitespace,"") : tmp;
if( tmp && tmp.length > 1 ) paragraphCount++;
}
return paragraphCount;
}
function countPunctuations(punctuationsStrings) {
let punctuationsCount = 0;
const punctuations = [".", ",", "!", "?"];
// const symbols = ["~","`","!","@","#","$","%","^","&","*","(",")","_","+","-","=","{","}","|","[","]","\",":",";","'","<",]","?","/",]
for (const ch of punctuationsStrings) {
if (punctuations.includes(ch)) punctuationsCount++;
}
return punctuationsCount;
}
fs.readFile("test-2.txt", 'utf8', function(err, data) {
if (err) throw err;
console.log("The text in the file:\n\n", data,"\n")
// store results in an object to present the log better
let result={
"word count": wordCount(data),
"unique words": uniqueWords(data),
"paragraph count": paragraphCount(data),
"punctuation count": countPunctuations(data),
}
console.table(result)
});
```
If you have a different solution, try to explain the code for people less experienced than myself rather just than pasting a solution.
Here is an example text file to be read from:
This file has eight words in this sentence.
There are two sentences. In this paragraph.
And two paragraphs in total.
Lorem ispsum.
Lorem ispsum.
Lorem ispsum.
Lorem ispsum.
/**
* Write a simple script to read text from a file and compute (approximately) the number of sentences, number of unique words and punctuation/symbols.
* Run the code with node ./problem-2.js test.txt
*/
// Use Node's builtin api to read a file
const fs = require('fs');
//#1 count the number of words
const wordCount = (string) => string.split(" ").length;
//#2 count the number of unique words
const uniqueWords = txt => new Set(txt.toLowerCase().match(/\w+/g)).size;
//#3 count the number of paragraphs
const paragraphCount = (paragraphString) =>{
//seperate each sentence into a seperate string
paragraphString = paragraphString.split("\n\n");
// console.table(paragraphString) // show as a table for better visual
let paragraphArray = paragraphString.length; // length of paragraph array
let paragraphCount = 0;
let strip_whitespace = /\s+/gi;
while (paragraphArray >=0) {
paragraphArray--;
let tmp = paragraphString[paragraphArray];
tmp = tmp ? tmp .replace(strip_whitespace,"") : tmp;
if( tmp && tmp.length > 1 ) paragraphCount++;
}
return paragraphCount;
}
function countPunctuations(punctuationsStrings) {
let punctuationsCount = 0;
const punctuations = [".", ",", "!", "?"];
// const symbols = ["~","`","!","@","#","$","%","^","&","*","(",")","_","+","-","=","{","}","|","[","]","\",":",";","'","<",]","?","/",]
for (const ch of punctuationsStrings) {
if (punctuations.includes(ch)) punctuationsCount++;
}
return punctuationsCount;
}
fs.readFile("test-2.txt", 'utf8', function(err, data) {
if (err) throw err;
console.log("The text in the file:\n\n", data,"\n")
// store results in an object to present the log better
let result={
"word count": wordCount(data),
"unique words": uniqueWords(data),
"paragraph count": paragraphCount(data),
"punctuation count": countPunctuations(data),
}
console.table(result)
});
```