Folks,
In this blog we will explore Microsoft Cognitive Services (Text Analytics API) in R!
This API can detect sentiment, key phrases, topics, and language from your text.
Click here & Register for the free subscription of Microsoft Cognitive Services (Text Analytics).
Here is my free subscription. Free 5,000 transactions per month.
After registering please copy the Subscription Key. This is the Subscription Key which provides access to this API.
Detect Sentiments
Request URL:
https://westus.api.cognitive.microsoft.com/text/analytics/v2.0/sentiment
Request Headers:
Content-Type (optional): Media type of the body sent to the API.
Ocp-Apim-Subscription-Key: Subscription key which provides access to this API.
Request Body
{
"documents": [
{
"language": "string",
"id": "string",
"text": "string"
}
]
}
R Commands & Output:
R Packages required:httr & jsonlite.
# Below is the Request body for the API having text id 1 = Negative sentiments, id 2 = Positive sentiments request_body <- data.frame( language = c("en","en"), id = c("1","2"), text = c("This is wasted! I'm angry","This is awesome! Good Job Team! appreciated") ) # Converting the Request body(Dataframe) to Request body(JSON) request_body_json <- toJSON(list(documents = request_body), auto_unbox = TRUE) # Below we are calling API (Adding Request headers using add_headers) result <- POST("https://westus.api.cognitive.microsoft.com/text/analytics/v2.0/sentiment", body = request_body_json, add_headers(.headers = c("Content-Type"="application/json","Ocp-Apim-Subscription-Key"="my_subscrition_key"))) Output <- content(result) # Show Output Output
Output Score:-
id - "1" score - 0.2324503 id "2" score - 0.9998128
Where scores close to 1 indicate positive sentiment, while scores close to 0 indicate negative sentiment.
Detect Language
Request URL:
https://westus.api.cognitive.microsoft.com/text/analytics/v2.0/languages[?numberOfLanguagesToDetect]
Request Parameters:
numberOfLanguagesToDetect - (Optional) Number of languages to detect. Set to 1 by default.
Request Headers:
Content-Type (optional): Media type of the body sent to the API.
Ocp-Apim-Subscription-Key: Subscription key which provides access to this API.
Request Body:
{ "documents": [ { "id": "string", "text": "string" } ] }
R Commands & Output:
R Packages required:httr & jsonlite.
# Below is the Request body for the API request_body <- data.frame( id = "1", text = "भारतीय धर्म में निर्वाण मुक्ति है", stringsAsFactors = FALSE ) # Converting the Request body(Dataframe) to Request body(JSON) request_body_json <- toJSON(list(documents = request_body), auto_unbox = TRUE) # Below we are calling API (Adding Request headers using add_headers) # Here parameter numberOfLanguagesToDetect=1 result <- POST("https://westus.api.cognitive.microsoft.com/text/analytics/v2.0/languages?numberOfLanguagesToDetect=1", body = request_body_json , add_headers(.headers = c("Content-Type"="application/json","Ocp-Apim-Subscription-Key"="my_subscription_key")) ) Output <- content(result) Output
Output Detected Language :-
name - "Hindi" iso6391Name - "hi" score - 1
Where scores close to 1 indicate 100% certainty that the identified language is true.
You can set numberOfLanguagesToDetect & text as per your requirement. API can detect multiple languages also, see below example where numberOfLanguagesToDetect = 2 .
# Below is the Request body for the API request_body <- data.frame( id = "1", text = "Nirvana is most commonly associated with Buddhism भारतीय धर्म में निर्वाण मुक्ति है", stringsAsFactors = FALSE ) request_body_json <- toJSON(list(documents = request_body), auto_unbox = TRUE) result <- POST("https://westus.api.cognitive.microsoft.com/text/analytics/v2.0/languages?numberOfLanguagesToDetect=2", body = request_body_json , add_headers(.headers = c("Content-Type"="application/json","Ocp-Apim-Subscription-Key"="my_subscription_key")) ) Output <- content(result) Output
For more details & API’s, please visit this Link.
You can also use this R Package for Microsoft Cognitive Services (Text Analytics API)
For me It is not working the way I want. e.g. “I want to open a project”. It is listing only “Project”. Could you suggest how I can include “Open” as well.
LikeLike
didnt get your question. please eloborate.
LikeLike
Thanks Shobhit, that was an extremely useful blog and code snippets
LikeLiked by 1 person
Thanks Mark!
LikeLike