Skip to content

Latest commit

 

History

History
213 lines (172 loc) · 5.82 KB

ai.text.sentiment.md

File metadata and controls

213 lines (172 loc) · 5.82 KB

Sentiment analysis features

This is an intent to analyze the sentiment of the provided text.

More on that in the documentation

Basic usage

To analyze a text for sentiments, specify the text, the language of the text, and the desired provider as in the following example:

client.ai.text.sentiment
    .fulfill({
        text: 'We love this place',
        lang: 'en',
        provider: 'ai.text.sentiment.meaningcloud.sentiment_analysis_api.2-1',
    })
    .then(data => {
        console.log('Sentiment analysis results:\n', data, '\n\n')
    })

The response contains the processed text and a service information:

{
    "results": [
        {
            "sentiment_label": "positive",
            "sentiment_score": 1.0,
            "sentiment_confidence": 1.0,
            "sentiment_subjectivity": "subjective",
            "agreement": true,
            "irony": false
        }
    ],
    "meta": {},
    "service": {
        "provider": {
            "id": "ai.text.sentiment.meaningcloud.sentiment_analysis_api.2-1",
            "name": "MeaningCloud Sentiment Analysis API"
        }
    }
}

If the provider doesn't have capabilities (e.g. does not support a specific language) to process request, 413 error will be returned:

{
    "error": {
        "code": 413,
        "message": "Provider ai.text.sentiment.meaningcloud.sentiment_analysis_api.2-1 constraint(s) violated: lang (Source language)"
    }
}

Bulk mode

We provide a bulk fulfillment mode to process an array of texts at once. The mode is activated by sending an array of strings to the text parameter. The bulk mode is supported for some of the providers (see Provider supporting bulk sentiment analysis section).

client.ai.text.sentiment
    .fulfill({
        text: [
            'We love this shop!',
            'The quality is not as good as it should'
        ],
        lang: 'en',
        provider: 'ai.text.sentiment.ibm.natural_language_understanding',
    })
    .then(console.log)

The response contains the processed texts and a service information:

{
    "results": [
        {
            "sentiment_label": "positive",
            "sentiment_score": 0.931392
        },
        {
            "sentiment_label": "neutral",
            "sentiment_score": 0.535453
        }
    ],
    "meta": {},
    "service": {
        "provider": {
            "id": "ai.text.sentiment.ibm.natural_language_understanding",
            "name": "IBM Watson Natural Language Understanding"
        }
    }
}

Explore sentiment analysis providers

List all providers:

client.ai.text.sentiment
    .providers()
    .then(data => data.forEach(p => console.info(p.name)))

Response structure for provider-related requests

In all cases a response object is a list of objects. Each object in that list describes one provider. The structure of the description is following:

{
    "id": "provider-id",
    "name": "Provider Name",
    "logo": "https://url/to/logo.png",
    "auth": {
        "key": "YOUR_KEY"
    },
    "billing": true,
    "languages": {
        "lang": ["list", "of", "lang", "codes"]
    },
    "lang_detect": false,
    "bulk": false
}

lang - is a list of language codes for which sentiment analysis in both directions is available.

Filtering providers by capabilities

The list of providers may be further constrained by adding desired parameter values.

Providers with language detect feature

client.ai.text.sentiment
    .providers({ lang_detect: true })
    .then(data => data.forEach(p => console.info(p.name)))

Provider supporting bulk sentiment analysis

client.ai.text.sentiment
    .providers({ bulk: true })
    .then(data => data.forEach(p => console.info(p.name)))

Providers able to analyze sentiment in Afrikaans

See more on (language codes -- see ISO 639-1 Code)

client.ai.text.sentiment
    .providers({ lang: 'af' })
    .then(data => data.forEach(p => console.info(p.name)))

Combine filters

Retrieve providers able to analyze sentiment in Italian, for an array of segments at once, detecting source language:

client.ai.text.sentiment
    .providers({ lang: 'it', bulk: true, lang_detect: true })
    .then(data => data.forEach(p => console.info(p.name)))

Getting information about a provider

To get information about a provider pass provider id to client.ai.text.sentiment.provider.

client.ai.text.sentiment
    .provider('ai.text.sentiment.microsoft.text_analytics_api.2-0')
    .then(console.log)

The response contains a list of the metadata fields and values available for the provider:

{
    "id": "ai.text.sentiment.microsoft.text_analytics_api.2-0",
    "name": "Microsoft",
    "logo": "https://inten.to/static/img/api/mcs_translate.png",
    "billing": true,
    "languages": {
        "lang": [
            "en"
        ]
    },
    "lang_detect": false,
    "bulk": false
}