Natural Language Response Classes#

Responses#

Response types from the Natural Language API.

class google.cloud.language.api_responses.EntityResponse(entities, language)[source]#

Bases: object

Object representation of entity responses.

A representation of a response sent back from the analyzeEntites request to the Google Natural language API.

Parameters:
  • entities (list) – A list of Entity objects.
  • language (str) – The language used for analysis.
classmethod from_api_repr(payload)[source]#

Return an entity response from a JSON representation.

Parameters:payload (dict) – A dictionary representing the response.
Return type:Entity
Returns:An Entity object.
class google.cloud.language.api_responses.SentimentResponse(sentiment, language, sentences)[source]#

Bases: object

Object representation of sentiment responses.

A representation of a response to an analyzeSentiment request to the Google Natural Language API.

Parameters:
  • sentiment (Sentiment) – A Sentiment object.
  • language (str) – The language used for analyzing sentiment.
  • sentences (list) – A list of Sentence objects.
classmethod from_api_repr(payload)[source]#

Return an sentiment response from a JSON representation.

Parameters:payload (dict) – A dictionary representing the response.
Return type:~.language.sentiment.Sentiment
Returns:A Sentiment object.
class google.cloud.language.api_responses.SyntaxResponse(tokens, language, sentences)[source]#

Bases: object

Object representation of syntax responses.

A representation of a response to an analyzeSyntax request to the Google Natural Language API.

Parameters:
  • tokens (list) – A list of Token objects.
  • language (str) – The language used for analyzing sentiment.
  • sentences (list) – A list of Sentence objects.
classmethod from_api_repr(payload)[source]#

Return an syntax response from a JSON representation.

Parameters:payload (dict) – A dictionary representing the response.
Return type:~.language.syntax.Syntax
Returns:A Syntax object.

Sentences#

Representation of Sentence objects.

class google.cloud.language.sentence.Sentence(content, begin, sentiment=None)[source]#

Bases: object

A Google Cloud Natural Language API sentence object.

See Sentence message.

Parameters:
  • content (str) – The text that the sentence is composed of.
  • begin (int) – The beginning offset of the sentence in the original document according to the encoding type specified in the API request.
  • sentiment (Sentiment) – (Optional) For calls to annotate_text() where include_sentiment is set to true, this field will contain the sentiment for the sentence.
classmethod from_api_repr(payload)[source]#

Convert a sentence from the JSON API into a Sentence.

Parameters:payload (The value from the backend.) – dict
Return type:Sentence
Returns:The sentence parsed from the API representation.

Entity#

Definition for Google Cloud Natural Language API entities.

An entity is used to describe a proper name extracted from text.

class google.cloud.language.entity.Entity(name, entity_type, metadata, salience, mentions, sentiment)[source]#

Bases: object

A Google Cloud Natural Language API entity.

Represents a phrase in text that is a known entity, such as a person, an organization, or location. The API associates information, such as salience and mentions, with entities.

See Entity message.

Parameters:
  • name (str) – The name / phrase identified as the entity.
  • entity_type (str) – The type of the entity. See EntityType enum.
  • metadata (dict) – The metadata associated with the entity. Wikipedia URLs and Knowledge Graph MIDs are provided, if available. The associated keys are “wikipedia_url” and “mid”, respectively.
  • salience (float) – The prominence of the entity / phrase within the text containing it.
  • mentions (list) – List of strings that mention the entity.
Params sentiment:
 

The sentiment; sent only on analyze_entity_sentiment calls.

classmethod from_api_repr(payload)[source]#

Convert an Entity from the JSON API into an Entity.

Parameters:payload (The value from the backend.) – dict
Return type:Entity
Returns:The entity parsed from the API representation.
class google.cloud.language.entity.EntityType[source]#

Bases: object

List of possible entity types.

CONSUMER_GOOD = 'CONSUMER_GOOD'#

Consumer good entity type.

EVENT = 'EVENT'#

Event entity type.

LOCATION = 'LOCATION'#

Location entity type.

ORGANIZATION = 'ORGANIZATION'#

Organization entity type.

OTHER = 'OTHER'#

Other entity type (i.e. known but not classified).

PERSON = 'PERSON'#

Person entity type.

UNKNOWN = 'UNKNOWN'#

Unknown entity type.

WORK_OF_ART = 'WORK_OF_ART'#

Work of art entity type.

class google.cloud.language.entity.Mention(text, mention_type)[source]#

Bases: object

A Google Cloud Natural Language API mention.

Represents a mention for an entity in the text. Currently, proper noun mentions are supported.

classmethod from_api_repr(payload)[source]#

Convert a Mention from the JSON API into an Mention.

Parameters:payload (The value from the backend.) – dict
Return type:Mention
Returns:The mention parsed from the API representation.
class google.cloud.language.entity.MentionType[source]#

Bases: object

List of possible mention types.

COMMON = 'COMMON'#

Common noun (or noun compound)

PROPER = 'PROPER'#

Proper name

TYPE_UNKNOWN = 'TYPE_UNKNOWN'#

Unknown mention type

class google.cloud.language.entity.TextSpan(content, begin_offset)[source]#

Bases: object

A span of text from Google Cloud Natural Language API.

Represents a word or phrase of text, as well as its offset from the original document.

classmethod from_api_repr(payload)[source]#

Convert a TextSpan from the JSON API into an TextSpan.

Parameters:payload (The value from the backend.) – dict
Return type:TextSpan
Returns:The text span parsed from the API representation.

Sentiment#

Definition for Google Cloud Natural Language API sentiment.

Sentiment is the response to an analyzeSentiment request.

class google.cloud.language.sentiment.Sentiment(score, magnitude)[source]#

Bases: object

A Google Cloud Natural Language API sentiment object.

See Sentiment message and Sentiment basics.

Parameters:
  • score (float) – Score of the sentiment in the [-1.0, 1.0] range. Larger numbers represent more positive sentiments.
  • magnitude (float) – A non-negative number in the [0, +inf) range, which represents the absolute magnitude of sentiment regardless of score (positive or negative).
classmethod from_api_repr(payload)[source]#

Convert a Sentiment from the JSON API into a Sentiment.

Parameters:payload (The value from the backend.) – dict
Return type:Sentiment
Returns:The sentiment parsed from the API representation.

Syntax#

Google Cloud Natural Language API helpers for tokenized text.

The annotateText method, when used with the “syntax” feature, breaks a document down into tokens and sentences.

class google.cloud.language.syntax.PartOfSpeech[source]#

Bases: object

Part of speech of a Token.

ADJECTIVE = 'ADJ'#

Part of speech: Adjective.

ADPOSITION = 'ADP'#

Adposition (preposition and postposition).

ADVERB = 'ADV'#

Adverb.

AFFIX = 'AFFIX'#

Affix.

CARDINAL_NUMBER = 'NUM'#

Cardinal number.

CONJUNCTION = 'CONJ'#

Conjunction.

DETERMINER = 'DET'#

Determiner.

NOUN = 'NOUN'#

Noun (common and proper).

OTHER = 'X'#

Other: foreign words, typos, abbreviations.

PARTICIPLE = 'PRT'#

Particle or other function word.

PRONOUN = 'PRON'#

Pronoun.

PUNCTUATION = 'PUNCT'#

Punctuation.

UNKNOWN = 'UNKNOWN'#

Unknown part of speech.

VERB = 'VERB'#

Verb (all tenses and modes).

classmethod reverse(tag)[source]#

Reverses the API’s enum name for the one on this class.

For example:

>>> PartOfSpeech.OTHER
'X'
>>> PartOfSpeech.reverse('X')
'OTHER'
Return type:str
Returns:The attribute name corresponding to the API part of speech enum.
class google.cloud.language.syntax.Token(text_content, text_begin, part_of_speech, edge_index, edge_label, lemma)[source]#

Bases: object

A Google Cloud Natural Language API token object.

See Token message.

Parameters:
  • text_content (str) – The text that the token is composed of.
  • text_begin (int) – The beginning offset of the content in the original document according to the encoding type specified in the API request.
  • part_of_speech (str) – The part of speech of the token. See PartOfSpeech for possible values.
  • edge_index (int) – The head of this token in the dependency tree. This is the index of the token which has an arc going to this token. The index is the position of the token in the array of tokens returned by the API method. If this token is a root token, then the edge_index is its own index.
  • edge_label (str) – See Label enum.
  • lemma (str) – The Lemma of the token.
classmethod from_api_repr(payload)[source]#

Convert a token from the JSON API into a Token.

Parameters:payload (The value from the backend.) – dict
Return type:Token
Returns:The token parsed from the API representation.