top of page
Writer's pictureJian Soo

Natural Language (Analysis) Toolkit: NLTK

Updated: Feb 27, 2019



While looking for solutions to interpret instructions, I came across NLTK, a toolkit used for analyzing natural language.

I implemented NLTK in a basic way, using it to tag verbs. Here's a flowchart that shows the processing.


If the action required more information, it would just analyse the bit after the verb to figure out what the specific action needs to be.

For example:

'Athena, play Princess of China.'

Verb = 'play':

Athena needs more information to that command: so it looks for the rest of the sentence after the verb.

Sentence after verb: 'Princess of China.'

So Athena will search for the song 'Princess of China in the Spotify library and play it.

Here's how that bit looks like now:

A bit of code explanation:


The Natural Language Toolkit offers a few functions to classify words into different classes. In order to classify, text needs to be 'tokenised' - a sentence like


'The quick brown fox jumps over the lazy dog'


turns into


['The', 'quick', 'brown', 'fox', 'jumps', 'over', 'the', 'lazy', 'dog'].


From there, each element in that array is 'tagged' - put into a tuple with the class of word.


Example of tags (a bit different from the tags in the program - this one uses VERB, whereas my program uses VB.)

The 'imperative_pos_tag' function is a modified tagger that recognises imperative verbs properly, scraped off a StackOverflow solution. When a command like 'Play Princess of China Coldplay' is passed, 'Play' is seen as a noun.


This cheeky bit of code bypasses that by adding 'He' in front of the imperative and tags it, forcing imperative tagging.

3 views0 comments

Recent Posts

See All

A massive update.

It's been a bit over a year since I've posted on this blog... GCSE revision, schoolwork and a massive project was on my mind, and I just...

bottom of page