The Poor Man’s Siri


With Siri hitting the office of my Apple enthusiast peers my attention was again thrown at Natural Language Processing (NLP).  This article isn’t explaining how Siri in particular works, but instead how to make something close on a shoe string budget. I originally wrote this in December of 2010 for Procon 2, an unreleased remote console tool for administering Game Servers and sequel to Procon, that I continue to work on today as a hobby.

Read on for an explanation of how I solved the problem, or skip to the live demo.

What’s this article cover and why?

I should start by saying that none of the work here is from any prior research. I am a hobbyist and this is the result of that hobby. I had an idea during my final exams at Uni to write the command line parser for Procon 2 like I would write a compiler for a basic programming language. After much fun poking and prodding and deep thought to overcome various problems encountered I finally had a working version, but what had I made?

It was then that I started researching natural language parsers and the various solutions out there. Most of them are far superior to that provided here, but the solution here can be used in a limited context with a fair amount of accuracy. It also fails brilliantly when it’s asked about something you didn’t program into it.

Well.. it fails brilliantly if you don’t happen to have internet access and google to ask something close to what the user wanted. You know, fake it :)

The project

This project is still a work in progress by far, but with more pressing projects and work it has been pushed to the back burner until Procon 2 is in beta.

The initial goals of the project

  • Extract meaning from short sentences
  • Match on fuzzy input per word (“Phogeu” = “Phogue”)
  • Able to do basic arithmetic
  • Add/Minus dates and times and understand delays, durations and intervals
  • Able to manipulate lists based on attributes of the objects in the list

Aside from not understanding the brackets, it can handle arithmetic provided there are spaces between the operators.   Since the brackets are dropped during execution they skew the current calculation. They are collected, and looking at the code it isn’t a biggy to include it so I must have thought of something last year which prevented it being included.. who knows, but this is a time when I see the merits of commenting code.

Dates and times the most complicated problem in this project. Think of how many possible ways you can say “Tomorrow at 1” and what it should mean (am? pm?) and you’ll get the idea. It’s not perfect, but it’s pretty good so far.

Sentences, Phrases and proper English

Most definitions in this article are thrown out the door or only borrow pieces from the proper dictionary definitions.

This article refers to the sentence as a whole, a subsection of the sentence as a phrase and an individual component of the sentence or phrase as a word.

“Hello World!” is a sentence with the words [“Hello”, “World”, “!”]; [“Hello”, “World”] and [“World”, “!”] are phrases of that sentence.

It starts with a Token..

A token is an object representation of a word. The words text follows a pattern matched against the token, which then has a data type associated with it and a value which can be extracted from the word.

A token can span many words. If a token is refactored to match against many words it is therefore a better match than just a single word.  Multiworded tokens replace their single worded counterparts in the chain of tokens making up the sentence.

Execution

Keeping the best matched

The base class Token has three attributes. The text that was used to match the token, a mixed value used to store the value of the token if it can be converted to a primitive type as well as a percentage of its match against the tokens pattern. Keeping in line with one of the project goals the text could remain fuzzy and still match against a token. However, if a token is a better or perfect match then it should rank higher than the fuzzy match.

The NLP never forgets Tokens as it finds better matches, but it does rank the better matches higher. If you see at the bottom of the example you can see some additional/garbage that was collected along the way. This can be used in some special circumstances within Procon, but is otherwise disregarded.

Initial Parse

Refactoring

Notice that combining “swap” and “player” produces the same match percentage as a single “swap”, however since the match required combining multiple words and a larger margin for error the new refactored Token is prioritized. Give it a go in the example below, you should see the garbage collected at the end.


Live Demo

This example was originally written to duplicate examples given by Apple in regards to Siri, but given how many articles are out there like that it woulda felt a little on the nose. Instead I re-re-wrote it so it functioned how my software would. Moderating a game server. I hopefully found a balance between the codes original purpose, a decent example of its capabilities while keeping it simple enough that anyone should be able to use it.

There are more objects that can be included, but this example only has a player list and a map list.

The output shows what was selected and what command would be executed by the software. No actions are actually taken so inputs and outputs can be easily compared. Play with it for a minute, I don’t doubt you’ll break it :)

Good examples to try

  • kick phogue
  • kick phogue and miko
  • kick everyone from australia
  • kick everyone except me in 10 mins from australia with more than 5 deaths
  • what is 5 + 2 * 5


Tokenized NLP Example

Comments

  • http://www.facebook.com/profile.php?id=611237035 Matt Green

    Looking forward to this being implemented

  • Prez

    Very Cool!

  • Originaldude

    Nice I like..

    I am looking forward to you creating this. Beat apple at their own game

  • http://twitter.com/ScHrAnZDiNgEnS Matt H.

    this sounds awesome….

  • Philipp Kolloczek

    This opens a total new kind of handling admin tasks for a rcon tool. Not just clicking at the options but be able to connect tasks.

    Sounds great for me! :)

  • Basert

    Wow, thats awesome, i don’t have to remember these crappy in game admin commands, i just have to write, what i want to do. Very Nice!

  • http://pulse.yahoo.com/_HZSCTL2UU2FHCRHWVIV6VEVYXA Michael M

    Looks like we broke it, “An internal server error occurred. Please try again later.”

  • http://www.thegamerszone-mgc.com Phragg

    Very nice! Changing the game as we know (as admins) :D

  • Madturnip

    Now if you could just implement voice recognition….

  • Geoff

    Sam, a front end guru in our office, showed me a tag that works in Google Chrome to accept voice input. It’s a little sketchy, but it shows that almost every framework or API out there usually has a voice-to-text section.

    Since this takes simple text with a limited vocabulary it could be used in conjunction to make the voice-to-text more precise, or at least limited to words it is expecting.