Skip to content
This repository has been archived by the owner on Dec 10, 2019. It is now read-only.

Remove stopwords refacture #4

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
Open

Conversation

mowolf
Copy link
Member

@mowolf mowolf commented May 27, 2018

  • refactured removeStpwords

@mowolf mowolf requested a review from Nilegfx May 27, 2018 16:25

export const removeStopwords = (wordCountObject, language) => {
var stopwords = STOPWORDS[language];
var filteredObject = Object.assign(wordCountObject);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does this line do?

Copy link
Member Author

Choose a reason for hiding this comment

The 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.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

but this is not what this line does. check Object.assign method documentation.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Object.assign() method is used to copy the values of all enumerable own properties from one or more source objects to a target object. It will return the target object.

Check out this codepen. Here I create a new object and then manipulate it without the first object being mutated.
https://codepen.io/mowolf/pen/pKjGyb?editors=0012

for (var key in wordCountObject) {
var containsWord = (wordsToFilterOut.indexOf(key) > -1);
if (containsWord) {
delete wordCountObject[key];
Copy link
Contributor

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.

Copy link
Member Author

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.

expect( typeof removeStopwords ).toBe( 'function' );
} );

it( 'should remove stopords', () => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

stopords -> stopWords

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

jep

// 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 './constants';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

better to save STOPWORDS in a separate file as it is localization not code constants

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

jep

}

export const removeStopwords = (wordCountObject, language) => {
var stopwords = STOPWORDS[language];
Copy link
Contributor

Choose a reason for hiding this comment

The 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:

  • Mock STOPWORDS during the test (which is a bit complicated)
  • as STOPWORDS is a dependency that we can inject to this function and in tests we can inject different "simplified" words.

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

  } );
} );

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do not really understand what you mean by this:

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.

@mowolf
Copy link
Member Author

mowolf commented Jun 2, 2018

I dont know why the other tests that are not related fail...

@mowolf
Copy link
Member Author

mowolf commented Jun 2, 2018

I have also no Idea why the check works now... However now it seems fine

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants