Search code examples
javascriptstringstring-parsing

How to check if string is a valid inquiry


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.

Solution

  • 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)