Made word count slightly better
This commit is contained in:
parent
b3143451e6
commit
c2dce58db2
@ -63,7 +63,9 @@ class WordCount extends Operation {
|
|||||||
for (let j = 0; j < inputArray.length; j++) {
|
for (let j = 0; j < inputArray.length; j++) {
|
||||||
|
|
||||||
// Trim whitespace and replace punctuation
|
// Trim whitespace and replace punctuation
|
||||||
const word = inputArray[j].replace(/(?:!|"|#|\$|%|&|\(|\)|\*|\+|,|-|\.|\/|:|;|<|=|>|\?|@|\[|\\|\]|\^|_|`|\{|\||\}|~|£)/g, "").trim();
|
const word = inputArray[j].replace(/[!"#\$%&\(\)\*\+,-\.\/:;<=>\?@\[\\\]\^_`\{\\}~£\|]/g, "").trim();
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// If empty string or ', then skip
|
// If empty string or ', then skip
|
||||||
if (word === "" || /[']+/.test(word)) {
|
if (word === "" || /[']+/.test(word)) {
|
||||||
@ -100,13 +102,11 @@ class WordCount extends Operation {
|
|||||||
|
|
||||||
// Process output to string
|
// Process output to string
|
||||||
let output = "WORD,COUNT\n";
|
let output = "WORD,COUNT\n";
|
||||||
for (let k = 0; k < order.length; k++) {
|
output = output + order.map(entry => `${entry},${counter[entry]}`).join('\n');
|
||||||
output = output + order[k] + "," + counter[order[k]] + "\n";
|
|
||||||
}
|
|
||||||
|
|
||||||
// Add total counter at the bottom
|
// Add total counter at the bottom
|
||||||
if (args[1]) {
|
if (args[1]) {
|
||||||
output = output + "TOTAL," + total;
|
output = output + "\nTOTAL," + total;
|
||||||
}
|
}
|
||||||
|
|
||||||
return output;
|
return output;
|
||||||
|
|||||||
@ -9,7 +9,7 @@ TestRegister.addTests([
|
|||||||
{
|
{
|
||||||
"name": "Word Count: Empty test 1",
|
"name": "Word Count: Empty test 1",
|
||||||
"input": "",
|
"input": "",
|
||||||
"expectedOutput": "WORD,COUNT\nTOTAL,0",
|
"expectedOutput": "WORD,COUNT\n\nTOTAL,0",
|
||||||
|
|
||||||
"recipeConfig": [
|
"recipeConfig": [
|
||||||
{
|
{
|
||||||
@ -21,7 +21,7 @@ TestRegister.addTests([
|
|||||||
{
|
{
|
||||||
"name": "Word Count: Empty test 2",
|
"name": "Word Count: Empty test 2",
|
||||||
"input": "",
|
"input": "",
|
||||||
"expectedOutput": "WORD,COUNT\nTOTAL,0",
|
"expectedOutput": "WORD,COUNT\n\nTOTAL,0",
|
||||||
|
|
||||||
"recipeConfig": [
|
"recipeConfig": [
|
||||||
{
|
{
|
||||||
@ -81,7 +81,7 @@ TestRegister.addTests([
|
|||||||
{
|
{
|
||||||
"name": "Word Count: Count test 3",
|
"name": "Word Count: Count test 3",
|
||||||
"input": "Hello world. Hello. \n\n World, ''!@£$%^&*()_+=-[]{};'|:/.,<>? world",
|
"input": "Hello world. Hello. \n\n World, ''!@£$%^&*()_+=-[]{};'|:/.,<>? world",
|
||||||
"expectedOutput": "WORD,COUNT\nhello,2\nworld,3\n",
|
"expectedOutput": "WORD,COUNT\nhello,2\nworld,3",
|
||||||
|
|
||||||
"recipeConfig": [
|
"recipeConfig": [
|
||||||
{
|
{
|
||||||
@ -93,7 +93,7 @@ TestRegister.addTests([
|
|||||||
{
|
{
|
||||||
"name": "Word Count: Count test 4",
|
"name": "Word Count: Count test 4",
|
||||||
"input": "Hello world. Hello. \n\n World, ''!@£$%^&*()_+=-[]{};'|:/.,<>? world",
|
"input": "Hello world. Hello. \n\n World, ''!@£$%^&*()_+=-[]{};'|:/.,<>? world",
|
||||||
"expectedOutput": "WORD,COUNT\nworld,3\nhello,2\n",
|
"expectedOutput": "WORD,COUNT\nworld,3\nhello,2",
|
||||||
|
|
||||||
"recipeConfig": [
|
"recipeConfig": [
|
||||||
{
|
{
|
||||||
@ -105,7 +105,7 @@ TestRegister.addTests([
|
|||||||
{
|
{
|
||||||
"name": "Word Count: Different delimiter test",
|
"name": "Word Count: Different delimiter test",
|
||||||
"input": "Hello, World\nhello, world \n''!@£$%^&*()_+=-[]{};'|:/.,<>? world",
|
"input": "Hello, World\nhello, world \n''!@£$%^&*()_+=-[]{};'|:/.,<>? world",
|
||||||
"expectedOutput": "WORD,COUNT\nworld,3\nhello,2\n",
|
"expectedOutput": "WORD,COUNT\nworld,3\nhello,2",
|
||||||
|
|
||||||
"recipeConfig": [
|
"recipeConfig": [
|
||||||
{
|
{
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user