ibm_watson.natural_language_understanding_v1 module¶
Analyze various features of text content at scale. Provide text, raw HTML, or a public URL and IBM Watson Natural Language Understanding will give you results for the features you request. The service cleans HTML content before analysis by default, so the results can ignore most advertisements and other unwanted content. You can create [custom models](https://cloud.ibm.com/docs/services/natural-language-understanding/customizing.html) with Watson Knowledge Studio to detect custom entities, relations, and categories in Natural Language Understanding.
-
class
NaturalLanguageUnderstandingV1
(version, url='https://gateway.watsonplatform.net/natural-language-understanding/api', username=None, password=None, iam_apikey=None, iam_access_token=None, iam_url=None)[source]¶ Bases:
ibm_cloud_sdk_core.base_service.BaseService
The Natural Language Understanding V1 service.
-
default_url
= 'https://gateway.watsonplatform.net/natural-language-understanding/api'¶
-
analyze
(features, text=None, html=None, url=None, clean=None, xpath=None, fallback_to_raw=None, return_analyzed_text=None, language=None, limit_text_characters=None, **kwargs)[source]¶ Analyze text.
Analyzes text, HTML, or a public webpage for the following features: - Categories - Concepts - Emotion - Entities - Keywords - Metadata - Relations - Semantic roles - Sentiment - Syntax (Experimental).
- Parameters
parameters is required. :param str html: The HTML file to analyze. One of the text, html, or url parameters is required. :param str url: The webpage to analyze. One of the text, html, or url parameters is required. :param bool clean: Set this to false to disable webpage cleaning. To learn more about webpage cleaning, see the [Analyzing webpages](https://cloud.ibm.com/docs/services/natural-language-understanding/analyzing-webpages.html) documentation. :param str xpath: An [XPath query](https://cloud.ibm.com/docs/services/natural-language-understanding/analyzing-webpages.html#xpath) to perform on html or url input. Results of the query will be appended to the cleaned webpage text before it is analyzed. To analyze only the results of the XPath query, set the clean parameter to false. :param bool fallback_to_raw: Whether to use raw HTML content if text cleaning fails. :param bool return_analyzed_text: Whether or not to return the analyzed text. :param str language: ISO 639-1 code that specifies the language of your text. This overrides automatic language detection. Language support differs depending on the features you include in your analysis. See [Language support](https://cloud.ibm.com/docs/services/natural-language-understanding?topic=natural-language-understanding-language-support) for more information. :param int limit_text_characters: Sets the maximum number of characters that are processed by the service. :param dict headers: A dict containing the request headers :return: A DetailedResponse containing the result, headers and HTTP status code. :rtype: DetailedResponse
-
list_models
(**kwargs)[source]¶ List models.
Lists Watson Knowledge Studio [custom models](https://cloud.ibm.com/docs/services/natural-language-understanding/customizing.html) that are deployed to your Natural Language Understanding service.
- Parameters
headers (dict) – A dict containing the request headers
- Returns
A DetailedResponse containing the result, headers and HTTP status code.
- Return type
DetailedResponse
-
-
class
AnalysisResults
(language=None, analyzed_text=None, retrieved_url=None, usage=None, concepts=None, entities=None, keywords=None, categories=None, emotion=None, metadata=None, relations=None, semantic_roles=None, sentiment=None, syntax=None)[source]¶ Bases:
object
Analysis results for each requested feature.
- Attr str language
(optional) Language used to analyze the text.
- Attr str analyzed_text
(optional) Text that was used in the analysis.
- Attr str retrieved_url
(optional) URL of the webpage that was analyzed.
- Attr AnalysisResultsUsage usage
(optional) API usage information for the request.
- Attr list[ConceptsResult] concepts
(optional) The general concepts referenced or
alluded to in the analyzed text. :attr list[EntitiesResult] entities: (optional) The entities detected in the analyzed text. :attr list[KeywordsResult] keywords: (optional) The keywords from the analyzed text. :attr list[CategoriesResult] categories: (optional) The categories that the service assigned to the analyzed text. :attr EmotionResult emotion: (optional) The anger, disgust, fear, joy, or sadness conveyed by the content. :attr AnalysisResultsMetadata metadata: (optional) Webpage metadata, such as the author and the title of the page. :attr list[RelationsResult] relations: (optional) The relationships between entities in the content. :attr list[SemanticRolesResult] semantic_roles: (optional) Sentences parsed into subject, action, and object form. :attr SentimentResult sentiment: (optional) The sentiment of the content. :attr SyntaxResult syntax: (optional) Tokens and sentences returned from syntax analysis.
-
class
AnalysisResultsMetadata
(authors=None, publication_date=None, title=None, image=None, feeds=None)[source]¶ Bases:
object
Webpage metadata, such as the author and the title of the page.
- Attr list[Author] authors
(optional) The authors of the document.
- Attr str publication_date
(optional) The publication date in the format ISO 8601.
- Attr str title
(optional) The title of the document.
- Attr str image
(optional) URL of a prominent image on the webpage.
- Attr list[Feed] feeds
(optional) RSS/ATOM feeds found on the webpage.
-
class
AnalysisResultsUsage
(features=None, text_characters=None, text_units=None)[source]¶ Bases:
object
API usage information for the request.
- Attr int features
(optional) Number of features used in the API call.
- Attr int text_characters
(optional) Number of text characters processed.
- Attr int text_units
(optional) Number of 10,000-character units processed.
-
class
Author
(name=None)[source]¶ Bases:
object
The author of the analyzed content.
- Attr str name
(optional) Name of the author.
-
class
CategoriesOptions
(limit=None, model=None)[source]¶ Bases:
object
Returns a five-level taxonomy of the content. The top three categories are returned. Supported languages: Arabic, English, French, German, Italian, Japanese, Korean, Portuguese, Spanish.
- Attr int limit
(optional) Maximum number of categories to return.
- Attr str model
(optional) Enter a [custom
model](https://cloud.ibm.com/docs/services/natural-language-understanding/customizing.html) ID to override the standard categories model.
-
class
CategoriesResult
(label=None, score=None)[source]¶ Bases:
object
A categorization of the analyzed text.
- Attr str label
(optional) The path to the category through the 5-level taxonomy
hierarchy. For the complete list of categories, see the [Categories hierarchy](https://cloud.ibm.com/docs/services/natural-language-understanding/categories.html#categories-hierarchy) documentation. :attr float score: (optional) Confidence score for the category classification. Higher values indicate greater confidence.
-
class
ConceptsOptions
(limit=None)[source]¶ Bases:
object
Returns high-level concepts in the content. For example, a research paper about deep learning might return the concept, “Artificial Intelligence” although the term is not mentioned. Supported languages: English, French, German, Italian, Japanese, Korean, Portuguese, Spanish.
- Attr int limit
(optional) Maximum number of concepts to return.
-
class
ConceptsResult
(text=None, relevance=None, dbpedia_resource=None)[source]¶ Bases:
object
The general concepts referenced or alluded to in the analyzed text.
- Attr str text
(optional) Name of the concept.
- Attr float relevance
(optional) Relevance score between 0 and 1. Higher scores
indicate greater relevance. :attr str dbpedia_resource: (optional) Link to the corresponding DBpedia resource.
-
class
DeleteModelResults
(deleted=None)[source]¶ Bases:
object
Delete model results.
- Attr str deleted
(optional) model_id of the deleted model.
-
class
DisambiguationResult
(name=None, dbpedia_resource=None, subtype=None)[source]¶ Bases:
object
Disambiguation information for the entity.
- Attr str name
(optional) Common entity name.
- Attr str dbpedia_resource
(optional) Link to the corresponding DBpedia resource.
- Attr list[str] subtype
(optional) Entity subtype information.
-
class
DocumentEmotionResults
(emotion=None)[source]¶ Bases:
object
Emotion results for the document as a whole.
- Attr EmotionScores emotion
(optional) Emotion results for the document as a whole.
-
class
DocumentSentimentResults
(label=None, score=None)[source]¶ Bases:
object
DocumentSentimentResults.
- Attr str label
(optional) Indicates whether the sentiment is positive, neutral, or
negative. :attr float score: (optional) Sentiment score from -1 (negative) to 1 (positive).
-
class
EmotionOptions
(document=None, targets=None)[source]¶ Bases:
object
Detects anger, disgust, fear, joy, or sadness that is conveyed in the content or by the context around target phrases specified in the targets parameter. You can analyze emotion for detected entities with entities.emotion and for keywords with keywords.emotion. Supported languages: English.
- Attr bool document
(optional) Set this to false to hide document-level emotion
results. :attr list[str] targets: (optional) Emotion results will be returned for each target string that is found in the document.
-
class
EmotionResult
(document=None, targets=None)[source]¶ Bases:
object
The detected anger, disgust, fear, joy, or sadness that is conveyed by the content. Emotion information can be returned for detected entities, keywords, or user-specified target phrases found in the text.
- Attr DocumentEmotionResults document
(optional) Emotion results for the document as
a whole. :attr list[TargetedEmotionResults] targets: (optional) Emotion results for specified targets.
-
class
EmotionScores
(anger=None, disgust=None, fear=None, joy=None, sadness=None)[source]¶ Bases:
object
EmotionScores.
- Attr float anger
(optional) Anger score from 0 to 1. A higher score means that the
text is more likely to convey anger. :attr float disgust: (optional) Disgust score from 0 to 1. A higher score means that the text is more likely to convey disgust. :attr float fear: (optional) Fear score from 0 to 1. A higher score means that the text is more likely to convey fear. :attr float joy: (optional) Joy score from 0 to 1. A higher score means that the text is more likely to convey joy. :attr float sadness: (optional) Sadness score from 0 to 1. A higher score means that the text is more likely to convey sadness.
-
class
EntitiesOptions
(limit=None, mentions=None, model=None, sentiment=None, emotion=None)[source]¶ Bases:
object
Identifies people, cities, organizations, and other entities in the content. See [Entity types and subtypes](https://cloud.ibm.com/docs/services/natural-language-understanding/entity-types.html). Supported languages: English, French, German, Italian, Japanese, Korean, Portuguese, Russian, Spanish, Swedish. Arabic, Chinese, and Dutch are supported only through custom models.
- Attr int limit
(optional) Maximum number of entities to return.
- Attr bool mentions
(optional) Set this to true to return locations of entity
mentions. :attr str model: (optional) Enter a [custom model](https://cloud.ibm.com/docs/services/natural-language-understanding/customizing.html) ID to override the standard entity detection model. :attr bool sentiment: (optional) Set this to true to return sentiment information for detected entities. :attr bool emotion: (optional) Set this to true to analyze emotion for detected keywords.
-
class
EntitiesResult
(type=None, text=None, relevance=None, mentions=None, count=None, emotion=None, sentiment=None, disambiguation=None)[source]¶ Bases:
object
The important people, places, geopolitical entities and other types of entities in your content.
- Attr str type
(optional) Entity type.
- Attr str text
(optional) The name of the entity.
- Attr float relevance
(optional) Relevance score from 0 to 1. Higher values indicate
greater relevance. :attr list[EntityMention] mentions: (optional) Entity mentions and locations. :attr int count: (optional) How many times the entity was mentioned in the text. :attr EmotionScores emotion: (optional) Emotion analysis results for the entity, enabled with the emotion option. :attr FeatureSentimentResults sentiment: (optional) Sentiment analysis results for the entity, enabled with the sentiment option. :attr DisambiguationResult disambiguation: (optional) Disambiguation information for the entity.
-
class
EntityMention
(text=None, location=None)[source]¶ Bases:
object
EntityMention.
- Attr str text
(optional) Entity mention text.
- Attr list[int] location
(optional) Character offsets indicating the beginning and
end of the mention in the analyzed text.
-
class
FeatureSentimentResults
(score=None)[source]¶ Bases:
object
FeatureSentimentResults.
- Attr float score
(optional) Sentiment score from -1 (negative) to 1 (positive).
-
class
Features
(concepts=None, emotion=None, entities=None, keywords=None, metadata=None, relations=None, semantic_roles=None, sentiment=None, categories=None, syntax=None)[source]¶ Bases:
object
Analysis features and options.
- Attr ConceptsOptions concepts
(optional) Returns high-level concepts in the content.
For example, a research paper about deep learning might return the concept, “Artificial Intelligence” although the term is not mentioned. Supported languages: English, French, German, Italian, Japanese, Korean, Portuguese, Spanish. :attr EmotionOptions emotion: (optional) Detects anger, disgust, fear, joy, or sadness that is conveyed in the content or by the context around target phrases specified in the targets parameter. You can analyze emotion for detected entities with entities.emotion and for keywords with keywords.emotion. Supported languages: English. :attr EntitiesOptions entities: (optional) Identifies people, cities, organizations, and other entities in the content. See [Entity types and subtypes](https://cloud.ibm.com/docs/services/natural-language-understanding/entity-types.html). Supported languages: English, French, German, Italian, Japanese, Korean, Portuguese, Russian, Spanish, Swedish. Arabic, Chinese, and Dutch are supported only through custom models. :attr KeywordsOptions keywords: (optional) Returns important keywords in the content. Supported languages: English, French, German, Italian, Japanese, Korean, Portuguese, Russian, Spanish, Swedish. :attr MetadataOptions metadata: (optional) Returns information from the document, including author name, title, RSS/ATOM feeds, prominent page image, and publication date. Supports URL and HTML input types only. :attr RelationsOptions relations: (optional) Recognizes when two entities are related and identifies the type of relation. For example, an awardedTo relation might connect the entities “Nobel Prize” and “Albert Einstein”. See [Relation types](https://cloud.ibm.com/docs/services/natural-language-understanding/relations.html). Supported languages: Arabic, English, German, Japanese, Korean, Spanish. Chinese, Dutch, French, Italian, and Portuguese custom models are also supported. :attr SemanticRolesOptions semantic_roles: (optional) Parses sentences into subject, action, and object form. Supported languages: English, German, Japanese, Korean, Spanish. :attr SentimentOptions sentiment: (optional) Analyzes the general sentiment of your content or the sentiment toward specific target phrases. You can analyze sentiment for detected entities with entities.sentiment and for keywords with keywords.sentiment.
Supported languages: Arabic, English, French, German, Italian, Japanese, Korean,
Portuguese, Russian, Spanish. :attr CategoriesOptions categories: (optional) Returns a five-level taxonomy of the content. The top three categories are returned. Supported languages: Arabic, English, French, German, Italian, Japanese, Korean, Portuguese, Spanish. :attr SyntaxOptions syntax: (optional) Returns tokens and sentences from the input text.
-
class
Feed
(link=None)[source]¶ Bases:
object
RSS or ATOM feed found on the webpage.
- Attr str link
(optional) URL of the RSS or ATOM feed.
-
class
KeywordsOptions
(limit=None, sentiment=None, emotion=None)[source]¶ Bases:
object
Returns important keywords in the content. Supported languages: English, French, German, Italian, Japanese, Korean, Portuguese, Russian, Spanish, Swedish.
- Attr int limit
(optional) Maximum number of keywords to return.
- Attr bool sentiment
(optional) Set this to true to return sentiment information
for detected keywords. :attr bool emotion: (optional) Set this to true to analyze emotion for detected keywords.
-
class
KeywordsResult
(count=None, relevance=None, text=None, emotion=None, sentiment=None)[source]¶ Bases:
object
The important keywords in the content, organized by relevance.
- Attr int count
(optional) Number of times the keyword appears in the analyzed text.
- Attr float relevance
(optional) Relevance score from 0 to 1. Higher values indicate
greater relevance. :attr str text: (optional) The keyword text. :attr EmotionScores emotion: (optional) Emotion analysis results for the keyword, enabled with the emotion option. :attr FeatureSentimentResults sentiment: (optional) Sentiment analysis results for the keyword, enabled with the sentiment option.
-
class
ListModelsResults
(models=None)[source]¶ Bases:
object
Custom models that are available for entities and relations.
- Attr list[Model] models
(optional) An array of available models.
-
class
MetadataOptions
[source]¶ Bases:
object
Returns information from the document, including author name, title, RSS/ATOM feeds, prominent page image, and publication date. Supports URL and HTML input types only.
-
class
Model
(status=None, model_id=None, language=None, description=None, workspace_id=None, version=None, version_description=None, created=None)[source]¶ Bases:
object
Model.
- Attr str status
(optional) When the status is available, the model is ready to
use. :attr str model_id: (optional) Unique model ID. :attr str language: (optional) ISO 639-1 code indicating the language of the model. :attr str description: (optional) Model description. :attr str workspace_id: (optional) ID of the Watson Knowledge Studio workspace that deployed this model to Natural Language Understanding. :attr str version: (optional) The model version, if it was manually provided in Watson Knowledge Studio. :attr str version_description: (optional) The description of the version, if it was manually provided in Watson Knowledge Studio. :attr datetime created: (optional) A dateTime indicating when the model was created.
-
class
RelationArgument
(entities=None, location=None, text=None)[source]¶ Bases:
object
RelationArgument.
- Attr list[RelationEntity] entities
(optional) An array of extracted entities.
- Attr list[int] location
(optional) Character offsets indicating the beginning and
end of the mention in the analyzed text. :attr str text: (optional) Text that corresponds to the argument.
-
class
RelationEntity
(text=None, type=None)[source]¶ Bases:
object
An entity that corresponds with an argument in a relation.
- Attr str text
(optional) Text that corresponds to the entity.
- Attr str type
(optional) Entity type.
-
class
RelationsOptions
(model=None)[source]¶ Bases:
object
Recognizes when two entities are related and identifies the type of relation. For example, an awardedTo relation might connect the entities “Nobel Prize” and “Albert Einstein”. See [Relation types](https://cloud.ibm.com/docs/services/natural-language-understanding/relations.html). Supported languages: Arabic, English, German, Japanese, Korean, Spanish. Chinese, Dutch, French, Italian, and Portuguese custom models are also supported.
- Attr str model
(optional) Enter a [custom
model](https://cloud.ibm.com/docs/services/natural-language-understanding/customizing.html) ID to override the default model.
-
class
RelationsResult
(score=None, sentence=None, type=None, arguments=None)[source]¶ Bases:
object
The relations between entities found in the content.
- Attr float score
(optional) Confidence score for the relation. Higher values
indicate greater confidence. :attr str sentence: (optional) The sentence that contains the relation. :attr str type: (optional) The type of the relation. :attr list[RelationArgument] arguments: (optional) Entity mentions that are involved in the relation.
-
class
SemanticRolesEntity
(type=None, text=None)[source]¶ Bases:
object
SemanticRolesEntity.
- Attr str type
(optional) Entity type.
- Attr str text
(optional) The entity text.
-
class
SemanticRolesKeyword
(text=None)[source]¶ Bases:
object
SemanticRolesKeyword.
- Attr str text
(optional) The keyword text.
-
class
SemanticRolesOptions
(limit=None, keywords=None, entities=None)[source]¶ Bases:
object
Parses sentences into subject, action, and object form. Supported languages: English, German, Japanese, Korean, Spanish.
- Attr int limit
(optional) Maximum number of semantic_roles results to return.
- Attr bool keywords
(optional) Set this to true to return keyword information for
subjects and objects. :attr bool entities: (optional) Set this to true to return entity information for subjects and objects.
-
class
SemanticRolesResult
(sentence=None, subject=None, action=None, object=None)[source]¶ Bases:
object
The object containing the actions and the objects the actions act upon.
- Attr str sentence
(optional) Sentence from the source that contains the subject,
action, and object. :attr SemanticRolesResultSubject subject: (optional) The extracted subject from the sentence. :attr SemanticRolesResultAction action: (optional) The extracted action from the sentence. :attr SemanticRolesResultObject object: (optional) The extracted object from the sentence.
-
class
SemanticRolesResultAction
(text=None, normalized=None, verb=None)[source]¶ Bases:
object
The extracted action from the sentence.
- Attr str text
(optional) Analyzed text that corresponds to the action.
- Attr str normalized
(optional) normalized version of the action.
- Attr SemanticRolesVerb verb
(optional)
-
class
SemanticRolesResultObject
(text=None, keywords=None)[source]¶ Bases:
object
The extracted object from the sentence.
- Attr str text
(optional) Object text.
- Attr list[SemanticRolesKeyword] keywords
(optional) An array of extracted keywords.
-
class
SemanticRolesResultSubject
(text=None, entities=None, keywords=None)[source]¶ Bases:
object
The extracted subject from the sentence.
- Attr str text
(optional) Text that corresponds to the subject role.
- Attr list[SemanticRolesEntity] entities
(optional) An array of extracted entities.
- Attr list[SemanticRolesKeyword] keywords
(optional) An array of extracted keywords.
-
class
SemanticRolesVerb
(text=None, tense=None)[source]¶ Bases:
object
SemanticRolesVerb.
- Attr str text
(optional) The keyword text.
- Attr str tense
(optional) Verb tense.
-
class
SentenceResult
(text=None, location=None)[source]¶ Bases:
object
SentenceResult.
- Attr str text
(optional) The sentence.
- Attr list[int] location
(optional) Character offsets indicating the beginning and
end of the sentence in the analyzed text.
-
class
SentimentOptions
(document=None, targets=None)[source]¶ Bases:
object
Analyzes the general sentiment of your content or the sentiment toward specific target phrases. You can analyze sentiment for detected entities with entities.sentiment and for keywords with keywords.sentiment.
Supported languages: Arabic, English, French, German, Italian, Japanese, Korean,
Portuguese, Russian, Spanish.
- Attr bool document
(optional) Set this to false to hide document-level sentiment
results. :attr list[str] targets: (optional) Sentiment results will be returned for each target string that is found in the document.
-
class
SentimentResult
(document=None, targets=None)[source]¶ Bases:
object
The sentiment of the content.
- Attr DocumentSentimentResults document
(optional) The document level sentiment.
- Attr list[TargetedSentimentResults] targets
(optional) The targeted sentiment to
analyze.
-
class
SyntaxOptions
(tokens=None, sentences=None)[source]¶ Bases:
object
Returns tokens and sentences from the input text.
- Attr SyntaxOptionsTokens tokens
(optional) Tokenization options.
- Attr bool sentences
(optional) Set this to true to return sentence information.
-
class
SyntaxOptionsTokens
(lemma=None, part_of_speech=None)[source]¶ Bases:
object
Tokenization options.
- Attr bool lemma
(optional) Set this to true to return the lemma for each token.
- Attr bool part_of_speech
(optional) Set this to true to return the part of speech
for each token.
-
class
SyntaxResult
(tokens=None, sentences=None)[source]¶ Bases:
object
Tokens and sentences returned from syntax analysis.
- Attr list[TokenResult] tokens
(optional)
- Attr list[SentenceResult] sentences
(optional)
-
class
TargetedEmotionResults
(text=None, emotion=None)[source]¶ Bases:
object
Emotion results for a specified target.
- Attr str text
(optional) Targeted text.
- Attr EmotionScores emotion
(optional) The emotion results for the target.
-
class
TargetedSentimentResults
(text=None, score=None)[source]¶ Bases:
object
TargetedSentimentResults.
- Attr str text
(optional) Targeted text.
- Attr float score
(optional) Sentiment score from -1 (negative) to 1 (positive).
-
class
TokenResult
(text=None, part_of_speech=None, location=None, lemma=None)[source]¶ Bases:
object
TokenResult.
- Attr str text
(optional) The token as it appears in the analyzed text.
- Attr str part_of_speech
(optional) The part of speech of the token. For descriptions
of the values, see [Universal Dependencies POS tags](https://universaldependencies.org/u/pos/). :attr list[int] location: (optional) Character offsets indicating the beginning and end of the token in the analyzed text. :attr str lemma: (optional) The [lemma](https://wikipedia.org/wiki/Lemma_%28morphology%29) of the token.