Is there a library that can preform the following transformations?

"5 servings" => {num: 5, name: "servings"}
"five blocks" => {num: 5, name: "blocks"}
"five and a half teasopons of sugar" => {num: 5.5, name: "teaspoons of sugar"}
"four hundred milligrams of iron" => {num: 400, name: "milligrams of iron"}

It would be even better if the unit could be seperated out to.

1 Answer 1


There is a JavaScript library called nlp_compromise.

nlp_compromise is a cool way to use natural language in javascript.

This is an open source library and its source code is available here. According to your question, this library can detect a number, unit, and noun. Here are below examples that I taken from nlp_compromise documentation:

nlp_compromise = {
  value :{
    number: Number,      //fifty kilometers -> 50
    unit: String,        //fifty km -> km
    unit_name: String,   //fifty km -> kilometer
    measurement: String, //fifty km -> distance

You also can get noun from a sentence. Please, take a look full API documentation here.

  • It does not accept fractions or decimals
    – 2426021684
    May 15, 2016 at 16:45
  • Also fails to parse 50 grams
    – 2426021684
    May 15, 2016 at 22:13
  • Yes, it is. This library is actively being developed right now. You can create an issue for that as your feature request.
    – meisyal
    Jul 19, 2017 at 4:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.