Learn to use an Artificial Intelligence Model in 30 Minutes or Less (Part 2: Windows)
By: Rj Jain
Tags: Tutorial, Naturallanguageprocessing, Ibm Watson, Artificial Intelligence, Beginner

Learn to use an Artificial Intelligence Model in 30 Minutes or Less (Part 2: Windows) RJ Jain Follow Feb 14 · 13 min read

This is a continuation from part 1 here, which describes the steps necessary to initialize an AI model using IBM Watson’s Natural Language Understanding service. In this part, we’ll be taking a look at how we can utilize our AI model on Windows 10. If you’re a macOS user, click here to view the relevant guide.

Let’s get started!

W-21

Open Notepad by searching for it in your taskbar, or finding it in your Start Menu.

W-22

Go to File > Save As, and navigate to the folder you created in step 17. Before creating a file name, click on the drop down menu labeled ‘Save as type:’ and select ‘All files (*.*).’

W-23

Now, in the ‘File name:’ text box, type in ‘MediumArticleAnalysis.bat’ to name the file. You don’t have to use this exact file name if you don’t want to, but make sure the *.bat* file extension is still used.

The *.bat* file is a Windows batch file. A Windows batch file saves commands that can be entered into the Windows command prompt.

Doing this:

Is exactly the same as double-clicking on a *.bat* file that looks like this:

W-24

Now that we have our empty batch file saved, it’s time to add some commands! Go ahead and copy the following code, and paste it into your file:



curl -X POST -u "apikey:[YOUR-API-KEY]"^

--header "Content-Type: application/json"^

--data "{\"url\":\"

"[YOUR-IBM-WATSON-NATURAL-LANGUAGE-UNDERSTANDING-URL]/v1/analyze?version=2019-07-12"^

> MediumArticleAnalysisOutput.txt

echo Finished!

pause title Learn to use an Artificial Intelligence: Article Analysiscurl -X POST -u "apikey:[YOUR-API-KEY]"^--header "Content-Type: application/json"^--data "{\"url\":\" [CHOSEN-ARTICLE-URL]\ ",\"features\":{\"sentiment\":{},\"categories\":{},\"concepts\":{},\"entities\":{},\"keywords\":{}}}"^"[YOUR-IBM-WATSON-NATURAL-LANGUAGE-UNDERSTANDING-URL]/v1/analyze?version=2019-07-12"^> MediumArticleAnalysisOutput.txtecho Finished!pause

W-25

If you’re not too familiar with programming, this might look a little imposing, but don’t worry! We’ll go through what it means line-by-line.

The first line is the simplest, this contains the title for the window running the program. It isn’t necessary, but it can be easier to keep your workspace organized when you don’t just have multiple windows titles ‘Command Prompt.’

The second line contains a command called ‘curl,’ often stylized ‘cURL.’ This is short for ‘Client URL,’ and as one might imagine, the command is used to transfer data between a client computer and a server computer located at a certain URL. As such, you can think of the following code as a ‘letter’ that is eventually going to be sent to the IBM Watson service we set up earlier, in order to get a request.

As we continue down the second line, we see ‘-X POST.’ The ‘-X’ is what is known as a ‘flag,’ which is essentially a way to change an option for a specific command. A command can operate in many ways, and by using flags we can select a specific option. Normally, the ‘curl’ command only receives data from a server. In this case, we want to send data before receiving a response, so we use the ‘-X’ flag to change the method we’re using to interact with the server, and then the ‘POST’ option for that flag in order to signal to the computer that we’re sending data.

The next flag we see on the second line is the ‘-u’ flag. This flag tells the server-side computer that we’re sending some sort of credentials in order to identify ourselves. Generally, this would be a username and password, but in our case it’s going to be the API Key we noted down earlier. Go ahead and replace the ‘[YOUR-API-KEY]’ block with the API Key you recorded in step 15. Make sure to delete the square brackets! We don’t need those.

The third line calls forth the ‘ — header’ flag. A header is any sort of supplemental data, which tells a computer how to process your main data. In this case, the header is telling the server-side computer that we’re sending it JSON information. JSON stands for JavaScript Object Notation, and it is a data format derived from the popular JavaScript programming language.

The fourth line is where we get to the ‘heart’ of our code. Here we have the data that is being sent to IBM’s server for processing. Paste the URL of the web article you selected in step 19 where you see ‘[CHOSEN-ARTICLE-URL]’ and again, make sure to remove the square brackets! One thing to be careful about is the backslash (‘\’) that comes after the URL. Make sure you don’t delete this! Otherwise the computer will find it hard to recognize where our URL ends.

After you paste in the URL, take a look at the rest of the line. You should see the word ‘features.’ This tells IBM what features of the AI Model we want to utilize, here we have selected ‘sentiment,’ ‘categories,’ ‘concepts,’ ‘entities,’ and ‘keywords.’ There’s a whole list of what we can use in IBM’s documentation here, but for now this should be plenty!

The fifth line has no flag. When the ‘curl’ command encounters a value with no flag, it assumes that this is the URL it’s supposed to access. As such, we want to make sure that we have the URL for our IBM Watson Natural Language Understanding service entered. Replace the ‘[YOUR-IBM-WATSON-NATURAL-LANGUAGE-UNDERSTANDING-URL]’ block with the URL you saved in step 15 earlier. Once again, make sure you didn’t leave the square brackets in!

The sixth line is fairly simple, the ‘>’ tells the computer to send the output of the command to a file instead of just displaying it in the window, and the ‘MediumArticleAnalysisOutput.txt’ provides a name for the file the output will be stored in.

The seventh line ensures that the command line window lets you know that the command has finished, by printing out the word ‘finished,’ and the eight line ensures that the window stays open instead of closing automatically once the file has been fully executed.

Now you should be ready to run your first AI analysis!

Just to double check, make sure you completed the following steps:

Replace the ‘[YOUR-API-KEY]’ block with the API Key you recorded in step 15. Make sure to delete the square brackets! We don’t need those.

Paste the url of the web article you selected in step 19 where you see ‘[CHOSEN-ARTICLE-URL]’ and again, make sure to remove the square brackets! One thing to be careful about is the backslash (‘\’) that comes after the URL. Make sure you don’t delete this!

Replace the ‘[YOUR-IBM-WATSON-NATURAL-LANGUAGE-UNDERSTANDING-URL]’ block with the URL you saved in step 15 earlier. Once again, make sure you didn’t leave the square brackets in!

Save the file, and you should be ready! Your file should look something like this:

If you’re having trouble, I’ve uploaded the file I used here. Just open it in Notepad and replace the API Key and URL

W-26

Exit out of Notepad, and go back to the file explorer window showing the folder you created earlier.

W-27

Double click on the file, and you should see the following screen pop up:

If so, congratulations! You’ve just completed your first AI analysis. Now, let’s take a look at the results.

W-28

When you head back to the folder containing your batch file, you should now see a second file titled “MediumArticleAnalysisOutput.txt’ which contains the results from your analysis. Open the file to see the results.

W-29

Let’; go through what we see.

The first block of text is titled “usage,” and this tells us what all Watson has analyzed for us. ‘text_units’ essentially tells us how many articles Watson has analyzed, and ‘text_characters’ tells us how many characters were in those articles. ‘features’ tells us how many of Watson’s features we used.

The next block of text contains the first piece of analysis Watson has given us: a sentiment analysis. A sentiment analysis tells us the overall tone of a piece of language; whether it is positive or negative on a scale from -1 to 1. The closer it gets to -1, the more negative the language is. The closer it gets to 1, the more positive the language is. This article seems to be fairly negative, with a sentiment score of around -0.57. Sentiment analysis can be extremely useful for companies and politicians as they analyze recent news stories and social media activity. It lets them know how they’re being perceived by the public, without having to manually sort through thousands of articles and posts.

Below that is the section titled ‘keywords.’ Here, Watson analyzes the article to determine the most relevant keywords for describing the text. It then assigns these keywords a score from 0 to 1 in order to describe how relevant they are.

Below the keywords section, is a section titled ‘entities.’ Entities tell us the subjects described in an article. They tend to be more specific than keywords. For example in an article about Dell laptops, a keyword might be ‘personal-computer manufacturers,’ while an entity might be ‘Dell Inc.’ Watson tells us the type of entity, the name of the entity, how relevant it is to the article on a scale of 0 to 1, how confident it is in its assessment on a scale from 0 to 1, and how many times the entity is mentioned in the article.

Next is the section titled ‘concepts.’ Concepts tell us what the article is about, without relying exclusively on words within the text. Keywords give us general descriptors about portions of the article, entities give us specific subjects mentioned in the article, but concepts tell us the general categories of information an article might fall into. For example, an article about Microsoft Azure would return the concept ‘Cloud Computing,’ even if cloud computing isn’t explicitly mentioned anywhere in the article.

The last section is titled ‘categories.’ Categories sort the analyzed language in accordance with IBM’s five-level taxonomy. IBM’s taxonomy starts by sorting the article into one of 21 ‘level 1’ categories. It then sorts it further into more specific categories based on the article’s content, until it can no longer confidently analyse the overall subject of the article. Watson also assigns a score to the category, which tells us on a scale from 0 to 1 how well it thinks the article falls into the category.

W-30

Now that we’ve analyzed an article, let’s analyse our own language!

Follow steps W-21 to W-23 again in order to create a new batch file in the folder, only this time name the file ‘MediumSentenceAnalysis.bat’

W-31

Now, copy the following code into Notepad:

title Learn to use an Artificial Intelligence: Sentence Analysis

curl -X POST -u "apikey:[YOUR-API-KEY]"^

--header "Content-Type: application/json"^

--data "{\"text\":\"[YOUR-SENTENCE]\",\"features\":{\"sentiment\":{\"targets\":[\"[TARGET-1]\",\"[TARGET-2]\",\"[TARGET-3]\"]},\"keywords\":{\"emotion\":true}}}"^

"[YOUR-IBM-WATSON-NATURAL-LANGUAGE-UNDERSTANDING-URL]/v1/analyze?version=2019-07-12"^

> MediumSentenceAnalysisOutput.txt

echo Finished!

pause

W-32

In step W-25 above, we replaced the ‘[YOUR-API-KEY]’ and ‘[YOUR-IBM-WATSON-NATURAL-LANGUAGE-UNDERSTANDING-URL]’ blocks respectively with the API Key and URL we noted down in step 15, making sure to remove the square brackets. I explained what that does in step W-25, so be sure to scroll up and give it a read through in case you don’t recall.

You’ll notice that the code looks identical to what we saw for the article analysis earlier, with the exception of line 4, which starts with ‘ — data.’

If you recall from earlier, the ‘ — data’ flag contains the information that we’re sending to Watson to analyze, so it makes sense that this would be the only thing to change. The first block in that line reads ‘[YOUR-SENTENCE],’ and we’ll be replacing this with a sentence or a few sentences to analyze.

Go ahead and think of something you want Watson to read! There are a few things to keep in mind though. You want to make sure there is some sentiment or intention in the sentence(s). For example, using the sentences ‘The sky is blue. Grass is green.’ would be extremely boring for Watson to analyze, because they’re simply statements of fact.

I used the sentences ‘Dogs are awesome; they’re always there to pick us up! Spiders are terrifying; I can’t stand the way they scurry around!’ because they’re statements of opinion, not fact, and because they don’t explicitly tell Watson what I mean. For example, I could have used ‘I love dogs! I hate spiders!’ as my sentences, but that would leave no work for the AI model to do. Feel free to use the same sentences as me, or to choose your own!

If you scroll to the right, you’ll see the word ‘sentiment.’ If you recall from earlier, a sentiment analysis determines how positive or negative the analyzed language is. Further to the right, we see three blocks which read ‘[TARGET-1],’ ‘[TARGET-2],’ and ‘[TARGET-3].’ This time, when we conduct the sentiment analysis, we’re going to choose specific subjects for Watson to focus on. Instead of just telling us the sentiment of the language as a whole, this will give us the sentiment of the language in regards to certain subjects.

Make sure you choose targets! If you don’t designate targets, you wont get a very accurate sentiment analysis. The results from this exercise will show us why.

The sentences ‘Dogs are awesome; they’re always there to pick us up! Spiders are terrifying; I can’t stand the way they scurry around!’ may be fairly neutral when looked at together, but they would clearly be positive in regard to dogs, and negative in regards to spiders. Choosing targets allows us to ensure we don’t miss anything by overgeneralizing. The three targets I chose are ‘Dogs,’ ‘Spiders,’ and ‘Cats.’ Two of these are clearly mentioned in my sentence, while one isn’t. I wanted to see what Watson does in this scenario.

If you want to use my sentence and targets, copy the fourth line from here:

--data "{\"text\":\"Dogs are awesome; They're always there to pick us up! Spiders are terrifying; I can't stand the way they scurry around!\",\"features\":{\"sentiment\":{\"targets\":[\"dogs\",\"spiders\",\"cats\"]},\"keywords\":{\"emotion\":true}}}"^

Copying from the paragraph tends to lead to errors, so make sure not to do so! When we copy and paste certain fonts, instead of pasting single and double quotation marks properly like this:

single: '

double: "

Pasted quotation marks often end up looking like this:

single: ‘ or ’

double: “ or ”

These quotation marks work well for improving readability for humans, but they can confuse computers, as they’re technically different characters.

If you continue down the line, you can see that we’re once again using Watson’s keywords feature. This time, however, we’re adding an option to keywords, called ‘emotion.’ This means that for each keyword Watson identifies, it will also evaluate how the language seems to feel about the keyword. The emotions Watson analyzes for are sadness, joy, fear, disgust, and anger.

To recap:

Replace the ‘[YOUR-API-KEY]’ block with the API Key you recorded in step 15. Make sure to delete the square brackets! We don’t need those.

Replace the ‘[YOUR-IBM-WATSON-NATURAL-LANGUAGE-UNDERSTANDING-URL]’ block with the URL you saved in step 15 earlier. Once again, make sure you didn’t leave the square brackets in!

Replace the ‘[YOUR-SENTENCE]’ block with a chosen sentence, or a couple sentences of your choosing. No square brackets! Make sure not to delete the backslash (‘\’) at the end.

Replace ‘[TARGET-1],’ ‘[TARGET-2],’ and ‘[TARGET-3]’ with three targets for the sentiment analysis. No square brackets! Make sure not to delete the backslash (‘\’) at the end.

If you’re having trouble, I’ve uploaded the file I used here. Just open it in Notepad and replace the API Key and URL

Go ahead and save the file. Here is what my code looked like:

W-33

Close out of Notepad and head back to the folder the file is saved in. Run the batch file, and you should once again get a window that looks like this:

If so, your analysis should be complete!

W-34

Head to your folder, and you should see a new file called ‘MediumSentenceAnalysisOutput.txt’ which contains the analysis from the AI model. Open to see what Watson has to say about our sentence. Here’s what my file looks like:

As you can see, my sentiment analysis showed a highly positive sentiment in regards to dogs, and a highly negative sentiment in regards to spiders. You can see how choosing targets for a sentiment analysis can be useful; the overall score of the sentences was positive, as shown by the score under ‘document,’ even though my views on spiders were clearly negative. Watson completely ignored the ‘cats’ target I had set, as the word ‘cats’ didn’t appear anywhere in my text.

Scrolling down, we come to the ‘keywords’ section. For me, ‘Spiders,’ and ‘Dogs,’ were the most relevant keywords in that order. Watson found that my most significant emotion in regards to spiders was fear, and my most significant emotion in regards to dogs was joy.

There you have it! That was a quick introduction to utilizing AI models with Watson. If you’re interested in learning more about what you can do with Watson’s Natural Language Understanding service, their documentation is available here. For more information about Watson in general, see here.

I chose Watson in part because of today’s date, but I also find it to be the most accessible and easy-to-use AI platform available today. Google, Amazon, and Microsoft also have incredibly robust and powerful AI offerings. There’s a great introduction to Google Cloud Platform and TensorFlow here, Microsoft’s Azure AI here, and Amazon’s AWS AI Services here if you’re interested. Microsoft’s tutorial is probably the easiest and most accessible of the three, but I’d recommend Google’s course if you’re interested in a thorough and easy-to-understand introduction to artificial intelligence and machine learning. Unfortunately, AWS isn’t nearly as learner-friendly as its competitors, but Timo Böhm has still managed to put together a spectacular introduction.

I hope this was an informative and enjoyable experience for everyone following along, thank you for reading!