watson_developer_cloud.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](/docs/services/natural-language-understanding/customizing.html) with Watson Knowledge Studio to detect custom entities and relations 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: watson_developer_cloud.watson_service.WatsonService

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, HTML, or a public webpage.

Analyzes text, HTML, or a public webpage with one or more text analysis features, including categories, concepts, emotion, entities, keywords, metadata, relations, semantic roles, and sentiment.

Parameters:
  • features (Features) – Specific features to analyze the document for.
  • text (str) – The plain text to analyze. One of the text, html, or url

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 web page to analyze. One of the text, html, or url parameters is required. :param bool clean: Remove website elements, such as links, ads, etc. :param str xpath: An [XPath query](https://www.w3.org/TR/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://www.bluemix.net/docs/services/natural-language-understanding/language-support.html) 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

delete_model(model_id, **kwargs)[source]

Delete model.

Deletes a custom model.

Parameters:
  • model_id (str) – model_id of the model to delete.
  • headers (dict) – A dict containing the request headers
Returns:

A DetailedResponse containing the result, headers and HTTP status code.

Return type:

DetailedResponse

list_models(**kwargs)[source]

List models.

Lists available models for Relations and Entities features, including Watson Knowledge Studio custom models that you have created and linked 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)[source]

Bases: object

Results of the analysis, organized by 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 that was used to retrieve HTML content.
Attr Usage usage:
 (optional) API usage information for the request.
Attr list[ConceptsResult] concepts:
 (optional) The general concepts referenced or

alluded to in the specified content. :attr list[EntitiesResult] entities: (optional) The important entities in the specified content. :attr list[KeywordsResult] keywords: (optional) The important keywords in content organized by relevance. :attr list[CategoriesResult] categories: (optional) The hierarchical 5-level taxonomy the content is categorized into. :attr EmotionResult emotion: (optional) The anger, disgust, fear, joy, or sadness conveyed by the content. :attr MetadataResult metadata: (optional) The metadata holds author information, publication date and the title of the text/HTML content. :attr list[RelationsResult] relations: (optional) The relationships between entities in the content. :attr list[SemanticRolesResult] semantic_roles: (optional) The subjects of actions and the objects the actions act upon. :attr SentimentResult sentiment: (optional) The sentiment of the content.

class Author(name=None)[source]

Bases: object

The author of the analyzed content.

Attr str name:(optional) Name of the author.
class CategoriesOptions(**kwargs)[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.

class CategoriesResult(label=None, score=None)[source]

Bases: object

The hierarchical 5-level taxonomy the content is categorized into.

Attr str label:(optional) The path to the category through the taxonomy hierarchy.
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, 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 specified content.

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

An object containing the emotion results of a document.

Attr EmotionScores emotion:
 (optional) An object containing the emotion results for

the document.

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) The returned emotion results across

the document. :attr list[TargetedEmotionResults] targets: (optional) The returned emotion results per specified target.

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](/docs/services/natural-language-understanding/entity-types.html). Supported languages: English, French, German, Italian, Japanese, Korean, Portuguese, Russian, Spanish, Swedish. Arabic, Chinese, and Dutch custom models are also supported.

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://www.bluemix.net/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)[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, 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](/docs/services/natural-language-understanding/entity-types.html). Supported languages: English, French, German, Italian, Japanese, Korean, Portuguese, Russian, Spanish, Swedish. Arabic, Chinese, and Dutch custom models are also supported. :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](/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.

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(relevance=None, text=None, emotion=None, sentiment=None)[source]

Bases: object

The most important keywords in the content, organized by relevance.

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

Models available for Relations and Entities features.

Attr list[Model] models:
 (optional) An array of available models.
class MetadataOptions(**kwargs)[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 MetadataResult(authors=None, publication_date=None, title=None, image=None, feeds=None)[source]

Bases: object

The Authors, Publication Date, and Title of the document. Supports URL and HTML input types.

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 Model(status=None, model_id=None, language=None, description=None)[source]

Bases: object

Model.

Attr str status:
 (optional) Shows as available if the model is ready for 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.
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](/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](/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) The extracted relation objects from the text.

class SemanticRolesAction(text=None, normalized=None, verb=None)[source]

Bases: object

SemanticRolesAction.

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 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 SemanticRolesObject(text=None, keywords=None)[source]

Bases: object

SemanticRolesObject.

Attr str text:(optional) Object text.
Attr list[SemanticRolesKeyword] keywords:
 (optional) An array of extracted keywords.
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 SemanticRolesSubject subject: (optional) The extracted subject from the sentence. :attr SemanticRolesAction action: (optional) The extracted action from the sentence. :attr SemanticRolesObject object: (optional) The extracted object from the sentence.

class SemanticRolesSubject(text=None, entities=None, keywords=None)[source]

Bases: object

SemanticRolesSubject.

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 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 TargetedEmotionResults(text=None, emotion=None)[source]

Bases: object

An object containing the emotion results for the target.

Attr str text:(optional) Targeted text.
Attr EmotionScores emotion:
 (optional) An object containing 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 Usage(features=None, text_characters=None, text_units=None)[source]

Bases: object

Usage information.

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.