-
Notifications
You must be signed in to change notification settings - Fork 4k
Added word count operation #2039
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
Open
sw5678
wants to merge
8
commits into
gchq:master
Choose a base branch
from
sw5678:word_count2
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
d726f1e
Added word count operation
sw5678 cbf8a77
Fixed linting errors
sw5678 3366fe3
Fixed linting errors
sw5678 b314345
Merge branch 'gchq:master' into word_count2
sw5678 c2dce58
Made word count slightly better
sw5678 27e4d7e
Fixing linting issues
sw5678 43c4ca3
Merge branch 'master' into word_count2
C85297 c57180a
Merge branch 'master' into word_count2
GCHQDeveloper581 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next
Next commit
Added word count operation
- Loading branch information
commit d726f1e1df65a3c9db32d5d0ce9acea1ca094a07
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -337,7 +337,8 @@ | |
| "Sleep", | ||
| "File Tree", | ||
| "Take nth bytes", | ||
| "Drop nth bytes" | ||
| "Drop nth bytes", | ||
| "Word Count" | ||
| ] | ||
| }, | ||
| { | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,116 @@ | ||
| /** | ||
| * @author sw5678 | ||
| * @copyright Crown Copyright 2016 | ||
| * @license Apache-2.0 | ||
| */ | ||
|
|
||
| import Operation from "../Operation.mjs"; | ||
| import Utils from "../Utils.mjs"; | ||
| import {LETTER_DELIM_OPTIONS} from "../lib/Delim.mjs"; | ||
| import {caseInsensitiveSort} from "../lib/Sort.mjs"; | ||
|
|
||
|
|
||
| /** | ||
| * Word Count operation | ||
| */ | ||
| class WordCount extends Operation { | ||
|
|
||
| /** | ||
| * Word Count constructor | ||
| */ | ||
| constructor() { | ||
| super(); | ||
|
|
||
| this.name = "Word Count"; | ||
| this.module = "Default"; | ||
| this.description = "Provides a count of each word in a given text"; | ||
| this.inputType = "string"; | ||
| this.outputType = "string"; | ||
| this.args = [ | ||
| { | ||
| name: "Delimiter", | ||
| type: "option", | ||
| value: LETTER_DELIM_OPTIONS | ||
| }, | ||
| { | ||
| "name": "Include Total", | ||
| "type": "boolean", | ||
| "value": true | ||
| }, | ||
| { | ||
| "name": "Order", | ||
| "type": "option", | ||
| "value": ["Alphabetical", "Count"] | ||
| } | ||
| ]; | ||
| } | ||
|
|
||
| /** | ||
| * @param {string} input | ||
| * @param {Object[]} args | ||
| * @returns {string} | ||
| */ | ||
| run(input, args) { | ||
|
|
||
| const delimiter = Utils.charRep(args[0]); | ||
|
|
||
| // Lower case and split | ||
| const inputArray = input.replace(/(?:\r\n|\r|\n)/g, delimiter).toLowerCase().split(delimiter); | ||
|
|
||
| // Count up the words | ||
| const counter = {}; | ||
| let total = 0; | ||
| for (let j = 0; j < inputArray.length; j++) { | ||
|
|
||
| // Trim whitespace and replace punctuation | ||
| const word = inputArray[j].replace(/(?:!|"|#|\$|%|&|\(|\)|\*|\+|,|-|\.|\/|:|;|<|=|>|\?|@|\[|\\|\]|\^|_|`|\{|\||\}|~|£)/g, "").trim(); | ||
|
|
||
| // If empty string or ', then skip | ||
| if (word === "" || /[']+/.test(word)) { | ||
| continue; | ||
| } else if (word in counter) { | ||
| counter[word]++; | ||
| total++; | ||
| } else { | ||
| counter[word] = 1; | ||
| total++; | ||
| } | ||
| } | ||
|
|
||
| // Sort results | ||
| let order; | ||
| if (args[2] === "Alphabetical") { | ||
| // Sort alphabetically | ||
| order = Object.keys(counter).sort(caseInsensitiveSort); | ||
| } else if (args[2] === "Count") { | ||
| // Sort by count | ||
| // Create the array of key-value pairs | ||
| order = Object.keys(counter).map((key) => { | ||
| return [key, counter[key]]; | ||
| }); | ||
| // Sort the array based on the second element (i.e. the value) | ||
| order.sort((first, second) => { | ||
| return second[1] - first[1]; | ||
| }); | ||
| // Obtain the list of keys in sorted order of the values. | ||
| order = order.map((e) => { | ||
| return e[0]; | ||
| }); | ||
| } | ||
|
|
||
| // Process output to string | ||
| let output = "WORD,COUNT\n"; | ||
| for (let k = 0; k < order.length; k++) { | ||
|
sw5678 marked this conversation as resolved.
Outdated
|
||
| output = output + order[k] + "," + counter[order[k]] + "\n"; | ||
| } | ||
|
|
||
| // Add total counter at the bottom | ||
| if (args[1]) { | ||
| output = output + "TOTAL," + total; | ||
| } | ||
|
|
||
| return output; | ||
| } | ||
| } | ||
|
|
||
| export default WordCount; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,117 @@ | ||
| /** | ||
| * @author sw5678 | ||
| * @copyright Crown Copyright 2023 | ||
| * @license Apache-2.0 | ||
| */ | ||
| import TestRegister from "../../lib/TestRegister.mjs"; | ||
|
|
||
| TestRegister.addTests([ | ||
| { | ||
| "name": "Word Count: Empty test 1", | ||
| "input": "", | ||
| "expectedOutput": "WORD,COUNT\nTOTAL,0", | ||
|
|
||
| "recipeConfig": [ | ||
| { | ||
| "op": "Word Count", | ||
| "args": ["Space", true, "Alphabetical"], | ||
| }, | ||
| ], | ||
| }, | ||
| { | ||
| "name": "Word Count: Empty test 2", | ||
| "input": "", | ||
| "expectedOutput": "WORD,COUNT\nTOTAL,0", | ||
|
|
||
| "recipeConfig": [ | ||
| { | ||
| "op": "Word Count", | ||
| "args": ["Space", true, "Count"], | ||
| }, | ||
| ], | ||
| }, | ||
| { | ||
| "name": "Word Count: Empty test 3", | ||
| "input": "", | ||
| "expectedOutput": "WORD,COUNT\n", | ||
|
|
||
| "recipeConfig": [ | ||
| { | ||
| "op": "Word Count", | ||
| "args": ["Space", false, "Alphabetical"], | ||
| }, | ||
| ], | ||
| }, | ||
| { | ||
| "name": "Word Count: Empty test 4", | ||
| "input": "", | ||
| "expectedOutput": "WORD,COUNT\n", | ||
|
|
||
| "recipeConfig": [ | ||
| { | ||
| "op": "Word Count", | ||
| "args": ["Space", false, "Count"], | ||
| }, | ||
| ], | ||
| }, | ||
| { | ||
| "name": "Word Count: Count test 1", | ||
| "input": "Hello world. Hello. \n\n World, ''!@£$%^&*()_+=-[]{};'|:/.,<>? world", | ||
| "expectedOutput": "WORD,COUNT\nhello,2\nworld,3\nTOTAL,5", | ||
|
|
||
| "recipeConfig": [ | ||
| { | ||
| "op": "Word Count", | ||
| "args": ["Space", true, "Alphabetical"], | ||
| }, | ||
| ], | ||
| }, | ||
| { | ||
| "name": "Word Count: Count test 2", | ||
| "input": "Hello world. Hello. \n\n World, ''!@£$%^&*()_+=-[]{};'|:/.,<>? world", | ||
| "expectedOutput": "WORD,COUNT\nworld,3\nhello,2\nTOTAL,5", | ||
|
|
||
| "recipeConfig": [ | ||
| { | ||
| "op": "Word Count", | ||
| "args": ["Space", true, "Count"], | ||
| }, | ||
| ], | ||
| }, | ||
| { | ||
| "name": "Word Count: Count test 3", | ||
| "input": "Hello world. Hello. \n\n World, ''!@£$%^&*()_+=-[]{};'|:/.,<>? world", | ||
| "expectedOutput": "WORD,COUNT\nhello,2\nworld,3\n", | ||
|
|
||
| "recipeConfig": [ | ||
| { | ||
| "op": "Word Count", | ||
| "args": ["Space", false, "Alphabetical"], | ||
| }, | ||
| ], | ||
| }, | ||
| { | ||
| "name": "Word Count: Count test 4", | ||
| "input": "Hello world. Hello. \n\n World, ''!@£$%^&*()_+=-[]{};'|:/.,<>? world", | ||
| "expectedOutput": "WORD,COUNT\nworld,3\nhello,2\n", | ||
|
|
||
| "recipeConfig": [ | ||
| { | ||
| "op": "Word Count", | ||
| "args": ["Space", false, "Count"], | ||
| }, | ||
| ], | ||
| }, | ||
| { | ||
| "name": "Word Count: Different delimiter test", | ||
| "input": "Hello, World\nhello, world \n''!@£$%^&*()_+=-[]{};'|:/.,<>? world", | ||
| "expectedOutput": "WORD,COUNT\nworld,3\nhello,2\n", | ||
|
|
||
| "recipeConfig": [ | ||
| { | ||
| "op": "Word Count", | ||
| "args": ["Comma", false, "Count"], | ||
| }, | ||
| ], | ||
| } | ||
| ]); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.