2

I need to ensure that certain strings are valid questions. I am thinking of how to write this myself, but without being pretty limiting it is actually a fairly complex bit of analysis, and I'm sure it must have been done many times before.

I'm not just talking about starting with an interrogatory word and ending with a question mark. For example I would like all these to pass:

  1. Is the grand canyon large?
  2. I know canyons are usually pretty big, but is the grand canyon large?
  3. I was thinking earlier, is the grand canyon a large canyon?

I would love to get hold of an existing js lib (or port something) that does this for me. I've been looking around for a while but have found nothing.

  • P.S. I had to use the word 'inquiry' in the title as the word 'question' is banned.
zero298
  • 25,467
  • 10
  • 75
  • 100
James Trickey
  • 1,322
  • 13
  • 21
  • This extends beyond the language you're using and into linguistic semantic parsing, a complex and (highly sough after) professional speciality. I'd be surprised if a JS library exists for this, most natural language processing code is proprietary and owned by the big guys (Google, MS, Amazon etc). – James Gould Aug 24 '17 at 10:32
  • 1
    [compromise.js](http://compromise.cool/) is a JS NLP engine, it even has a `.questions()` subset. Of course its not a magical fix-all so YMMV. – Alex K. Aug 24 '17 at 10:40
  • @Jay Gould - Hey - thanks for getting back. Yes I agree, but I though it may be common enough that there might be something, as there are for detecting potentially offensive language. Though these often use a less semantic / brute force approach. – James Trickey Aug 24 '17 at 10:44
  • @Alex K - Oh wow - at a glance this looks exactly the sort of thing I'm looking for! Thanks, I'll investigate now! – James Trickey Aug 24 '17 at 10:46
  • Google Cloud has an NLP API as well, paid for I think. – Alex K. Aug 24 '17 at 10:53
  • @Alex K - Oooh that is interesting too, just checked it out. I'm probably going to be releasing on GCloude so may come in handy. For the time being though, I just plugged in compromise.js and it did exactly what I needed! For a second there my faith in npm was momentarily shaken!! Thanks mate! – James Trickey Aug 24 '17 at 11:00

1 Answers1

5

Alex K in the comments under my question gave me the exact answer I needed.

compromise.js

Awesome! :-)

Edit:

As requested, here is a working example.

In your terminal, with npm installed, type the following:

npm install compromise

Now to test if a string (myString) contains a question you would use the following:

var nlp = require('compromise');
var containsQuestion = nlp(myString).questions().data().length === 1;

(In the above case, multiple questions also results in a negative result)

James Trickey
  • 1,322
  • 13
  • 21