Product

Automated Text Analysis with PowerShell and AYLIEN Text Analysis API

Introduction

This is a continuation of our getting up and running with AYLIEN’s Text Analysis API blog series. In our previous ‘getting started’ blog, we went through the process of signing up for the API, obtaining your Application ID and Application Key and making calls with Node.JS. For this blog, we are going to focus on working with the API using Windows PowerShell.

We’re going to walk you through how easy it is to perform some basic Text Analysis processes like, analyzing the sentiment of a piece of text, detecting what language a piece of text is written in and finally generating some hashtags for a URL that can be used for maximum exposure on social media, using PowerShell.

The script we’re going to use was put together by one of our Text Analysis API users, Doug Finke. Doug is the author of ‘PowerShell for Developers’, a six time recipient of the Microsoft Most Valuable Professional (MVP) for PowerShell and works with former PowerShell Team member James Brundage at Start-Automating. Start-Automating is a PowerShell specialist company delivering PowerShell automation, tools, consulting and training.

You can catch up with Doug at his blog Development in a Blink.

Getting Started

To begin open a Windows Powershell ISE by typing “powershell ise” in the Windows Run box.

To open a new PowerShell script window, Click File -> New.

Once you have your new window open, Copy and Paste the following PowerShell script into the script editor:


function Invoke-SentimentAnalysis {
       param(
       [Parameter(ValueFromPipeline=$true)]
       [string]$Text,
       $ApplicationId,
       $ApplicationKey
   )
   Begin {
       if($env:AylienApplicationId)  {$ApplicationId=$env:AylienApplicationId}
       if($env:AylienApplicationKey) {$ApplicationKey=$env:AylienApplicationKey}
       $Headers=@{
           "X-AYLIEN-TextAPI-Application-ID"=$ApplicationId
           "X-AYLIEN-TextAPI-Application-Key"=$ApplicationKey
       }
   }
   Process {
       $address = "https://api.aylien.com/api/v1/sentiment?text=$($Text)"
       Invoke-RestMethod -Uri $address -Headers $Headers
   }
}

Sentiment Analysis

The script above performs a call to the Sentiment Analysis endpoint of AYLIEN Text Analysis API to analyze the text contained within the $Text parameter, in this case ‘Have a nice day’.

You can invoke the script by running the following in the console pane of Windows Powershell ISE.


$env:AylienApplicationId  = '<Specify Your App ID>'
$env:AylienApplicationKey = '<Specify Your App Key>'
$text = $(
   'Have a nice day.'
)
$text | Invoke-SentimentAnalysis | Format-Table -AutoSize

Note: Replace the <Specify Your App Key> and <Specify Your App ID> placeholders in the snippet with the “Key” and “App ID” with your credentials you received during the sign up process.

Once executed you should receive an output similar to the result below:

text: Have a nice day                   

subjectivity: Subjective   

subjectivity_confidence: 0.9900886100760287 

polarity: Positive     

polarity_confidence: 0.75

Language Detection

To automatically detect the language that a piece of text is written in you can invoke the Language Detection by using the following function:


function Invoke-LanguageDetection {
       param(
       [Parameter(ValueFromPipeline=$true)]
       [string]$Text,
       $ApplicationId,
       $ApplicationKey
   )
   Begin {
       if($env:AylienApplicationId)  {$ApplicationId=$env:AylienApplicationId}
       if($env:AylienApplicationKey) {$ApplicationKey=$env:AylienApplicationKey}
       $Headers=@{
           "X-AYLIEN-TextAPI-Application-ID"=$ApplicationId
           "X-AYLIEN-TextAPI-Application-Key"=$ApplicationKey
       }
   }
   Process {
       $address = "https://api.aylien.com/api/v1/language?text=$($Text)"
       Invoke-RestMethod -Uri $address -Headers $Headers
   }
}

Running the script again from PowerShell as follows:


$env:AylienApplicationId  = '<Specify Your App ID>'
$env:AylienApplicationKey = '<Specify Your App Key>'
$text = $(
   'Can you spot what language this is written in?'
)
$text | Invoke-LanguageDetection | Format-Table -AutoSize

You should be left with the following results:

text: Can you spot what language this is written in?                                 

lang: en        

confidence: 0.9999965739026125

Hashtag Suggestions

Finally, to get a list of suggested hashtags for a given URL you can use the function below:


function Invoke-HashtagExtraction {
       param(
       [Parameter(ValueFromPipeline=$true)]
       [string]$Text,
       $ApplicationId,
       $ApplicationKey
   )
   Begin {
       if($env:AylienApplicationId)  {$ApplicationId=$env:AylienApplicationId}
       if($env:AylienApplicationKey) {$ApplicationKey=$env:AylienApplicationKey}
       $Headers=@{
           "X-AYLIEN-TextAPI-Application-ID"=$ApplicationId
           "X-AYLIEN-TextAPI-Application-Key"=$ApplicationKey
       }
   }
   Process {
       $address = "https://api.aylien.com/api/v1/hashtags?url=$($Text)"
       Invoke-RestMethod -Uri $address -Headers $Headers
   }
}

You can then run the script from PowerShell as follows


$env:AylienApplicationId  = '<Specify Your App ID>'
$env:AylienApplicationKey = '<Specify Your App Key>'

$url = $(
   'http://www.networkworld.com/article/2686045/smartphone/iphone6-review-roundup-the-raves-come-quietly.html'
)

$url | Invoke-HashtagExtraction | Format-Table -AutoSize

to receive the following output:

text: iPhone 6 review roundup – the …                                             

language: en                

hashtags: #BMW6Series, #Samsung, #Appleinc

More PowerShell function wrappers for the rest of the AYLIEN Text Analysis features can be found on GitHub.

For more getting started guides and code snippets to help you get up and running with our API visit our Getting Started page on our website. If you haven’t already done so you can get free access to our API on our sign up page.





Text Analysis API - Sign up





Author


Avatar

Mike Waldron

Head of Marketing & Sales @ AYLIEN A legal convert with a masters degree from Smurfit Business School, Mike runs our Sales and Marketing at AYLIEN. Mike gathered his Sales and Marketing experience with technology companies in Sydney and Dublin before getting the startup itch and joining the team at AYLIEN. Twitter: @MikeWallly