Search code examples
typescriptstop-words

TypeError: Cannot read property 'removeStopwords' of undefined


I am working on a project using stopwords and typescript and I am receiving the below error I have attempted to debug by removing the !string.trim() and replacing with string.trim() and I receive an output of

0 0

Also have attempted to give fixedSpelling a type of any and also the getSentiment(str:string | undefined) but no success..

// @ts-ignore
import aposToLexForm from "apos-to-lex-form";
import { WordTokenizer } from "natural";
// @ts-ignore
import SpellCorrector from 'spelling-corrector';
import stopword from "stopword";

const tokenizer = new WordTokenizer();
const spellCorrector = new SpellCorrector();
spellCorrector.loadDictionary();

function getSentiment(str: string): -1 | 0 | 1 {
    if (!str.trim()) {
        return 0;
    }

const lexed = aposToLexForm(str).toLowerCase().replace(/[^a-zA-Z\s]+/g, "");

const tokenized = tokenizer.tokenize(lexed);

const fixedSpelling = tokenized.map((word) => spellCorrector.correct(word));

const stopWordsRemoved = stopword.removeStopwords(fixedSpelling);

console.log(stopWordsRemoved);

return 0;

}

console.log(getSentiment('This is awesome!'))
console.log(getSentiment('Logging is super duper cool!'))

error being received -

TypeError: Cannot read property 'removeStopwords' of undefined
    at getSentiment (/home/karlito/projects/twitter/src/app.ts:23:39)
    at Object.<anonymous> (/home/karlito/projects/twitter/src/app.ts:31:13)
    at Module._compile (node:internal/modules/cjs/loader:1109:14)
    at Module._compile (/home/karlito/projects/twitter/node_modules/source-map-support/source-map-support.js:568:25)
    at Module.m._compile (/tmp/ts-node-dev-hook-30469514417241084.js:69:33)
    at Module._extensions..js (node:internal/modules/cjs/loader:1138:10)
    at require.extensions..jsx.require.extensions..js (/tmp/ts-node-dev-hook-30469514417241084.js:114:20)
    at require.extensions.<computed> (/tmp/ts-node-dev-hook-30469514417241084.js:71:20)
    at Object.nodeDevHook [as .ts] (/home/karlito/projects/twitter/node_modules/ts-node-dev/lib/hook.js:63:13)
    at Module.load (node:internal/modules/cjs/loader:989:32)
[ERROR] 18:44:36 TypeError: Cannot read property 'removeStopwords' of undefined

Solution

  • It looks like 'stopword' does not have a default export. You could to change your import statement to:

    import * as stopword from 'stopword'
    

    Or without the stopword module:

    import { removeStopwords } from 'stopword'
    

    Stopword docs