# How to find the subject in a text?

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

Examples:
- 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?

• Isn't that the object? The subject is 'I' and the object 'apple' . As far as I know in English they follow a simple rule in most cases. The subject is the part of the sentence before the verb and the object after the verb. Maybe this might be useful in some way. – DuttaA Feb 15 '18 at 7:28
• erg.delph-in.net/logon and similar online software or librarians. Google for "sentence analyser software". – pasaba por aqui Feb 15 '18 at 10:04
• @pasabaporaqui thanks for contributing. With a little more elaboration, your comment would be a good answer I think... – DukeZhou Feb 15 '18 at 20:32
• @DukeZhou: thanks for your support, but I prefer not answer questions that, in my opinion, are of very low quality and should be closed and removed. Develop bots and no idea of parsing and/or natural language ? It seems contradictory. – pasaba por aqui Feb 15 '18 at 20:41
• @pasabaporaqui Good to know. From a community perspective, I tend to look at it from the opposite direction--can this question be massaged and answered so that it is generally useful to others who might stumble upon it? – DukeZhou Feb 15 '18 at 21:01

Early linguists like Noam Chomsky have tried to understand natural language on a pattern base. The idea was to parse a sentence in the same way as a compiler would parse computer code. The input stream is converted into an abstract syntax tree and this helps to tag basic patterns like subject, verb and object. The problem with natural language is, that it is more complicated then only the pattern itself. The hidden complexity is described in the literature as grounding. Grounding means, to map a sentence into actions.

The example sentence “I want an apple” means by itself nothing. Sure, it is possible to search in the string for a substring but what next? Let us assume that this sentence was used in a textadventure. Here the pattern has a meaning. The word “apple” is grounded in sourcecode and is referencing to an item which can be taken by the player, and if the player has the apple he can eat it. The question is not, how to parse natural language, the question is how does the environment around this language look like.

Sometimes the textpattern is more complex and contains time attributes. The new sentence could be “I want an apple, now”. But what is the meaning of “now”? Right, it is not given by the text. “now” doesn't describe the meaning itself, it is only a pointer to a higher layer. At foremost, “now” is referencing to a dictionary entry which is providing synonyms. And these synonyms are referencing to the grounded model. That means, to the concept of time which is implemented in the textadventure. If in the game a clock variable is given, “now” is linked to that variable. What I want to explain is, that natural language is similar to the tip of an iceberg.

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.