Advanced grammar and Natural Language Processing with Syntaxnet

Lets play a game: can you tell the difference between these two sentences?

“Most of the time, travellers worry about their luggage.”

“Most of the time travellers worry about their luggage.”

Whoa, remove the comma and all of a sudden we’re having an entirely different conversation!

The little nuances of language can be hard enough for a human to understand, let alone a computer! How could we possibly teach a computer to understand the difference?

Language is the heart of most human endeavors – being able to effectively communicate is one of the most important socioeconomic indicators of success. We discussed the process of teaching computers language in our NLP blog post, but we primarily discussed complete tools – not the underlying components to create them.

Introduction to Syntaxnet

First off, lets do something different: we’re going to start by describing what Syntaxnet isn’t.

Syntaxnet isn’t going be able to tell you what someone’s sentiment was in a tweet (Sentiment Analysis), nor is it capable of extracting street addresses from written documents (NER). Syntaxnet is not a high level magic bullet for every big NLP task. What it is, however, is a force multiplier.

Most natural language tools are trained to grok semantic meaning by focusing on word order and the removal of so-called “stop words”. This works pretty well but it obviously loses resolution.

Consider what was at one point the state-of-the art of document classification.

The common most approaches loosely follow this procedure:

define your corpus and split it up into separate “documents”

use TF-IDF to extract important keywords from your documents, ignoring the others

Did you notice yet why the standard approach might not yield the best results?

We’re completely ignoring grammar and linguistic syntax of a sentence, and only considering “rare” words which in some cases can result in nonsense! You may say “Sure James, this might not work in every case, but it should get pretty close most of the time right?”

For any predictive system, often the important consideration is the difference to a baseline predictor, rather than the absolute accuracy. A model that predicts that the weather will be the same as yesterday will often be accurate, but it’s not adding any value.
The thing about dependency parsing is that about 80% of the dependencies are very easy and unambiguous, which means that a system that only predicts those dependencies correctly is injecting very little extra information, that wasn’t trivially available by just looking at each word and its neighbours.

For some applications like the development function / info bots such as Alexa or the Google Assistant a simple, heuristic approach gets you “good enough” results. These bots have been designed to understand and process simple requests with basic syntax structures like “Alexa, order me a pizza”, or “Hey Google, call an Uber to take me home”.

Those two examples probably worked, but try saying this to a google home bot: “Hey Google, order me a ride home, but pick the cheapest ride between lyft and uber.”. How did that turn out? Is there any way you can rephrase that so that Google Home might understand?

If these NLP bots were trained with a powerful syntax parser like Syntaxnet, it’s possible that we could train a bot that’s capable of answering more linguistically challenging requests, even the example above.

I’m intrigued – What is Syntaxnet?

Syntaxnet is a tensorflow based syntax parsing framework developed by Google. Within this framework, the fantastic development team created a whimsically named pre-trained model called Parsey McParseface. It’s this pre-trained model that makes our algorithm work!

“What’s a Syntax Parser”, you ask? We can split up that definition into two components: a Dependency mapper, and a Part of Speech Tagger.

In basic terms, it splits up a sentence into component phrases with arrows pointing between them, eventually even breaking down phrases into words. These arrows contain “dependency” labels like negation or contraction, and compound – but also labels like preposition, phrase object and appositional modifier are frequently found. You can find a full list of available modifiers here. What’s great about capturing dependency labels is that now we’re able to break up sentences into separate components, and find patterns that can help translate natural language into machine readable directives.

Great, so that’s the basics of a dependency map… but what about Parts of Speech Tagging? Parts of Speech provide us with even more information about how words are used in a sentence. Lets revisit our two earlier examples, and compare the two Parts of Speech Tags (tree format only outputs Parts of Speech tags, so we can more cleanly spot the differences):

{
"src":"Most of the time, travellers worry about their luggage.",
"language":"english",
"format":"tree"
}

Notice the difference a comma makes? The word time can mean different things, depending on context. In the phrase Most of the time, time is an adjectival modifier and it modifies the word most. However in the second case, time forms a compound word with traveller, creating time traveller which is a pluralized noun.

Great, so this tool captures both the parts of speech, and the dependency relationships between phrases. Lets talk about how we can incorporate this into a pipeline.

How to get started using Syntaxnet

Getting started with syntaxnet is easy – we should note that it takes around 4 seconds to load, so processing in batch mode is most efficient.