View a markdown version of this page

Real-time analisi utilizzando l'API - Amazon Comprehend

Le traduzioni sono generate tramite traduzione automatica. In caso di conflitto tra il contenuto di una traduzione e la versione originale in Inglese, quest'ultima prevarrà.

Real-time analisi utilizzando l'API

Gli esempi seguenti dimostrano come utilizzare l'API Amazon Comprehend per l'analisi in tempo reale AWS CLI, utilizzando, e AWS SDK per .NET AWS SDK per Java, e AWS SDK per Python (Boto3). Usa gli esempi per conoscere le operazioni sincrone di Amazon Comprehend e come elementi costitutivi per le tue applicazioni.

Negli esempi di .NET riportati in questa sezione viene utilizzato AWS SDK per .NET. Puoi utilizzarlo AWS Toolkit for Visual Studio per sviluppare AWS applicazioni usando.NET. Include modelli utili e AWS Explorer per la distribuzione di applicazioni e la gestione dei servizi. Per un punto di vista degli sviluppatori .NET AWS, consulta la AWS guida per gli sviluppatori .NET.

Individuazione della lingua dominante

Per determinare la lingua dominante utilizzata nel testo, utilizzate l'DetectDominantLanguageoperazione. Per rilevare la lingua dominante in un massimo di 25 documenti in un batch, utilizzate l'BatchDetectDominantLanguageoperazione. Per ulteriori informazioni, consulta Real-time API batch.

Utilizzo di AWS Command Line Interface

L'esempio seguente dimostra l'utilizzo dell'DetectDominantLanguageoperazione con. AWS CLI

L'esempio è formattato per Unix, Linux e macOS. Per Windows, sostituisci il carattere di continuazione UNIX barra rovesciata (\) al termine di ogni riga con un accento circonflesso (^).

aws comprehend detect-dominant-language \ --region region \ --text "It is raining today in Seattle."

Amazon Comprehend risponde con quanto segue:

{ "Languages": [ { "LanguageCode": "en", "Score": 0.9793661236763 } ] }

Utilizzo di AWS SDK per Java, SDK per Python o SDK per .NET

Per esempi SDK su come determinare la lingua dominante, vedi. Utilizzare DetectDominantLanguagecon un AWS SDK o CLI

Rilevamento di entità denominate

Per determinare le entità denominate in un documento, utilizzate l'DetectEntitiesoperazione. Per rilevare le entità in un massimo di 25 documenti in un batch, utilizzate l'BatchDetectEntitiesoperazione. Per ulteriori informazioni, consulta Real-time API batch.

Utilizzo di AWS Command Line Interface

L'esempio seguente dimostra l'utilizzo dell'DetectEntitiesoperazione utilizzando. AWS CLIÈ necessario specificare la lingua del testo di input.

L'esempio è formattato per Unix, Linux e macOS. Per Windows, sostituisci il carattere di continuazione UNIX barra rovesciata (\) al termine di ogni riga con un accento circonflesso (^).

aws comprehend detect-entities \ --region region \ --language-code "en" \ --text "It is raining today in Seattle."

Amazon Comprehend risponde con quanto segue:

{ "Entities": [ { "Text": "today", "Score": 0.97, "Type": "DATE", "BeginOffset": 14, "EndOffset": 19 }, { "Text": "Seattle", "Score": 0.95, "Type": "LOCATION", "BeginOffset": 23, "EndOffset": 30 } ], "LanguageCode": "en" }

Utilizzo di AWS SDK per Java, SDK per Python o SDK per .NET

Per esempi SDK su come determinare la lingua dominante, vedi. Utilizzare DetectEntitiescon un AWS SDK o CLI

Rilevamento delle frasi chiave

Per determinare le frasi sostantive chiave utilizzate nel testo, usate l'operazione. DetectKeyPhrases Per rilevare le frasi nominali chiave in un massimo di 25 documenti in un batch, usa l'operazione. BatchDetectKeyPhrases Per ulteriori informazioni, consulta Real-time API batch.

Utilizzo di AWS Command Line Interface

L'esempio seguente dimostra l'utilizzo dell'DetectKeyPhrasesoperazione con. AWS CLIÈ necessario specificare la lingua del testo di input.

L'esempio è formattato per Unix, Linux e macOS. Per Windows, sostituisci il carattere di continuazione UNIX barra rovesciata (\) al termine di ogni riga con un accento circonflesso (^).

aws comprehend detect-key-phrases \ --region region \ --language-code "en" \ --text "It is raining today in Seattle."

Amazon Comprehend risponde con quanto segue:

{ "LanguageCode": "en", "KeyPhrases": [ { "Text": "today", "Score": 0.89, "BeginOffset": 14, "EndOffset": 19 }, { "Text": "Seattle", "Score": 0.91, "BeginOffset": 23, "EndOffset": 30 } ] }

Utilizzo di AWS SDK per Java, SDK per Python o SDK per .NET

Per esempi di SDK che rilevano frasi chiave, vedi. Utilizzare DetectKeyPhrasescon un AWS SDK o CLI

Determinazione del sentimento

Amazon Comprehend fornisce le seguenti operazioni API per l'analisi del sentiment:

Utilizzo di AWS Command Line Interface

L'esempio seguente dimostra l'utilizzo dell'DetectSentimentoperazione con. AWS CLI Questo esempio specifica la lingua del testo di input.

L'esempio è formattato per Unix, Linux e macOS. Per Windows, sostituisci il carattere di continuazione UNIX barra rovesciata (\) al termine di ogni riga con un accento circonflesso (^).

aws comprehend detect-sentiment \ --region region \ --language-code "en" \ --text "It is raining today in Seattle."

Amazon Comprehend risponde con quanto segue:

{ "SentimentScore": { "Mixed": 0.014585512690246105, "Positive": 0.31592071056365967, "Neutral": 0.5985543131828308, "Negative": 0.07093945890665054 }, "Sentiment": "NEUTRAL", "LanguageCode": "en" }

Utilizzo di AWS SDK per Java, SDK per Python o SDK per .NET

Per esempi di SDK che determinano il sentiment del testo di input, vedi. Utilizzare DetectSentimentcon un AWS SDK o CLI

Real-time analisi per un sentiment mirato

Amazon Comprehend fornisce le seguenti operazioni API per un'analisi mirata del sentiment in tempo reale:

Se il testo che stai analizzando non include alcun sentimento miratoTipi di entità, l'API restituisce un array Entities vuoto.

Utilizzo di AWS Command Line Interface

L'esempio seguente dimostra l'utilizzo dell'DetectTargetedSentimentoperazione con. AWS CLI Questo esempio specifica la lingua del testo di input.

L'esempio è formattato per Unix, Linux e macOS. Per Windows, sostituisci il carattere di continuazione UNIX barra rovesciata (\) al termine di ogni riga con un accento circonflesso (^).

aws comprehend detect-targeted-sentiment \ --region region \ --language-code "en" \ --text "The burger was cooked perfectly but it was cold. The service was OK."

Amazon Comprehend risponde con quanto segue:

{ "Entities": [ { "DescriptiveMentionIndex": [ 0 ], "Mentions": [ { "BeginOffset": 4, "EndOffset": 10, "Score": 1, "GroupScore": 1, "Text": "burger", "Type": "OTHER", "MentionSentiment": { "Sentiment": "POSITIVE", "SentimentScore": { "Mixed": 0.001515, "Negative": 0.000822, "Neutral": 0.000243, "Positive": 0.99742 } } }, { "BeginOffset": 36, "EndOffset": 38, "Score": 0.999843, "GroupScore": 0.999661, "Text": "it", "Type": "OTHER", "MentionSentiment": { "Sentiment": "NEGATIVE", "SentimentScore": { "Mixed": 0, "Negative": 0.999996, "Neutral": 0.000004, "Positive": 0 } } } ] }, { "DescriptiveMentionIndex": [ 0 ], "Mentions": [ { "BeginOffset": 53, "EndOffset": 60, "Score": 1, "GroupScore": 1, "Text": "service", "Type": "ATTRIBUTE", "MentionSentiment": { "Sentiment": "NEUTRAL", "SentimentScore": { "Mixed": 0.000033, "Negative": 0.000089, "Neutral": 0.993325, "Positive": 0.006553 } } } ] } ] }

Rilevamento della sintassi

Per analizzare il testo per estrarre le singole parole e determinare le parti del discorso per ciascuna parola, usa l'operazione. DetectSyntax Per analizzare la sintassi di un massimo di 25 documenti in un batch, utilizzate l'operazione. BatchDetectSyntax Per ulteriori informazioni, consulta Real-time API batch.

Utilizzo di AWS Command Line Interface.

L'esempio seguente dimostra l'utilizzo dell'DetectSyntaxoperazione con. AWS CLI Questo esempio specifica la lingua del testo di input.

L'esempio è formattato per Unix, Linux e macOS. Per Windows, sostituisci il carattere di continuazione UNIX barra rovesciata (\) al termine di ogni riga con un accento circonflesso (^).

aws comprehend detect-syntax \ --region region \ --language-code "en" \ --text "It is raining today in Seattle."

Amazon Comprehend risponde con quanto segue:

{ "SyntaxTokens": [ { "Text": "It", "EndOffset": 2, "BeginOffset": 0, "PartOfSpeech": { "Tag": "PRON", "Score": 0.8389829397201538 }, "TokenId": 1 }, { "Text": "is", "EndOffset": 5, "BeginOffset": 3, "PartOfSpeech": { "Tag": "AUX", "Score": 0.9189288020133972 }, "TokenId": 2 }, { "Text": "raining", "EndOffset": 13, "BeginOffset": 6, "PartOfSpeech": { "Tag": "VERB", "Score": 0.9977611303329468 }, "TokenId": 3 }, { "Text": "today", "EndOffset": 19, "BeginOffset": 14, "PartOfSpeech": { "Tag": "NOUN", "Score": 0.9993606209754944 }, "TokenId": 4 }, { "Text": "in", "EndOffset": 22, "BeginOffset": 20, "PartOfSpeech": { "Tag": "ADP", "Score": 0.9999061822891235 }, "TokenId": 5 }, { "Text": "Seattle", "EndOffset": 30, "BeginOffset": 23, "PartOfSpeech": { "Tag": "PROPN", "Score": 0.9940338730812073 }, "TokenId": 6 }, { "Text": ".", "EndOffset": 31, "BeginOffset": 30, "PartOfSpeech": { "Tag": "PUNCT", "Score": 0.9999997615814209 }, "TokenId": 7 } ] }

Utilizzo di AWS SDK per Java, SDK per Python o SDK per .NET

Per esempi SDK che rilevano la sintassi del testo di input, vedi. Utilizzare DetectSyntaxcon un AWS SDK o CLI

Real-time API batch

Per inviare batch di un massimo di 25 documenti, puoi utilizzare le operazioni batch in tempo reale di Amazon Comprehend. Chiamare un'operazione batch è identico a chiamare le API per ogni singolo documento nella richiesta. L'utilizzo delle API batch può migliorare le prestazioni delle applicazioni. Per ulteriori informazioni, consulta Elaborazione sincrona di più documenti.

Elaborazione in batch con AWS CLI

Questi esempi mostrano come utilizzare le operazioni API batch utilizzando AWS Command Line Interface. Tutte le operazioni tranne BatchDetectDominantLanguage l'utilizzo del seguente file JSON chiamato process.json come input. Per tale operazione l'LanguageCodeentità non è inclusa.

Il terzo documento nel file JSON ("$$$$$$$$") causerà un errore durante l'elaborazione in batch. È incluso in modo che le operazioni includano un BatchItemError nella risposta.

{ "LanguageCode": "en", "TextList": [ "I have been living in Seattle for almost 4 years", "It is raining today in Seattle", "$$$$$$$$" ] }

Gli esempi sono formattati per Unix, Linux e macOS. Per Windows, sostituisci il carattere di continuazione UNIX barra rovesciata (\) al termine di ogni riga con un accento circonflesso (^).

Rileva la lingua dominante utilizzando un batch (AWS CLI)

L'BatchDetectDominantLanguageoperazione determina la lingua dominante di ogni documento in un batch. Per un elenco delle lingue che Amazon Comprehend è in grado di rilevare, consultaLingua dominante. Il AWS CLI comando seguente richiama l'BatchDetectDominantLanguageoperazione.

aws comprehend batch-detect-dominant-language \ --endpoint endpoint \ --region region \ --cli-input-json file://path to input file/process.json

Di seguito è riportata la risposta dell'BatchDetectDominantLanguageoperazione:

{ "ResultList": [ { "Index": 0, "Languages":[ { "LanguageCode":"en", "Score": 0.99 } ] }, { "Index": 1 "Languages":[ { "LanguageCode":"en", "Score": 0.82 } ] } ], "ErrorList": [ { "Index": 2, "ErrorCode": "InternalServerException", "ErrorMessage": "Unexpected Server Error. Please try again." } ] }

Rileva le entità utilizzando un batch (AWS CLI)

Usa l'BatchDetectEntitiesoperazione per trovare le entità presenti in un batch di documenti. Per ulteriori informazioni sulle entità, consulta Entità. Il AWS CLI comando seguente chiama l'BatchDetectEntitiesoperazione.

aws comprehend batch-detect-entities \ --endpoint endpoint \ --region region \ --cli-input-json file://path to input file/process.json

Rileva le frasi chiave utilizzando un batch (AWS CLI)

L'BatchDetectKeyPhrasesoperazione restituisce le frasi nominali chiave in un batch di documenti. Il AWS CLI comando seguente chiama l'operazione. BatchDetectKeyNounPhrases

aws comprehend batch-detect-key-phrases --endpoint endpoint --region region --cli-input-json file://path to input file/process.json

Rileva il sentiment utilizzando un batch (AWS CLI)

Rileva il sentiment complessivo di un gruppo di documenti utilizzando l'BatchDetectSentimentoperazione. Il AWS CLI comando seguente richiama l'BatchDetectSentimentoperazione.

aws comprehend batch-detect-sentiment \ --endpoint endpoint \ --region region \ --cli-input-json file://path to input file/process.json

Elaborazione in batch con AWS SDK per .NET

Il seguente programma di esempio mostra come utilizzare l'BatchDetectEntitiesoperazione con SDK per .NET. La risposta del server contiene un BatchDetectEntitiesItemResult oggetto per ogni documento che è stato elaborato correttamente. Se si verifica un errore nell'elaborazione di un documento, verrà visualizzato un record nell'elenco degli errori della risposta. L'esempio recupera tutti i documenti con un errore e li invia nuovamente.

L'esempio.NET in questa sezione utilizza. AWS SDK per .NET È possibile utilizzare il AWS Toolkit for Visual Studio per sviluppare AWS applicazioni utilizzando .NET. Include modelli utili e AWS Explorer per la distribuzione di applicazioni e la gestione dei servizi. Per un punto di vista degli sviluppatori .NET AWS, consulta la AWS guida per gli sviluppatori .NET.

using System; using System.Collections.Generic; using Amazon.Comprehend; using Amazon.Comprehend.Model; namespace Comprehend { class Program { // Helper method for printing properties static private void PrintEntity(Entity entity) { Console.WriteLine(" Text: {0}, Type: {1}, Score: {2}, BeginOffset: {3} EndOffset: {4}", entity.Text, entity.Type, entity.Score, entity.BeginOffset, entity.EndOffset); } static void Main(string[] args) { AmazonComprehendClient comprehendClient = new AmazonComprehendClient(Amazon.RegionEndpoint.USWest2); List<String> textList = new List<String>() { { "I love Seattle" }, { "Today is Sunday" }, { "Tomorrow is Monday" }, { "I love Seattle" } }; // Call detectEntities API Console.WriteLine("Calling BatchDetectEntities"); BatchDetectEntitiesRequest batchDetectEntitiesRequest = new BatchDetectEntitiesRequest() { TextList = textList, LanguageCode = "en" }; BatchDetectEntitiesResponse batchDetectEntitiesResponse = comprehendClient.BatchDetectEntities(batchDetectEntitiesRequest); foreach (BatchDetectEntitiesItemResult item in batchDetectEntitiesResponse.ResultList) { Console.WriteLine("Entities in {0}:", textList[item.Index]); foreach (Entity entity in item.Entities) PrintEntity(entity); } // check if we need to retry failed requests if (batchDetectEntitiesResponse.ErrorList.Count != 0) { Console.WriteLine("Retrying Failed Requests"); List<String> textToRetry = new List<String>(); foreach(BatchItemError errorItem in batchDetectEntitiesResponse.ErrorList) textToRetry.Add(textList[errorItem.Index]); batchDetectEntitiesRequest = new BatchDetectEntitiesRequest() { TextList = textToRetry, LanguageCode = "en" }; batchDetectEntitiesResponse = comprehendClient.BatchDetectEntities(batchDetectEntitiesRequest); foreach(BatchDetectEntitiesItemResult item in batchDetectEntitiesResponse.ResultList) { Console.WriteLine("Entities in {0}:", textList[item.Index]); foreach (Entity entity in item.Entities) PrintEntity(entity); } } Console.WriteLine("End of DetectEntities"); } } }