RailsConf 2017

Syntax Isn't Everything: NLP for Rubyists

Syntax Isn't Everything: NLP for Rubyists

by Aja Hammerly

The video titled "Syntax Isn't Everything: NLP for Rubyists" by Aja Hammerly, presented at RailsConf 2017, offers an introduction to Natural Language Processing (NLP) specifically tailored for Ruby developers. In the talk, Hammerly emphasizes the complexities and challenges of teaching computers to understand human language, which is often nuanced and context-sensitive. The speaker begins by defining NLP as the intersection of computer science, artificial intelligence, and linguistics, focusing on the interactions between computers and human language.

Key points discussed in the presentation include:

- Bad NLP Experiences: Hammerly connects the audience's experiences with poor natural language interfaces, explaining how current systems struggle with understanding human nuances, often leading to frustrating interactions.

- The Promise of NLP: The speaker highlights improvements in NLP as essential for achieving better user experiences, proposing a shift where computers learn to engage in conversations similar to human interactions.

- Accessibility Use Cases: A significant benefit of enhanced NLP is its application in accessibility, particularly for children or individuals with disabilities who rely on voice interactions due to limitations in reading or typing.

- Sentiment Analysis Example: Hammerly shares her past experience of conducting sentiment analysis on tweets using emoji scores, demonstrating how NLP can help analyze user feedback more effectively.

- Challenges with Language: The speaker starkly outlines the difficulties in NLP by describing the irregularities and imprecision of human languages, which complicate machine understanding. She notes how idioms, slang, and evolving language patterns significantly challenge NLP efforts.

- Historical Context: Hammerly mentions historical milestones in NLP development, including Alan Turing's contributions and the emergence of early chatbots, underlining the long-standing efforts in this field.

- Demo and Tools: She provides a live demo using the Google Cloud Natural Language API, illustrating how Ruby developers can implement NLP functionalities by leveraging existing models rather than building them from scratch.

- Conclusion: Hammerly concludes by encouraging developers to experiment with NLP technologies and presents Google’s resources for getting started. The session ends with a live sentiment analysis demo reflecting the audience's interactions on Twitter, showcasing practical applications of NLP concepts.

Overall, the presentation conveys not only the potential of NLP in enhancing interactions with technology but also the significant obstacles posed by human language complexity. Rubyists in the audience leave with an understanding of basic NLP concepts, challenges, and practical tools to help them begin exploring this fascinating area of tech.

00:00:11.660 So can everyone see the screens? I'm going to try to do a live demo because, you know, I like living on the edge a little dangerously.
00:00:17.910 I'm Aja Hammerly, but I go by Sacre Miser in most places on the Internet. Tigra Miser was taken on Twitter, so it's the Sacre Miser.
00:00:24.000 I love it when people tweet me questions or comments, or even if they think I'm completely wrong about something during talks.
00:00:30.689 My phone is somewhere over there, and I believe Twitter is turned off on the laptop, so you all won't see it anyway. Feel free to go nuts!
00:00:37.890 I blog once a week at SacreMiser.com, covering various topics including DevOps, Ruby, and the art of speaking.
00:00:43.110 I also work at Google on the cloud platform, so if you're interested in Google Cloud, Kubernetes, or the other things we do like machine learning APIs, I'm happy to answer questions.
00:00:48.710 I have a plethora of opinions that I'm more than happy to share with you, so feel free to come find me and we'll chat.
00:00:54.840 And since I work for a large company, I have to say that any code in this talk is copyright Google and licensed under Apache v2.
00:01:05.010 It is currently not on GitHub, but it will all go up there in the next day or two, and I will post about it on my blog and on Twitter when it gets there.
00:01:12.720 Hopefully, since the room is relatively empty, people aren't here by random chance because they didn't like the last talk they went to or walked in a little late.
00:01:18.090 But what is NLP? The acronym stands for Natural Language Processing, which may get you a bit closer to what my talk is about, but it's still somewhat fuzzy for me.
00:01:24.119 Let's go to Wikipedia: Natural language processing is a field of computer science, artificial intelligence, and computational linguistics. It's concerned with the interactions between computers and human natural languages.
00:01:30.180 In particular, it focuses on programming computers to process large data corpora.
00:01:35.250 That's one exceptionally long sentence filled with big words, so here’s the definition I use: teaching computers to understand and ideally respond to human languages.
00:01:41.759 By human languages, I mean things like English, Japanese, American Sign Language, and British Sign Language—basically all the languages humans use.
00:01:48.600 So, to echo millions of middle schoolers everywhere, when are we ever going to use this? And, why should I care?
00:01:55.680 The reason is that bad NLP is already here. Who has ever had to interact with a phone system that says, 'Please say your account number,' and you scream back, 'I didn’t understand you, say it again!'?
00:02:04.170 Who here has logged onto a website and had a chat window pop up with some disembodied figure saying, 'If you need help, I'm here to help you.'?
00:02:10.160 And you wonder if there's really a person behind that or if it's just bad NLP? I had that happen to me last week. It was creepy!
00:02:18.180 And all these things involving natural language processing, frequently bad NLP, are likely involved.
00:02:23.639 However, the promise of NLP is actually better user experiences. I want to live in a world where, instead of teaching people how to interact with computers, we can teach computers how to interact in ways that people already find successful.
00:02:30.180 So, an example of non-ideal NLP that many of you are probably familiar with is this: my favorite slide in the entire talk is 'Computer, tea, Earl Grey, hot.'
00:02:41.759 That was an example of bad NLP; the way I phrased the request was very specific.
00:02:49.179 I wouldn't use that phrase with any other person, but when it was on air, it seemed really futuristic.
00:02:56.120 It turns out we can do better than that. These random things that live in our houses and potentially order food for us are great.
00:03:03.000 There was a talk by Jonah and Julian yesterday, where they were arguing with multiple Alexa devices up here on stage, and I have a Google Home, and I really like it.
00:03:12.750 Depending on the particular brand, they have a relatively large, and in some cases, nearly limitless corpus of voice commands that they can respond to.
00:03:17.640 And, if you really want one of these devices, come to the Google booth; we're raffling one off at 4:30 tomorrow. Stick around until the end of the talk, and I'll show you how to enter!
00:03:25.050 Additionally, I mentioned earlier that we have tech support and phone trees, and these are actually getting a lot better.
00:03:31.140 In my experience, I can actually call my credit card company, say I never received my credit card, and they will send me to the right person who can answer that question.
00:03:37.890 I really hope that this technology improves over the next few years.
00:03:44.200 Now let’s talk about some use cases you may not have thought about; accessibility is one.
00:03:50.940 During the closing keynote yesterday, a point made was that children often use voice interfaces because they can't read yet.
00:03:57.750 I used to work on software for kids, specifically specializing in those with learning disabilities.
00:04:04.220 The ability to use voice interaction or not have to type and spell correctly is fantastic for individuals with dyslexia.
00:04:10.380 It's also great for those with broken arms or individuals who might be holding babies.
00:04:17.850 There are all sorts of use cases where this kind of accessibility matters.
00:04:24.020 In addition to the things that many of you may already think of when I put the word accessibility on the slide, such as for blind people.
00:04:30.054 Blind people can definitely benefit from voice interaction.
00:04:36.230 And the other thing that NLP can help us with is improving our understanding and capacity to analyze large amounts of data.
00:04:42.530 Who here works on an app that has a feedback button somewhere?
00:04:49.350 When I worked in EdTech for kids, the feedback we received via that button was often amazing.
00:04:55.370 Five-year-olds can have the most creative—and sometimes harsh—ways of letting you know they hate your software.
00:05:01.640 And when we had software that became more popular, processing all that feedback was initially really hard.
00:05:07.720 Initially, everyone received every feedback email, and eventually we realized we needed a system to manage it.
00:05:14.090 We wanted to route feedback to the right people, like billing-related inquiries to the billing team.
00:05:20.000 But the complaints about the software needed to go to a 'Things to read on your bad day' folder.
00:05:25.800 And requests for help should go to the team working on that interface.
00:05:32.000 We didn't have a proper way to do this at the time, so we didn't bother.
00:05:36.870 NLP can also assist in various ways. One of my co-workers created a tool called Deep Breath to analyze your emails.
00:05:43.680 If you try to send an email that seems hostile, it tells you, 'You might want to rethink that.'
00:05:50.110 Imagine how many GitHub flame wars could be avoided if everyone had that tool!
00:05:56.520 So, why don't we have that already? Because NLP is hard! Really, really hard.
00:06:02.360 You don't have to believe me; let's talk about why it's hard. First off, English is horrible!
00:06:10.059 Pop quiz: what do you think of when I say the word 'seal'? Raise your hand if you thought of a marine animal.
00:06:16.600 Keep your hand up if you thought of an event where someone signs a contract.
00:06:23.960 Who thought of a musician? Who thought of something completely different?
00:06:30.020 One word can represent many different ideas. This was actually fairly evenly distributed, which is quite interesting.
00:06:35.410 We have our famous 'humma humma' phones, and with so many English teachers around, we have so little time.
00:06:42.810 Additionally, we have words that can be multiple parts of speech. For example, 'love.'
00:06:49.360 Love can be a verb, as in 'She loves her wife,' but it can also be a noun, as in 'Love lasts forever.'
00:06:55.250 English is complicated—don't even get me started on irregular verbs, slang, idioms, and other bits that make a language human.
00:07:02.110 It turns out that English is especially difficult, but all human languages can be quite challenging in similar ways.
00:07:08.180 Even the languages we've tried to manufacture to be less confusing are still complex because humans create idioms and slang.
00:07:15.120 Language evolves and becomes particularly troublesome for computers because there's no formal closed grammar for human languages.
00:07:22.000 While some languages are more irregular than others, grammar for computer languages typically fits on a page or two of printed text.
00:07:29.180 They're generally very regular, with a limited vocabulary; the rules are straightforward.
00:07:35.580 Human languages are messy. For instance, we can say, 'I'm starving,' but that has a remote chance of being true.
00:07:41.700 Or if I say to someone, 'You look freezing,' the probability that’s true is quite low.
00:07:48.240 We exaggerate a lot, and I recently read a cool article stating that the word 'unique' is becoming less unique over the past thirty years.
00:07:54.840 Thirty years ago, the word 'unique' was relatively rare in newspaper articles. Frequently, editors would use 'unusual' instead.
00:08:01.340 But now, it's almost as likely to find 'unique' as you are to find 'unusual,' which is problematic.
00:08:07.840 Another example I like is the word 'literally.' As of two years ago, it doesn't even mean literally anymore!
00:08:14.490 We also have the issues with sarcasm: it's not just humans who struggle, but computers do too.
00:08:20.210 This is an example I used in a previous talk: if I say, 'Sure, I’d love to help you out with your problem,' it can sound sincere.
00:08:28.300 However, if I say, 'Sure, I’d love to do the dishes,' depending on how I say it, the meaning changes.
00:08:34.440 The context and surrounding words really affect how we convey meaning.
00:08:41.820 Despite learning from Douglas Adams's 'The Hitchhiker’s Guide to the Galaxy,' computers are really bad at distinguishing sarcasm.
00:08:47.280 And they struggle even more with generating it. So, I ask again, why is this so hard?
00:08:53.950 I hope I’ve confused you and convinced you, but the main reason is that natural language processing is difficult.
00:09:01.100 Human languages are complicated and always changing. But because we created them, we can simplify everything.
00:09:07.670 So why is NLP hard? Because understanding natural language is hard—humans are tricky!
00:09:14.030 At its core, language is hard for computers due to the intricacies of human communication.
00:09:21.100 However, not all is lost! We've been working on this problem for a really long time.
00:09:27.920 Two famous mathematicians proposed ways to accomplish algorithmic translation between languages.
00:09:36.880 I am of the generation that saw Google Translate become a thing as I was growing up.
00:09:42.630 Automated translation has improved greatly over the past 15 to 20 years, but we have been working on this problem for hundreds of years.
00:09:50.200 Another great example is Alan Turing, who proposed the Turing Test back in 1950.
00:09:57.270 Who here has heard of the Turing Test? A few people. For those who haven't, let’s define it.
00:10:03.130 The Turing Test is a way to measure machine intelligence using natural language.
00:10:09.750 An impartial judge observes a conversation between a human and a machine without knowing which is which.
00:10:15.240 Usually, the conversation occurs via text. If the judge cannot identify which participant is the machine, the machine passes the Turing Test.
00:10:22.010 People have tried to solve this challenge. One of my favorite examples is Eliza, which came out shortly after the Turing Test.
00:10:29.990 If you use Emacs, you can play with Meta-X Eliza in real-time. Since I have the time, let's do that now!
00:10:36.000 So, here we are! Hi, I’m feeling sad! Eliza replies, 'Hi, how do you do? What brings you to me?'
00:10:42.540 I respond, 'I already told you I'm feeling sad.' And Eliza says, 'Why do you say you already told me that you're feeling sad?'
00:10:48.230 And I might say, 'Because I already told you, you stupid machine!' And Eliza would respond, 'Is it because you already told me?'
00:10:58.050 This is a classic example of NLP, and many implementations exist.
00:11:03.360 My project for the plane ride home is to port a Python implementation to Ruby, because, hey, why not?
00:11:10.260 So, typically, chatbots have been around since the late 50s and early 60s, which is somewhat surprising because many think they were invented recently.
00:11:16.900 Now I've rambled on for about 15 minutes without showing you any code, and that's not fair.
00:11:23.650 Code! I like to give a super impractical example in talks so that, hopefully, no one will actually use my examples, but they'll apply the ideas.
00:11:30.440 In RubyConf 2015, I demonstrated sentiment analysis of tweets using emoji scores.
00:11:37.120 Sentiment analysis is a subfield of natural language processing focused on determining if a body of text is generally positive or generally negative.
00:11:44.710 Using emoji, I assigned scores to indicate how positive or negative they were.
00:11:50.860 This was my general scale; I may have partly done this to put the poop emoji on the screen many times!
00:11:57.110 I utilized emoji at the time, and you can watch the video on Confreaks because NLP is super hard.
00:12:05.320 In November 2015, I didn't have the skills to train a model capable of effective natural language processing on the crazy tweets during the conference.
00:12:12.690 However, most people who do machine learning don’t build their own models.
00:12:14.470 Building models requires a lot of time and knowledge since there are many pitfalls.
00:12:21.500 Over the last year, many pre-trained models have been released that you can access via their API.
00:12:28.140 I will use the Google Cloud natural language API for my demo today, rather than my hacky emoji system.
00:12:34.830 We have a gem, which is available on GitHub. Just install 'google-cloud-language,' and it’s currently in alpha.
00:12:42.340 Here's the code I need to perform the sentiment scoring. I'll create a new object and use the 'analyze' method.
00:12:49.060 The 'analyze' method receives a document created from the tweet text. When I call the sentiment method, it returns an object with a score and a magnitude.
00:12:57.290 The score indicates if the sentiment is positive or negative, ranging from -1 to 1. Magnitude indicates how strong that sentiment is.
00:13:04.130 For example, if I get a score of 0.1 and a magnitude of 0.1, that indicates a very neutral tweet.
00:13:10.680 On the other hand, if I have a score of negative 0.9 and a magnitude of 0.1, it's negative but not excessively so.
00:13:18.630 I have a guide about how to interpret those if you're curious because understanding those two things can be difficult.
00:13:25.210 Don’t multiply the score by the magnitude; it doesn't work that way—I’ve already tried!
00:13:32.420 I'm going to skip over how I set up everything. It's a kind of funky nineties cluster pulling data from Twitter.
00:13:39.580 I'd be happy to explain more afterward, so come to my booth where everything is running.
00:13:46.560 I explained it all in detail in my original talk about setting up distributed systems for crazy things.
00:13:54.000 If we have enough time, I’ll do a demo at the end of the talk, but I need your help!
00:14:00.610 I’ll be pulling from that hashtag, so if everyone could take a photo or tweet something, please include a sentiment in your tweet.
00:14:06.740 My tweet has been running pretty good through the conference so far.
00:14:12.930 It’s not surprising—now I’m going to take a drink of water while you all tweet, and I’ll be back in a second!
00:14:18.190 Okay, now that you're all done tweeting, let's talk about grammar for a bit.
00:14:26.800 When this tool came out, I was thinking about how many of us learned how to diagram sentences in grade school.
00:14:33.520 Did you hate that process? I certainly did! It drove me insane to repeat the same thing over and over.
00:14:39.700 If you've never seen this before, this is one form of sentence diagramming.
00:14:46.560 In this diagram, you have a subject connected by a vertical line to a verb, which is also connected by another vertical line to a direct object.
00:14:52.900 All the modifiers go on lines below. Anyone here had to draw diagrams like this? Show of hands!
00:14:58.120 Okay, I'm clearly not alone in my misery.
00:15:03.800 With both methods of diagramming, the verb is at the center, surrounded by everything that modifies it.
00:15:09.640 When I showed this to some of my friends in Seattle at Ruby, they groaned. They hate grammar!
00:15:15.340 Now, I'm returning from that brief side quest about grammar—let’s dive a little deeper.
00:15:21.280 One way we understand words is by labeling them based on their function; this is called parts of speech.
00:15:27.280 Verbs denote actions; you can’t have a sentence without a verb. One type of action could be a verb like 'jump'.
00:15:33.200 However, there are also state-of-being verbs like 'thinking.'
00:15:39.150 Next, we have nouns. A noun is a person, place, or thing—that's strictly true.
00:15:45.040 A person could be Matt or Alan Turing; a place could be the bathroom or Phoenix; and a thing might be a cactus or mountain goat.
00:15:51.170 You can even have nouns that signify abstract ideas, like 'democracy' or 'love.'
00:15:56.750 Then there are adjectives, which describe or modify nouns. They can specify attributes like color, size, or quantity.
00:16:02.720 Additionally, there are determiners, modern grammar calls them. Determiners clarify which noun is being referenced.
00:16:10.440 They include articles like 'a' and 'the,' as well as this and that.
00:16:17.200 Not every determiner is an article, but all articles are determiners—it’s like squares and rectangles.
00:16:22.676 Now, let's talk about sentence structure. You need a verb for a complete sentence.
00:16:29.650 Thus, in the sentence 'The cat eats fish,' the subject is 'cat,' the verb is 'eats,' and the direct object is 'fish.'
00:16:36.080 Side quest complete! Now that we've reviewed the basics of grammar, let's return to our NLP and sentence diagramming.
00:16:42.200 In diagramming a sentence, we begin with the subject, the verb, and the direct object, along with any modifiers.
00:16:49.410 To perform the analysis, I need to identify the part each word plays in the sentence.
00:16:57.000 There are tools for that! Instead of using the sentiment method, I will use the syntax method.
00:17:03.510 This will return a list of tokens along with a wealth of information about each token.
00:17:09.480 For instance, for the word 'cat' in 'The cat eats fish,' I would receive details such as its function in the sentence.
00:17:16.620 The API returns the part of speech, indicating that 'cat' is a singular noun.
00:17:24.340 Interestingly, English lacks many modifiers and can trip up algorithms.
00:17:30.920 If you’re trying to understand parts of speech, it can be enlightening to use it on other languages, like German or Spanish.
00:17:41.100 This token system also includes a 'head token index,' denoting the parent word in relation to the current token.
00:17:47.160 Where 'the' is linked to 'cat', we can see how they are structurally associated.
00:17:53.390 With this information, I can create an ASCII art diagram for a sentence.
00:18:00.600 I can identify the subject of the sentence based on the token label.
00:18:05.970 Next, I find the direct object and string together a more refined ASCII art diagram.
00:18:12.110 However, I was missing one word, so I looked again to identify how the direct object fits into the sentence.
00:18:19.050 The NLP API provides token indices for relationships among tokens.
00:18:25.480 For example, 'the' refers to 'cat' as its main subject, allowing for straightforward diagramming.
00:18:31.470 I simply iterate through all my tokens and print those that refer to the subject.
00:18:38.079 I can produce a basic diagram here, which I'm pretty happy with!
00:18:44.320 Now, let’s step it up with a more complicated sentence to form a more robust visual.
00:18:51.710 Let’s try 'The cat eats fish with a side of milk.' In the analyst’s eye, that adds layers of complexity.
00:18:58.090 Due to the prepositional phrase at the beginning, I can examine how that fits within the structure.
00:19:05.240 The 'with milk' phrase influences how I interpret the entire sentence and shows fluidity in grammar.
00:19:11.950 There are all sorts of practical uses of NLP.
00:19:18.330 We've talked about customer feedback analysis, summarizing content, and creating more accessible products.
00:19:25.200 At this point, I hope I’ve done my job convincing you that there’s great potential here.
00:19:31.750 If you want to get started, the Google Natural Language API is an excellent resource.
00:19:37.790 The first 5,000 requests to each endpoint in text sentiment are free.
00:19:44.280 Once you exceed that, it is reasonably priced per thousand.
00:19:51.450 I also had the Jabberwocky analyzed, and to my surprise, it got the syntax analysis correct.
00:19:59.630 It’s a wonderful opportunity to experiment with technology and see what you're capable of.
00:20:06.820 Dig in and play around! I work for Google, and we have a booth at RailsConf this year.
00:20:14.320 There are live coding labs, cool stickers, and answers at our booth.
00:20:20.480 Also, come grab a dinosaur sticker if you hang out with me afterward!
00:20:26.570 We are also giving away a Google Home—check the link or the whiteboard at our booth.
00:20:33.450 Thank you for your attention! Now I'd be happy to take any questions.
00:20:39.300 Yes, over there; I see a hand raised.
00:20:45.600 The first question was about how the sentence diagramming approach deals with incorrect grammar.
00:20:52.960 I have tried it out with sentences that are missing punctuation and it struggles.
00:20:58.590 Yes, it’s a machine, so machines make mistakes, and they're improving over time.
00:21:05.390 My general rule is that I have bad grammar, i.e., I need a copy editor for my blog.
00:21:12.180 However, it performs well with the common mistakes individuals tend to make.
00:21:19.590 Others may handle less common mistakes better, so there's always room for improvement.
00:21:27.260 The next question was about how the model interprets emojis.
00:21:31.200 That's something I've been testing, and I can show you the current sentiment!
00:21:36.570 The current sentiment is positive, which indicates a majority of positive sentiment in your tweets.
00:21:43.440 Even if some try to skew it negatively, positive sentiments are outweighing them.
00:21:50.890 Lastly, cleaning up data and ensuring high-quality data definitely increases the accuracy.
00:21:58.390 I’ve been pretty lazy and have skipped significant data cleaning so far.
00:22:06.540 But I've been satisfied with results without pre-processing, though it’s something to consider.
00:22:12.600 Another question focused on how the analysis identifies proper nouns, companies, and entities in context.
00:22:20.400 One use case is reviewing SEC filings, identifying key players and significant entities.
00:22:27.720 The sentiment also works well for statements related to personal preferences.
00:22:34.540 Thank you all for a fantastic audience today! Your questions were insightful.
00:22:41.140 You can come get dinosaurs from me anytime. I appreciate your time. Thank you!