Há mais AWS SDK exemplos disponíveis no GitHub repositório AWS Doc SDK Examples
As traduções são geradas por tradução automática. Em caso de conflito entre o conteúdo da tradução e da versão original em inglês, a versão em inglês prevalecerá.
Use DetectEntities
com um AWS SDK ou CLI
Os exemplos de código a seguir mostram como usar o DetectEntities
.
Exemplos de ações são trechos de código de programas maiores e devem ser executados em contexto. É possível ver essa ação no contexto no seguinte exemplo de código:
- .NET
-
- AWS SDK for .NET
-
nota
Tem mais sobre GitHub. Encontre o exemplo completo e saiba como configurar e executar no Repositório de exemplos de código da AWS
. using System; using System.Threading.Tasks; using Amazon.Comprehend; using Amazon.Comprehend.Model; /// <summary> /// This example shows how to use the AmazonComprehend service detect any /// entities in submitted text. /// </summary> public static class DetectEntities { /// <summary> /// The main method calls the DetectEntitiesAsync method to find any /// entities in the sample code. /// </summary> public static async Task Main() { string text = "It is raining today in Seattle"; var comprehendClient = new AmazonComprehendClient(); Console.WriteLine("Calling DetectEntities\n"); var detectEntitiesRequest = new DetectEntitiesRequest() { Text = text, LanguageCode = "en", }; var detectEntitiesResponse = await comprehendClient.DetectEntitiesAsync(detectEntitiesRequest); foreach (var e in detectEntitiesResponse.Entities) { Console.WriteLine($"Text: {e.Text}, Type: {e.Type}, Score: {e.Score}, BeginOffset: {e.BeginOffset}, EndOffset: {e.EndOffset}"); } Console.WriteLine("Done"); } }
-
Para API obter detalhes, consulte DetectEntitiesem AWS SDK for .NET APIReferência.
-
- CLI
-
- AWS CLI
-
Para detectar entidades nomeadas no texto de entrada
O exemplo de
detect-entities
a seguir analisa o texto de entrada e retorna as entidades nomeadas. A pontuação de confiança do modelo pré-treinado também é gerada para cada previsão.aws compreh
en
d detect-entities \ --language-code en \ --text"Hello Zhang Wei, I am John. Your AnyCompany Financial Services, LLC credit card \ account 1111-XXXX-1111-XXXX has a minimum payment of $24.53 that is due by July 31st. Based on your autopay settings, \ we will withdraw your payment on the due date from your bank account number XXXXXX1111 with the routing number XXXXX0000. \ Customer feedback for Sunshine Spa, 123 Main St, Anywhere. Send comments to Alice at AnySpa@example.com."
Saída:
{ "Entities": [ { "Score": 0.9994556307792664, "Type": "PERSON", "Text": "Zhang Wei", "BeginOffset": 6, "EndOffset": 15 }, { "Score": 0.9981022477149963, "Type": "PERSON", "Text": "John", "BeginOffset": 22, "EndOffset": 26 }, { "Score": 0.9986887574195862, "Type": "ORGANIZATION", "Text": "AnyCompany Financial Services, LLC", "BeginOffset": 33, "EndOffset": 67 }, { "Score": 0.9959119558334351, "Type": "OTHER", "Text": "1111-XXXX-1111-XXXX", "BeginOffset": 88, "EndOffset": 107 }, { "Score": 0.9708039164543152, "Type": "QUANTITY", "Text": ".53", "BeginOffset": 133, "EndOffset": 136 }, { "Score": 0.9987268447875977, "Type": "DATE", "Text": "July 31st", "BeginOffset": 152, "EndOffset": 161 }, { "Score": 0.9858865737915039, "Type": "OTHER", "Text": "XXXXXX1111", "BeginOffset": 271, "EndOffset": 281 }, { "Score": 0.9700471758842468, "Type": "OTHER", "Text": "XXXXX0000", "BeginOffset": 306, "EndOffset": 315 }, { "Score": 0.9591118693351746, "Type": "ORGANIZATION", "Text": "Sunshine Spa", "BeginOffset": 340, "EndOffset": 352 }, { "Score": 0.9797496795654297, "Type": "LOCATION", "Text": "123 Main St", "BeginOffset": 354, "EndOffset": 365 }, { "Score": 0.994929313659668, "Type": "PERSON", "Text": "Alice", "BeginOffset": 394, "EndOffset": 399 }, { "Score": 0.9949769377708435, "Type": "OTHER", "Text": "AnySpa@example.com", "BeginOffset": 403, "EndOffset": 418 } ] }
Para obter mais informações, consulte Entidades no Guia do desenvolvedor do Amazon Comprehend.
-
Para API obter detalhes, consulte DetectEntities
na Referência de AWS CLI Comandos.
-
- Java
-
- SDKpara Java 2.x
-
nota
Tem mais sobre GitHub. Encontre o exemplo completo e saiba como configurar e executar no Repositório de exemplos de código da AWS
. import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.comprehend.ComprehendClient; import software.amazon.awssdk.services.comprehend.model.DetectEntitiesRequest; import software.amazon.awssdk.services.comprehend.model.DetectEntitiesResponse; import software.amazon.awssdk.services.comprehend.model.Entity; import software.amazon.awssdk.services.comprehend.model.ComprehendException; import java.util.List; /** * Before running this Java V2 code example, set up your development * environment, including your credentials. * * For more information, see the following documentation topic: * * https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html */ public class DetectEntities { public static void main(String[] args) { String text = "Amazon.com, Inc. is located in Seattle, WA and was founded July 5th, 1994 by Jeff Bezos, allowing customers to buy everything from books to blenders. Seattle is north of Portland and south of Vancouver, BC. Other notable Seattle - based companies are Starbucks and Boeing."; Region region = Region.US_EAST_1; ComprehendClient comClient = ComprehendClient.builder() .region(region) .build(); System.out.println("Calling DetectEntities"); detectAllEntities(comClient, text); comClient.close(); } public static void detectAllEntities(ComprehendClient comClient, String text) { try { DetectEntitiesRequest detectEntitiesRequest = DetectEntitiesRequest.builder() .text(text) .languageCode("en") .build(); DetectEntitiesResponse detectEntitiesResult = comClient.detectEntities(detectEntitiesRequest); List<Entity> entList = detectEntitiesResult.entities(); for (Entity entity : entList) { System.out.println("Entity text is " + entity.text()); } } catch (ComprehendException e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } } }
-
Para API obter detalhes, consulte DetectEntitiesem AWS SDK for Java 2.x APIReferência.
-
- Python
-
- SDKpara Python (Boto3)
-
nota
Tem mais sobre GitHub. Encontre o exemplo completo e saiba como configurar e executar no Repositório de exemplos de código da AWS
. class ComprehendDetect: """Encapsulates Comprehend detection functions.""" def __init__(self, comprehend_client): """ :param comprehend_client: A Boto3 Comprehend client. """ self.comprehend_client = comprehend_client def detect_entities(self, text, language_code): """ Detects entities in a document. Entities can be things like people and places or other common terms. :param text: The document to inspect. :param language_code: The language of the document. :return: The list of entities along with their confidence scores. """ try: response = self.comprehend_client.detect_entities( Text=text, LanguageCode=language_code ) entities = response["Entities"] logger.info("Detected %s entities.", len(entities)) except ClientError: logger.exception("Couldn't detect entities.") raise else: return entities
-
Para API obter detalhes, consulte a DetectEntitiesReferência AWS SDK do Python (Boto3). API
-