-
Notifications
You must be signed in to change notification settings - Fork 1
Remove stopwords refacture #4
base: master
Are you sure you want to change the base?
Changes from all commits
03ae974
4272c97
ffb883f
9b9172f
97a025f
28eab83
cb90bb5
deb2721
6ab66a1
d2d1e48
d2a640b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1 @@ | ||
| export const WORD_DECIMETER = /[^\b\s ()?!:,|\[\]+.\-;"^'`´&]+/g; | ||
| export const WORD_DECIMETER = /[^\b\s ()?!:,|\[\]+.\-;"^'`´&]+/g; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| // SUGGESTIONS: refacture ino shouldDisplay | ||
| // and this function take a word and the language, then it returns true or false, and this function should be used wherever you are trying to display the words | ||
|
|
||
| import { STOPWORDS } from './stopwords'; | ||
|
|
||
| const filterWords = (wordCountObject, wordsToFilterOut) => { | ||
| for (var key in wordCountObject) { | ||
| var containsWord = (wordsToFilterOut.indexOf(key) > -1); | ||
| if (containsWord) { | ||
| delete wordCountObject[key]; | ||
| } | ||
| } | ||
| return wordCountObject; | ||
| } | ||
|
|
||
| export const removeStopwords = (wordCountObject, language) => { | ||
| var stopwords = STOPWORDS[language]; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. right now this function depends on (tightly coupled with) the actual words. so in your tests you have to use one of these words from the original stopwords. to avoid this behavior, we could do one of:
for example: export const createRemoveStopwords =(stopwords)=> (wordCountObject, language) => {
// logic here ..
}and then we export another function like that: export const removeStopwords(STOPWORDS);and when we test, we use custom/simple stop words: +import { createRemoveStopwords } from './removeStopwords';
it( 'should remove stopords', () => {
let stopWords = {'en': ['word1', 'stopword2', 'thirdword']}
let mockedWords = { 'word1': 1, 'stays': 1, "'stopword2": 1,"'thirdword":1};
let mockedLanguage = 'en';
let expectedResult = {
'stays': 1
};
let result = removeStopwords( mockedWords, mockedLanguage );
expect( result ).toEqual( expectedResult );
} );
} );
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I do not really understand what you mean by this:
|
||
| var filteredObject = Object.assign(wordCountObject); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What does this line do?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This creates a new object. So that we don not mutate the original object, but instead have two separate ones.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. but this is not what this line does. check
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Check out this codepen. Here I create a new object and then manipulate it without the first object being mutated. |
||
| return filterWords(filteredObject, stopwords) | ||
|
|
||
| // TODO: MOVE HTML | ||
| // // get element | ||
| // div = document.getElementById("wordsByUsage"+i); | ||
| // // remove old content | ||
| // div.innerHTML = ""; | ||
| // // add new content | ||
| // if (filteredWords[i].length > 29) { | ||
| // max = 30; | ||
| // } else { | ||
| // max = filteredWords[i].length; | ||
| // } | ||
| // var mostUsedHTML =""; | ||
| // for (var j = 0; j < max; j++) { | ||
| // mostUsedHTML = mostUsedHTML + "<p>" + filteredWords[i][j][0].substring(1) +" - "+ Math.round(filteredWords[i][j][1]/totalWords*1000)/10 + "%</p>"; | ||
| // } | ||
| // div.innerHTML = mostUsedHTML; | ||
|
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| import { removeStopwords } from './removeStopwords'; | ||
|
|
||
| describe( 'removeStopwords', () => { | ||
|
|
||
| it( 'should be function', () => { | ||
| expect( typeof removeStopwords ).toBe( 'function' ); | ||
| } ); | ||
|
|
||
| it( 'should remove stopwords', () => { | ||
| let mockedWords = { 'i': 1, 'asdasd': 1, "'ll": 1,"'tis":1,"'twas":1}; | ||
| let mockedLanguage = 'en'; | ||
| let expectedResult = { | ||
| 'asdasd': 1 | ||
| }; | ||
|
|
||
| let result = removeStopwords( mockedWords, mockedLanguage ); | ||
| expect( result ).toEqual( expectedResult ); | ||
|
|
||
| } ); | ||
|
|
||
|
|
||
| } ); |
Large diffs are not rendered by default.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
as discussed, this is risky as it mutates the original object.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
see comment above. Should be fine if this is a separate object.