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

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 macOS. If you’re a Windows user, click here to view the relevant guide.

Let’s get started!

M-21

Open TextEdit by searching for it in Launchpad, or finding it in your list of apps.

M-22

Go to File > Save…, and navigate to the folder you created in step 17. Save the file as MediumArticleAnalysis.

M-23

Now, if you noticed earlier, this file is saved as a Rich Text File. Unfortunately, a Rich Text File can’t be used to execute the command we want, so to change the format we’re going to perform the following steps:

Go to the top bar in TextEdit, and click Format > Make Plain Text. Close TextEdit completely. Go to Finder, and navigate to the folder the file is located in. Right click on the file, and click Rename. Add ‘.command’ to the end of the files name, changing nothing else, and press return. You should get a prompt asking you ‘Are you sure you want to change the extension from “.txt” to “.command”?’

7. Click Use “.command”

8. Right click the file, select Open With, and select TextEdit.

Now you should be ready to start putting together some commands for Watson!

The *.command* file is a terminal script file for macOS. A script file saves commands that can be entered into the macOS terminal.

Doing this:

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

M-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": "

"features": {

"sentiment": {},

"categories": {},

"concepts": {},

"entities": {},

"keywords": {}

}

}' \

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

> MediumArticleAnalysisOutput.txt

read -rsp $'Finished! Press any key to continue...

' -n 1 key PROMPT_COMMAND='echo -ne "\033]0;Learn to use an Artificial Intelligence Model: Article Analysis\007"'curl -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.txtread -rsp $'Finished! Press any key to continue...

' -n 1 key

M-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 next line (which looks like the third line in the Medium article) 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 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 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 next 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 next line is where we get to the ‘heart’ of our code. In this block, starting with ‘ — data’ and ending with ‘}’ \’ 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!

After you paste in the URL, take a look at the rest of the block. 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 next 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 second-to-last 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 last line ensures that the command line window lets you know that the command has finished, by printing out the word ‘finished,’ and ensures that the window stays open instead of closing automatically once the file has been fully executed.

And now you should be ready to run your first AI analysis!

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

Replace ‘[YOUR-API-KEY]’ 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 ‘[YOUR-IBM-WATSON-NATURAL-LANGUAGE-UNDERSTANDING-URL]’ 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 TextEdit and replace the API Key and URL

M-26

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

M-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.

If you’re using a computer without full privileges, you might have some trouble with this step. Don’t worry, there’s another way!

Start by going to Launchpad, and opening ‘Terminal.’ We’re going to have to navigate to the folder our file is stored in. To do that, type in the following command:

cd Documents/'Your Folder Name'

‘Your Folder Name’ should be replaced by the name of the folder you created to store your files in step 17. For me, the command was:

cd Documents/'Learning AI'

3. Now, type in the following command:

sh MediumArticleAnalysis.command

There you go! Now you should be set to continue.

M-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.

M-29

Let’s 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 analyze 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.

M-30

Now that we’ve analyzed an article, let’s analyze 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.command’

M-31

Now, copy the following code into TextEdit:

PROMPT_COMMAND='echo -ne "\033]0;Learn to use an Artificial Intelligence Model: Sentence Analysis\007"'

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

read -rsp $'Finished! Press any key to continue...

' -n 1 key

M-32

In step M-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 M-25, so be sure to scroll up and give it a read through in case you don’t recall. Do the same thing here.

You’ll notice that the code looks identical to what we saw for the article analysis earlier, with the exception of the block starting with ‘ — data’ and ending with ‘}’ \’

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 line in that block contains a portionn which 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.

ALSO! Be very careful not to use semicolons or apostrophes in your sentences! Your computer might think you’re ending the command or sentence! Developers use ‘escape characters’ to get around this limitation, but we can save that for another date.

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.

The sentences ‘Dogs are awesome! they are always there to pick us up! Spiders are terrifying! I can not 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 data block from here:

--data '{

"text": "Dogs are awesome! They are always there to pick us up! Spiders are terrifying! I can not stand the way they scurry around!",

"features": {

"sentiment": {

"targets": [

"dogs",

"spiders",

"cats"

]

},

"keywords": {

"emotion": true

}

}

}' \

If you continue down the block, 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 ‘[YOUR-API-KEY]’ with the API Key you recorded in step 15. Make sure to delete the square brackets! We don’t need those.

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

Replace ‘[YOUR-SENTENCE]’ 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 TextEdit and replace the API Key and URL

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

M-33

Close out of TextEdit 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!

If you’re using a computer without full privileges, you might have some trouble with this step. Don’t worry, there’s another way!

Start by going to Launchpad, and opening ‘Terminal.’ We’re going to have to navigate to the folder our file is stored in. To do that, type in the following command:

cd Documents/'Your Folder Name'

‘Your Folder Name’ should be replaced by the name of the folder you created to store your files in step 17. For me, the command was:

cd Documents/'Learning AI'

3. Now, type in the following command:

sh MediumSentenceAnalysis.command

There you go! Now you should be set to continue.

W-33

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, I haven’t found AWS to be nearly as learner-friendly as its competitors, but Timo Böhm has still managed to put together a spectacular introduction here on Medium.

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