Saturday, October 13, 2018

Implementing Contextual Chatbot Framework

gk_ wrote a really good piece on developing a contextual chatbot framework in Python using tflearn.

We’ll be working through 3 steps:

1. We’ll transform conversational intent definitions to a Tensorflow model

2. Next, we will build a chatbot framework to process responses

3. Lastly, we’ll show how basic context can be incorporated into our response processor

He develops a simple framework that is easy to build from. When I say "simple" I mean that it is easy to follow and would be easy to extend. It is not simplistic by any means. In fact, for the most part, it is significantly more sophisticated than Microsoft's botbuilder framework and Amazon's LEX framework. (We're not even going to compare it to IBM's hopelessly unusable Watson or Blue or whatever-it's-being-sold-as framework.)

gk_ also demonstrates a clean way to represent training data in a json file. I have my own way of representing training data, but it's not better than his. His training data and bot-configuration are all in a single json file and that is the only part of the solution that is application-specific. In other words, the framework he presents in his article will adapt to any domain by simply changing the json configuration file. I like that.

What does his framework get right? A number of things but providing context for subsequent intents is very powerful. One problem I've dealt with is how to let a user navigate backwards through a dialog or ask for help or any other context specific question. My way of dealing with that in the past has been to create some navigation intents (GOBACK, HELP, WHYDOYOUASK, etc.) and then check for those intents before looking for other intents. The way I've dealt with it is fine, but I like his better.

I do wish that frameworks had the ability to "GOBACK" to the previous question. In chatbots such as he's modeled, it's not important to be able to do that. But in a chatbot that is guiding a user through a long conversation, the ability to "GOBACK" is important to usability. The good news is that his framework could be extended to handle "GOBACK" if necessary.

The other thing his framework does not do is provide any concept of intent slots that must be filled. That's really LEX's strength--the definition of intent slots and then the dogged prompting of the user until all the slots are filled.

Getting Started with Tensorflow via TfLearn

Adam Geitgey has written an excellent series showing how to get started with machine learning. He chose tflearn as his API.

This guide is for anyone who is curious about machine learning but has no idea where to start. I imagine there are a lot of people who tried reading the wikipedia article, got frustrated and gave up wishing someone would just give them a high-level explanation. That’s what this is.

His five-part series takes you through classification models, price-prediction models, and ultimately shows how to use machine learning to analyze all the recognizable objects in a photo.

Monday, September 3, 2018

Getting Started with spaCy

spaCy claims to be the fastest natural language processing (NLP) library in the world. It also claims to be the best way to prepare text for NLP processing:

spaCy is the best way to prepare text for deep learning. It interoperates seamlessly with TensorFlow, PyTorch, scikit-learn, Gensim and the rest of Python's awesome AI ecosystem. With spaCy, you can easily construct linguistically sophisticated statistical models for a variety of NLP problems.

spaCy's creators differentiate spaCy from SciKit and Stanford's CoreNlp by characterizing spaCy as a production library, not a research library. For that reason, it is "opinionated," as that term is used in the tech world, and does not provide the ability to switch between similar models for a task. For my work, this is just fine.

The Lightning Tour shows how to load and use spaCy's most salient features, including its powerful browser-based visualizers. Text processing produces some complicated vectors yet speech itself is intuitive to most of us. The gap between our intuitive understanding of a text and the vectors produced when analyzing the same text is nicely bridged through these visualization tools.

The developers provide some simple spaCy demo programs written in Python.

My projects tend toward analyzing the speech of people who are excitedly blabbering into a text area in a browser. A common text might involve several hundred words, without punctuation, that appear to comprise multiple sentences but which are not obviously organized into sentences by the traditional likes of punctuation, capitalization, or line-breaks.

All NLP libraries utterly FAIL on most of the text I need to process. I did not make this next example up--this is exactly the question that was posed to me:

Okay in November last year cps made visit to my house an found no immediate dangers of my child so they set up a service plan an I followed everything except going up to do a drug assessment because i was had claimed to be smoking weed an that me an my boyfriend that lives with me at the time fight an that i shot at him well investagitor came out an assests the home an found no bullet holes in the home then i recieved a letter in the mail that states unable to determine but they was still going to offer family based safety service plan well i did not comply so i had stop responding to them an they then sent me a letter stating I was ordered to court an had to supply a drug test an that is when I tested positive for methaphemines an they removes her from the home cause my boyfriend at the time refused to even take one so they removed her an places her with one my daughter friend an the girls father well now as it is reported by cps my daughter has become even more depressed an has been smoking weed that was provided by the foster dad's niece in the home an my daughter has been sneaking out the house at night an having sex with men she's 18 to 20 an then I was notified that the foster dad niece had shot up 40units of heroin in front of my daughter an almost over dosed in front of my daughter an as after my daughter had told them she's going to kill herself she was removed from the home an placed in an behavioral center an then was removed from the home an is now still in the behavioral center due to they have no where to place my daughter so the hospital is holding her there until they can find placement well since then I did get clean from drugs an now have a job doing home healthcare as I did in previous years but now they are taking me back to court to have my parental rights terminated can I file indigent an get an attorney an prove that they submitted her to much more dangerous situations an gain coustidy back of her with stipulations as in I would submit a weekly drug test an random as well as my daughter an show that she was not in as much danger as to what she has experienced since she has been in the coustidy of cps

(Yes, the author of that question is probably still using "methaphemines".)

I ran that jewel through every model and SaaS I could find and threw it at a few humans, some whom were attorneys and some who were not. The software didn't even come close to discerning the true call of the question and no human made it through the text without screaming and saying something to the effect of, "this person is crazy."

Crazy or not, the person had a question and I gave her a 100% accurate one-word answer:

Question 1: If I meet the county's indigence requirements, can I get a court-appointed and court-paid-for attorney to represent me in a suit where the state wants to terminate my parental rights?

Answer 1: Yes.

Of course, she had the same follow up question that all litigants have, i.e., "Will I win?"

Question 2: If I can prove the foster care system exposed my daughter to more danger than I ever did, will I win?

Answer 2: I don't know.

CONCLUSION: spaCy appears to be a powerful library for NLP processing. The current state of the art requires that we prompt humans to break their text into smaller chunks responsive to more narrow prompts than, "What happened?"

Sunday, September 2, 2018

Deep Contextualized Word Representations

Natural Language Processing (NLP) is the holy grail for someone who makes their living in the world of words, as I do. Over the last several years, researchers have brought us more advances in this area than in the prior 25 years of my experience in the field. But there are still problems.

The word "run" is a killer. Look it up in a dictionary (LOL!! OK, do a Google search for the word's various meanings) and you'll find it has more meanings than almost any other word. When we're asking the computer to analyze what someone says, how in the world do we differentiate the exact meaning intended from all the possible meanings?

Think of how YOU do it. Perhaps you're not paying super close attention to a conversation and you hear someone ask, "Is he going to run?" The plain English does not give you any guidance as to whether the speaker is asking if "he" is going to flee, participate in a foot race, stand for election, or any other sensible meaning. To figure it out, you'd process the context of the statement.

As a person in the room, you'd have access to context that, for now, computers don't have access to. You'd be able to see whether the speaker was in a courtroom nodding toward a prisoner (flee) or an elected judge (election) or an athletic client (footrace). You'd process that context backwards and then forward into the sentence to ascertain the meaning. You might get it wrong...the speaker might wonder if the judge might flee or the prisoner might stand for election...but you'd probably get it right.

Some researchers at the Allen Institute for Artificial Intelligence at Paul G. Allen School for Computer Science and Engineering at the University of Washington have developed an approach to addressing this problem. The call it "deep contextualized word representations" and have written about it and developed software to implement their ideas. Lucky for us, they have created a repository of open-source code so that the rest of us can take advantage of these advances in NLP.

Implement JWT with Google Sign-On

Once you understand the basics of JWT, it's probably time to implement it with a third-party identity provider such as Google, Facebook, or Amazon (among too many). The benefits of using a third-party identity provider are:

  1. Users of your app do not have to create a new account with you. If you are offering a new product or service, there is a huge temptation to gather as much information as possible about your users. Resist that temptation. Gather only what you need and what you don't need is to manage the usernames and passwords of your users.
  2. You do not have to provide the customer service function of dealing with lost, forgotten, or compromised credentials (usernames, passwords). Google, Facebook, Amazon, etc. have thought about this a lot harder than you have and devoted way more resources than you have to this effort. Let them do it.
  3. You can connect otherwise personally identifiable but private information to an abstract ID from the third-party identity provider and thereby anonymize your data. As my good friend Shawn Tuma says, there are two types of companies: Those that have experienced a data breech and those that will. When that terrible day comes to your app, it will be nice to know that no login credentials were stolen and that most of the data you store is so anonymized that even you don't know what belongs to whom.

Developers roll their own identity solutions for a number of (I contend) bad reasons, one of which is that using third-party identity providers is not easy because it's not very well explained. Until now. In this practical blog post by Eiji Kitamura, Mr. Kitamura provides a tutorial on how to implement JWT authentication with Google's Sign-On service.


Simple JWT Implementation

Single page apps (SPA) are not only the rage, but the future (for now). Their benefits include more responsive user interfaces, less traffic between the user and the server, and a more native experience for the user. Another benefit is that they can permit developers to implement a server-less back-end. An example of a server-less back-end is Amazon's lambda functions.

User authentication and authorization are important topics no matter what architecture you select for your service, but they become a little tricky in the single page app world. Part of that trickiness is probably due to the fact that most of us are not accustomed to having to reintroduce ourselves and our qualifications in every encounter with someone, but that's what you have to do when implementing a server-less singe page app.

Naren Arya has posted this excellent tutorial on the basics of implementing JSON Web Tokens (JWT) and explains his interest in the topic this way:

JWT authentication is becoming very popular these days. The traditional authentication uses cookies and sessions. With the advent of Single Page Applications(SPA) and microservices, there is a need to look beyond the sessions. Any token based authentication serves that purpose. JWT is a type of token-based authentication.

He's absolutely right.

If you are new to JWT or, as in my case, have probably implemented it...ah...imperfectly, read his post AND do yourself the favor of trying it out.

Once I read this post and understood the basics of JWT, I was able to go back to my app and fix my integration with Google's OATH service.
9.4Thomas James Daley