IBM Watson Language
| Deprecated: This ibm-watson-language is deprecated and may be removed in a future release. |
Since Camel 4.16
Only producer is supported
The IBM Watson Language component provides natural language understanding capabilities using IBM Watson Natural Language Understanding.
Prerequisites
You must have a valid IBM Cloud account and have provisioned a Watson Natural Language Understanding service instance.
URI Format
ibm-watson-language:label[?options]
Where label is a logical name for the endpoint.
You can append query options to the URI in the following format:
?option1=value&option2=value&…
Configuring Options
Camel components are configured on two separate levels:
-
component level
-
endpoint level
Configuring Component Options
At the component level, you set general and shared configurations that are, then, inherited by the endpoints. It is the highest configuration level.
For example, a component may have security settings, credentials for authentication, urls for network connection and so forth.
Some components only have a few options, and others may have many. Because components typically have pre-configured defaults that are commonly used, then you may often only need to configure a few options on a component; or none at all.
You can configure components using:
-
the Component DSL.
-
in a configuration file (
application.properties,*.yamlfiles, etc). -
directly in the Java code.
Configuring Endpoint Options
You usually spend more time setting up endpoints because they have many options. These options help you customize what you want the endpoint to do. The options are also categorized into whether the endpoint is used as a consumer (from), as a producer (to), or both.
Configuring endpoints is most often done directly in the endpoint URI as path and query parameters. You can also use the Endpoint DSL and DataFormat DSL as a type safe way of configuring endpoints and data formats in Java.
A good practice when configuring options is to use Property Placeholders.
Property placeholders provide a few benefits:
-
They help prevent using hardcoded urls, port numbers, sensitive information, and other settings.
-
They allow externalizing the configuration from the code.
-
They help the code to become more flexible and reusable.
The following two sections list all the options, firstly for the component followed by the endpoint.
Component Options
The IBM Watson Language component supports the following options which are listed below.
| Name | Description | Default | Type |
|---|---|---|---|
The URL of the Natural Language Understanding service instance, as given in its service credentials. If not set, the SDK uses https://api.us-south.natural-language-understanding.watson.cloud.ibm.com (Dallas). | String | ||
Whether to request the categories of the content in a hierarchical taxonomy, by default the top three. The CamelIBMWatsonLanguageAnalyzeCategories header replaces this option. | false | boolean | |
Whether to request the high-level concepts of the content, which can include concepts that the text does not name. The CamelIBMWatsonLanguageAnalyzeConcepts header replaces this option. | false | boolean | |
Whether to request the emotions of the whole document: a score from 0 to 1 for anger, disgust, fear, joy and sadness. The service supports it for English only. The CamelIBMWatsonLanguageAnalyzeEmotion header replaces this option. | false | boolean | |
Whether to request the entities, such as people, cities and organizations, that the content mentions. The CamelIBMWatsonLanguageAnalyzeEntities header replaces this option. | true | boolean | |
Whether to request the important keywords of the content. The CamelIBMWatsonLanguageAnalyzeKeywords header replaces this option. | true | boolean | |
Whether to request the sentiment of the whole document. The producer then sets the CamelIBMWatsonLanguageSentimentScore and CamelIBMWatsonLanguageSentimentLabel headers. The CamelIBMWatsonLanguageAnalyzeSentiment header replaces this option. | true | boolean | |
The configuration shared by the endpoints. Each endpoint starts from a copy of it, and its endpoint options replace single values. | WatsonLanguageConfiguration | ||
Whether the producer should be started lazy (on the first message). By starting lazy you can use this to allow CamelContext and routes to startup in situations where a producer may otherwise fail during starting and cause the route to fail being started. By deferring this startup to be lazy then the startup failure can be handled during routing messages via Camel’s routing error handlers. Beware that when the first message is processed then creating and starting the producer may take a little time and prolong the total processing time of the processing. | false | boolean | |
The operation that the producer performs: analyzeText analyzes the given text, and analyzeUrl the web page at the given URL, which the service retrieves. The CamelIBMWatsonLanguageOperation header replaces this option. The producer fails if neither is set. Enum values:
| WatsonLanguageOperations | ||
Whether autowiring is enabled. This is used for automatic autowiring options (the option must be marked as autowired) by looking up in the registry to find if there is a single instance of matching type, which then gets configured on the component. This can be used for automatic configuring JDBC data sources, JMS connection factories, AWS Clients, etc. | true | boolean | |
Used for enabling or disabling all consumer based health checks from this component. | true | boolean | |
Used for enabling or disabling all producer based health checks from this component. Notice: Camel has by default disabled all producer based health-checks. You can turn on producer checks globally by setting camel.health.producersEnabled=true. | true | boolean | |
Required The IBM Cloud API key of the Natural Language Understanding service instance. The IBM Cloud IAM authenticator of the SDK exchanges it for an access token. The endpoint fails to start if it is not set. | String |
Endpoint Options
The IBM Watson Language endpoint is configured using URI syntax:
ibm-watson-language:label
With the following path and query parameters:
Query Parameters
| Name | Description | Default | Type |
|---|---|---|---|
The URL of the Natural Language Understanding service instance, as given in its service credentials. If not set, the SDK uses https://api.us-south.natural-language-understanding.watson.cloud.ibm.com (Dallas). | String | ||
Whether to request the categories of the content in a hierarchical taxonomy, by default the top three. The CamelIBMWatsonLanguageAnalyzeCategories header replaces this option. | false | boolean | |
Whether to request the high-level concepts of the content, which can include concepts that the text does not name. The CamelIBMWatsonLanguageAnalyzeConcepts header replaces this option. | false | boolean | |
Whether to request the emotions of the whole document: a score from 0 to 1 for anger, disgust, fear, joy and sadness. The service supports it for English only. The CamelIBMWatsonLanguageAnalyzeEmotion header replaces this option. | false | boolean | |
Whether to request the entities, such as people, cities and organizations, that the content mentions. The CamelIBMWatsonLanguageAnalyzeEntities header replaces this option. | true | boolean | |
Whether to request the important keywords of the content. The CamelIBMWatsonLanguageAnalyzeKeywords header replaces this option. | true | boolean | |
Whether to request the sentiment of the whole document. The producer then sets the CamelIBMWatsonLanguageSentimentScore and CamelIBMWatsonLanguageSentimentLabel headers. The CamelIBMWatsonLanguageAnalyzeSentiment header replaces this option. | true | boolean | |
The operation that the producer performs: analyzeText analyzes the given text, and analyzeUrl the web page at the given URL, which the service retrieves. The CamelIBMWatsonLanguageOperation header replaces this option. The producer fails if neither is set. Enum values:
| WatsonLanguageOperations | ||
Whether the producer should be started lazy (on the first message). By starting lazy you can use this to allow CamelContext and routes to startup in situations where a producer may otherwise fail during starting and cause the route to fail being started. By deferring this startup to be lazy then the startup failure can be handled during routing messages via Camel’s routing error handlers. Beware that when the first message is processed then creating and starting the producer may take a little time and prolong the total processing time of the processing. | false | boolean | |
Required The IBM Cloud API key of the Natural Language Understanding service instance. The IBM Cloud IAM authenticator of the SDK exchanges it for an access token. The endpoint fails to start if it is not set. | String |
Message Headers
The IBM Watson Language component supports the following message header(s), which is/are listed below:
| Name | Description | Default | Type |
|---|---|---|---|
CamelIBMWatsonLanguageOperation (producer) Constant: | The operation to perform, analyzeText or analyzeUrl. It replaces the operation option. | String | |
CamelIBMWatsonLanguageText (producer) Constant: | The text to analyze for analyzeText. If not set, the producer analyzes the message body. | String | |
CamelIBMWatsonLanguageUrl (producer) Constant: | The URL of the web page to analyze for analyzeUrl, which the service retrieves. If not set, the producer reads the URL from the message body. | String | |
CamelIBMWatsonLanguageAnalyzeSentiment (producer) Constant: | Whether to request the sentiment of the document. It replaces the analyzeSentiment option. | Boolean | |
CamelIBMWatsonLanguageAnalyzeEmotion (producer) Constant: | Whether to request the emotions of the document. It replaces the analyzeEmotion option. | Boolean | |
CamelIBMWatsonLanguageAnalyzeEntities (producer) Constant: | Whether to request the entities of the content. It replaces the analyzeEntities option. | Boolean | |
CamelIBMWatsonLanguageAnalyzeKeywords (producer) Constant: | Whether to request the keywords of the content. It replaces the analyzeKeywords option. | Boolean | |
CamelIBMWatsonLanguageAnalyzeConcepts (producer) Constant: | Whether to request the concepts of the content. It replaces the analyzeConcepts option. | Boolean | |
CamelIBMWatsonLanguageAnalyzeCategories (producer) Constant: | Whether to request the categories of the content. It replaces the analyzeCategories option. | Boolean | |
CamelIBMWatsonLanguageLanguage (producer) Constant: | The ISO 639-1 code of the language that the service detected and used for the analysis, for example en. The producer sets it after each analysis. | String | |
CamelIBMWatsonLanguageSentimentScore (producer) Constant: | The sentiment score of the whole document, from -1 (negative) to 1 (positive). The producer sets it when sentiment is requested. | Double | |
CamelIBMWatsonLanguageSentimentLabel (producer) Constant: | The sentiment label of the whole document: positive, neutral or negative. The producer sets it when sentiment is requested. | String |
Message Body
The message body at each side of the IBM Watson Language component: Consumer is the body of the messages the consumer creates, Consumer reply the bodies a consumer that answers its caller can reply with, Producer input the bodies the producer can send or write, and Producer output the body after the producer.
| Side | Body | Description |
|---|---|---|
Producer input |
| The text to analyze for analyzeText, or the URL of the page for analyzeUrl, read as text, unless the CamelIBMWatsonLanguageText or CamelIBMWatsonLanguageUrl header gives it. Without the header, no body or a blank one fails, and a Map or a POJO is analyzed as its toString(): marshal it first. |
Producer output |
| The results of the features enabled (sentiment, entities, keywords and so on), with the sentiment score and label and the language in headers too. Converted to text, it is the JSON of the results. |
The bodies are the ones with the default options. A side that is not listed is not known, and tooling does not guess it. The carriers, the sides and the tools that use them are described in Message Body Types.
Required IBM Watson Language component options
You must provide the apiKey for authentication and optionally the serviceUrl for your Watson service instance.
Usage
IBM Watson Natural Language Understanding
This component integrates with IBM Watson Natural Language Understanding service to analyze text and extract metadata like sentiment, entities, keywords, concepts, categories, and emotions.
Authentication
IBM Watson services use IBM Cloud IAM (Identity and Access Management) for authentication. You need to provide:
-
apiKey: Your IBM Cloud API key -
serviceUrl: The endpoint URL for your Watson service instance (optional, defaults to the public endpoint)
You can find your service credentials in the IBM Cloud console.
Operations
-
analyzeText- Analyze text content provided in the message body or header -
analyzeUrl- Analyze content from a URL
Analysis Features
You can enable/disable various analysis features:
-
analyzeSentiment- Detect positive, negative, or neutral sentiment (default: true) -
analyzeEntities- Extract people, companies, organizations, cities, etc. (default: true) -
analyzeKeywords- Extract important keywords (default: true) -
analyzeEmotion- Detect emotions like joy, anger, sadness (default: false) -
analyzeConcepts- Identify high-level concepts (default: false) -
analyzeCategories- Classify content into categories (default: false)
Examples
Sentiment Analysis Example
Analyze sentiment of text:
-
Java
-
YAML
from("direct:sentiment")
.setBody(constant("I love this product! It's amazing!"))
.to("ibm-watson-language:default?apiKey=RAW(yourApiKey)&operation=analyzeText&analyzeSentiment=true&analyzeEntities=false&analyzeKeywords=false")
.process(exchange -> {
AnalysisResults results = exchange.getIn().getBody(AnalysisResults.class);
String sentiment = results.getSentiment().getDocument().getLabel();
Double score = results.getSentiment().getDocument().getScore();
// Or use convenience headers:
String sentimentLabel = exchange.getIn().getHeader("CamelIBMWatsonLanguageSentimentLabel", String.class);
Double sentimentScore = exchange.getIn().getHeader("CamelIBMWatsonLanguageSentimentScore", Double.class);
}); - from:
uri: direct:sentiment
steps:
- setBody:
constant: "I love this product! It's amazing!"
- to:
uri: ibm-watson-language:default
parameters:
apiKey: RAW(yourApiKey)
operation: analyzeText
analyzeSentiment: true
analyzeEntities: false
analyzeKeywords: false
- log: "Sentiment: ${header.CamelIBMWatsonLanguageSentimentLabel} (${header.CamelIBMWatsonLanguageSentimentScore})" The full AnalysisResults object is returned in the body, containing all requested analyses (sentiment, entities, keywords, etc.). Common values like sentiment score and label are also available as headers for convenience.
Extract Entities and Keywords
Analyze text to extract entities and keywords:
-
Java
-
YAML
from("direct:analyze")
.setBody(constant("IBM Watson is an AI platform developed by IBM in Armonk, New York."))
.to("ibm-watson-language:default?apiKey=RAW(yourApiKey)&operation=analyzeText&analyzeSentiment=false&analyzeEntities=true&analyzeKeywords=true")
.process(exchange -> {
AnalysisResults results = exchange.getIn().getBody(AnalysisResults.class);
results.getEntities().forEach(entity ->
System.out.println("Entity: " + entity.getText() + " (" + entity.getType() + ")")
);
results.getKeywords().forEach(keyword ->
System.out.println("Keyword: " + keyword.getText() + " (relevance: " + keyword.getRelevance() + ")")
);
}); - from:
uri: direct:analyze
steps:
- setBody:
constant: "IBM Watson is an AI platform developed by IBM in Armonk, New York."
- to:
uri: ibm-watson-language:default
parameters:
apiKey: RAW(yourApiKey)
operation: analyzeText
analyzeSentiment: false
analyzeEntities: true
analyzeKeywords: true
- log: "Analysis complete: ${body}" Comprehensive Analysis
Analyze text with multiple features enabled:
-
Java
-
YAML
from("direct:comprehensive")
.setBody(constant("Apple Inc. announced record profits. The CEO praised the team's innovation."))
.to("ibm-watson-language:default?apiKey=RAW(yourApiKey)&operation=analyzeText&analyzeSentiment=true&analyzeEntities=true&analyzeKeywords=true&analyzeEmotion=true")
.process(exchange -> {
AnalysisResults results = exchange.getIn().getBody(AnalysisResults.class);
System.out.println("Sentiment: " + results.getSentiment().getDocument().getLabel());
System.out.println("Entities: " + results.getEntities().size());
System.out.println("Keywords: " + results.getKeywords().size());
if (results.getEmotion() != null) {
System.out.println("Emotions: " + results.getEmotion().getDocument().getEmotion());
}
}); - from:
uri: direct:comprehensive
steps:
- setBody:
constant: "Apple Inc. announced record profits. The CEO praised the team's innovation."
- to:
uri: ibm-watson-language:default
parameters:
apiKey: RAW(yourApiKey)
operation: analyzeText
analyzeSentiment: true
analyzeEntities: true
analyzeKeywords: true
analyzeEmotion: true Analyze URL
Analyze content from a webpage:
-
Java
-
YAML
from("direct:url")
.setHeader("CamelIBMWatsonLanguageUrl", constant("https://www.ibm.com/watson"))
.to("ibm-watson-language:default?apiKey=RAW(yourApiKey)&operation=analyzeUrl&analyzeSentiment=true&analyzeKeywords=true")
.process(exchange -> {
AnalysisResults results = exchange.getIn().getBody(AnalysisResults.class);
System.out.println("Page language: " + results.getLanguage());
System.out.println("Page sentiment: " + results.getSentiment().getDocument().getLabel());
}); - from:
uri: direct:url
steps:
- setHeader:
name: CamelIBMWatsonLanguageUrl
constant: "https://www.ibm.com/watson"
- to:
uri: ibm-watson-language:default
parameters:
apiKey: RAW(yourApiKey)
operation: analyzeUrl
analyzeSentiment: true
analyzeKeywords: true
- log: "Page sentiment: ${header.CamelIBMWatsonLanguageSentimentLabel}" Dynamic Feature Selection
Use headers to dynamically enable/disable analysis features:
-
Java
-
YAML
from("direct:dynamic")
.setBody(constant("This is great news for the company!"))
.setHeader(WatsonLanguageConstants.ANALYZE_SENTIMENT, constant(true))
.setHeader(WatsonLanguageConstants.ANALYZE_EMOTION, constant(true))
.setHeader(WatsonLanguageConstants.ANALYZE_KEYWORDS, constant(false))
.to("ibm-watson-language:default?apiKey=RAW(yourApiKey)&operation=analyzeText"); - from:
uri: direct:dynamic
steps:
- setBody:
constant: "This is great news for the company!"
- setHeader:
name: CamelIBMWatsonLanguageAnalyzeSentiment
constant: true
- setHeader:
name: CamelIBMWatsonLanguageAnalyzeEmotion
constant: true
- setHeader:
name: CamelIBMWatsonLanguageAnalyzeKeywords
constant: false
- to:
uri: ibm-watson-language:default
parameters:
apiKey: RAW(yourApiKey)
operation: analyzeText Custom Service URL
If your Watson service is in a specific region, specify the service URL:
-
Java
-
YAML
from("direct:regional")
.to("ibm-watson-language:default?apiKey=RAW(yourApiKey)&serviceUrl=https://api.us-south.natural-language-understanding.watson.cloud.ibm.com&operation=analyzeText"); - from:
uri: direct:regional
steps:
- to:
uri: ibm-watson-language:default
parameters:
apiKey: RAW(yourApiKey)
serviceUrl: "https://api.us-south.natural-language-understanding.watson.cloud.ibm.com"
operation: analyzeText Dependencies
Maven users will need to add the following dependency to their pom.xml.
pom.xml
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-ibm-watson-language</artifactId>
<version>x.x.x</version>
<!-- use the same version as your Camel core version -->
</dependency> where x.x.x is the version number of Camel.