Google NL API module logo

This module provides functionality to connect to Google's Natural Language API and run analysis on text, including sentiment, entities, syntax, entity sentiment and content classification.

Requirements

  • You must have a valid Google service account json key file with Natural Language enabled for the project.
  • The encryption module
    is required, and will be automatically installed when using composer.

Installation

Install this module via composer by running the following command:

composer require drupal/google_nl_api

Configuration

Configure Google NL API in Administration » Configuration » Google NL » Google NL API Settings
or by going directly to /admin/config/google_nl/google_nl_api_settings:

Basic usage

  • Analyze Sentiment

    Sentiment Analysis inspects the given text and identifies the prevailing emotional opinion within the text, especially to determine a writer's attitude as positive, negative, or neutral. Sentiment analysis is performed through the analyzeSentiment method.



    Example call:

    \Drupal::service('google_nl_api')->analyzeSentiment('Do you know the way to San Jose?');


    Example response:

    Array
    (
        [magnitude] => 0.1
        [score] => 0.1
    )
    

  • Analyze Entities

    Entity Analysis inspects the given text for known entities (proper nouns such as public figures, landmarks, etc.), and returns information about those entities. Entity analysis is performed with the analyzeEntities method.



    Example call:

    \Drupal::service('google_nl_api')->analyzeEntities('Do you know the way to San Jose?');


    Example response:

    Array
    (
        [0] => Array
            (
                [name] => way
                [type] => OTHER
                [metadata] => Array
                    (
                    )
    
                [salience] => 0.6935411
                [mentions] => Array
                    (
                        [0] => Array
                            (
                                [text] => Array
                                    (
                                        [content] => way
                                        [beginOffset] => 16
                                    )
    
                                [type] => COMMON
                            )
    
                    )
    
            )
    
        [1] => ...
    
    )
    
  • Analyze Syntax

    While most Natural Language API methods analyze what a given text is about, the analyzeSyntax method inspects the structure of the language itself. Syntactic Analysis breaks up the given text into a series of sentences and tokens (generally, words) and provides linguistic information about those tokens.



    Example call:

    \Drupal::service('google_nl_api')->analyzeSyntax('Do you know the way to San Jose?');


    Example response:

    Array
    (
        [0] => Array
            (
                [text] => Array
                    (
                        [content] => Do
                        [beginOffset] => 0
                    )
    
                [partOfSpeech] => Array
                    (
                        [tag] => VERB
                        [aspect] => ASPECT_UNKNOWN
                        [case] => CASE_UNKNOWN
                        [form] => FORM_UNKNOWN
                        [gender] => GENDER_UNKNOWN
                        [mood] => INDICATIVE
                        [number] => NUMBER_UNKNOWN
                        [person] => PERSON_UNKNOWN
                        [proper] => PROPER_UNKNOWN
                        [reciprocity] => RECIPROCITY_UNKNOWN
                        [tense] => PRESENT
                        [voice] => VOICE_UNKNOWN
                    )
    
                [dependencyEdge] => Array
                    (
                        [headTokenIndex] => 2
                        [label] => AUX
                    )
    
                [lemma] => Do
            )
    
        [1] => ...
    
    )
    

  • Analyze Entity Sentiment

    Entity Sentiment Analysis combines both entity analysis and sentiment analysis and attempts to determine the sentiment (positive or negative) expressed about entities within the text. Entity sentiment is represented by numerical score and magnitude values and is determined for each mention of an entity. Those scores are then aggregated into an overall sentiment score and magnitude for an entity.

    NOTE: This call requires at least 20 words to do analysis.


    Example call:

    \Drupal::service('google_nl_api')->analyzeEntitySentiment('Leverage agile frameworks to provide a robust synopsis for high level overviews. Iterative approaches to corporate strategy foster collaborative thinking to further the overall value proposition. Organically grow the holistic world view of disruptive innovation via workplace diversity and empowerment.');


    Example response:

    Array
    (
        [0] => Array
            (
                [name] => frameworks
                [type] => OTHER
                [metadata] => Array
                    (
                    )
    
                [salience] => 0.41024795
                [mentions] => Array
                    (
                        [0] => Array
                            (
                                [text] => Array
                                    (
                                        [content] => frameworks
                                        [beginOffset] => 15
                                    )
    
                                [type] => COMMON
                                [sentiment] => Array
                                    (
                                        [magnitude] => 0
                                        [score] => 0
                                    )
    
                            )
    
                    )
    
                [sentiment] => Array
                    (
                        [magnitude] => 0
                        [score] => 0
                    )
    
            )
    
        [1] => ...
    
    )
    

  • Classify Content

    Content Classification analyzes a document and returns a list of content categories that apply to the text found in the document.

    A complete list of possible categories are found here.

    Example call:

    \Drupal::service('google_nl_api')->classifyContent('Do you know the way to San Jose? I sure don\t and it\s really becoming a problem for my personal life.');


    Example response:

    Array
    (
        [0] => Array
            (
                [name] => /Business & Industrial/Business Operations/Management
                [confidence] => 0.66
            )
    
        [1] => Array
            (
                [name] => /Computers & Electronics/Software/Business & Productivity Software
                [confidence] => 0.52
            )
    
    )
    

Related Modules

Credits

Module development is sponsored by Ashday Interactive Systems.

Supporting organizations: 

Project information

Releases