javascriptnode.jsbrowsernlpstemming

NLP stemming with JavaScript and PHP pages in the browser


I'm trying to figure out, how to implement and use stemming results with JavaScript and PHP pages in the browser.

By using node index.js in the VS Code terminal I got the output word using natural:

var natural = require("natural");
console.log(natural.PorterStemmer.stem("words")); 

with NLP.js librarie:

const { StemmerIt } = require("@nlpjs/lang-it");
const stemmer = new StemmerIt();
const input = ["ho", "visto", "uno", "sviluppatore"];
console.log(stemmer.stem(input));

they are Node.js libraries, and I got an error in the Chrome browser console log using require:

Uncaught ReferenceError: require is not defined at index.js:320:15

I'm not sure if there is some way to use libraries with the browser, like for example Brain.js providing NPM and CND.

I did not try to use Compromise.cool library yet, which can be used to run NLP on the browser, looking for a stemming method.

I've tried Porters Stemming Algorithm Javascript but can't figure out, how to use it as a function. Seems like I'm doing it incorrectly:

var result = stemmer("words");
console.log(result);

Uncaught TypeError: stemmer is not a function at index.js:346:14

Also, I've tried to follow Natural Language Processing in the Browser guide, separately installing each dependency listed in package.json:

"@nlpjs/core": "^4.14.0",
"@nlpjs/lang-en-min": "^4.14.0",
"@nlpjs/nlp": "^4.15.0",
"@nlpjs/request-rn": "^4.14.3",
"browserify": "^17.0.0",
"terser": "^5.3.8"

but npm run build throws the error:

10 error missing script: build

Any advice, guide, or example would be useful.


Solution

  • For browser side NLP & stemming look at the example of stemming and lemmatization. This Observable notebook and the associated collection contain details of making pure browser side NLP apps.