I often develop bots and I need to understand what some people are saying.

- I want an apple
- I want an a p p l e

How do I find the object (apple)? I honestly don't know where to start looking. Is there an API that I can send the text to which returns the object? Or perhaps I should manually code something that analyses the grammar?


It depends on the complexity on your sentences. If you have a limited range, you could do simple pattern matching on part-of-speech tags. Put your sentence through a tagger (there are plenty of them around) and look for the first noun following a verb:

I       want an         apple
Pronoun verb determiner noun

(I assume you mean the object, as the subject in that sentence would be I). Scan through the list until you hit a verb, then pick the next noun. This should work for most English sentences.

It gets a bit more complicated if your nouns have additional words around them, for example red apple (where red is an adjective) or bottle of beer (where you have the pattern noun OF noun). So if you want to capture those fully, you might need a few more complex matching rules.

Overall it should still be a lot easier than implementing a full-blown syntactic parser which creates a comprehensive structural analysis of your sentence, most of which you wouldn't need in the first place.

  • $\begingroup$ I'm trying to do something similar to this. I would like to perform sentiment analysis, however I need to keep track of the subject, so I can be aware of what the sentiment applies to. using SVO, to manually parse it might work, what would you reccomend? $\endgroup$
    – johnny 5
    Feb 11 at 23:45
  • $\begingroup$ Depends how precise it has to be. You can get quite far with simple heuristics, but every percentage accuracy in precision beyond say, 70-80 percent, requires a lot more effort. $\endgroup$ Feb 12 at 12:05
  • $\begingroup$ Thanks, I'm going to try a hybrid solution. use AI to find the subject using a syntax parser, and then some list of rules to see if the subject from the previous sentence is the same $\endgroup$
    – johnny 5
    Feb 12 at 17:27
  • 1
    $\begingroup$ That sounds like a good approach. $\endgroup$ Feb 13 at 11:59

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