Kevin Whitaker
Kevin Whitaker

Reputation: 13435

Pluralize/singularize a sentence using the compromise library in javascript

I'm trying to use the compromise library to pluralize/singularize sentences that take these forms:

const str1 = "10 dog";
const str2 = "a man and 3 cat";
const str3 = "a bear";

If I use the pluralize method from compromise, it returns the list of plural nouns or verbs, but I'm trying to figure out how to get it to pluralize the entire string.

const nlp = require("compromise");
const doc = nlp(str2);
const finalStr = doc.nouns().toPlural().out("normal"); // "man cats" instead of "a man and 3 cats";

Upvotes: 0

Views: 877

Answers (1)

Peter Kim
Peter Kim

Reputation: 1977

This will work because it will identify all the nouns first:

doc.match("#Noun").nouns().toPlural().out("normal");

edit

I see where the problem came from. Take a look at below two console logs:

const nlp = require("compromise")
let doc = nlp("a man and 3 cat")
doc.match("#Noun").nouns().toPlural()
console.log(doc.out("normal")) // "a man and 3 cats"

let doc2 = doc.match("#Noun").nouns().toPlural()
console.log(doc2.out("normal")) // "man cats"

Note that in the second example, I am saving it into a new variable. So I guess .match("#Noun") was not necessary, and you were right the first time -- just don't save it into finalStr.

Upvotes: 3

Related Questions