本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。
第 4 步:开始使用亚马逊 Comprehend Medical APIs
以下示例演示了如何使用、Java 和 Python 来使用 Amazon Comprehend Medical 操作。 AWS CLI使用它们了解 Amazon Comprehend Medical 操作,并将其作为您自己的应用程序的构建块。
要运行 AWS CLI 和 Python 示例,请安装 AWS CLI。有关更多信息,请参阅 步骤 2:设置 AWS Command Line Interface (AWS CLI)。
要运行 Java 示例,请安装 适用于 Java 的 AWS SDK。有关安装 适用于 Java 的 AWS SDK的说明,请参阅 Set up the AWS SDK for Java。
使用 AWS Command Line Interface检测医疗实体
以下示例演示如何使用DetectEntitiesV2操作返回文本中检测到的医疗实体。 AWS CLI 要运行示例,您必须安装 AWS CLI。有关更多信息,请参阅 步骤 2:设置 AWS Command Line Interface (AWS CLI)。
此示例的格式适用于 Unix、Linux 和 macOS。对于 Windows,请将每行末尾的反斜杠 (\) Unix 行继续符替换为脱字号 (^)。
aws comprehendmedical detect-entities-v2 \ --endpointendpoint\ --regionregion\ --text "aspirin is required 20 mg po daily for 2 times as tab"
响应将为以下内容:
{
    "Entities": [
        {
            "Category": "MEDICATION", 
            "BeginOffset": 0, 
            "EndOffset": 7, 
            "Text": "aspirin", 
            "Traits": [], 
            "Score": 0.9988090991973877, 
            "Attributes": [
                {
                    "BeginOffset": 20, 
                    "EndOffset": 25, 
                    "Text": "20 mg", 
                    "Traits": [], 
                    "Score": 0.9559056162834167, 
                    "Type": "DOSAGE", 
                    "Id": 1, 
                    "RelationshipScore": 0.9981593489646912
                }, 
                {
                    "BeginOffset": 26, 
                    "EndOffset": 28, 
                    "Text": "po", 
                    "Traits": [], 
                    "Score": 0.9995359182357788, 
                    "Type": "ROUTE_OR_MODE", 
                    "Id": 2, 
                    "RelationshipScore": 0.9969323873519897
                }, 
                {
                    "BeginOffset": 29, 
                    "EndOffset": 34, 
                    "Text": "daily", 
                    "Traits": [], 
                    "Score": 0.9803128838539124, 
                    "Type": "FREQUENCY", 
                    "Id": 3, 
                    "RelationshipScore": 0.9990783929824829
                }, 
                {
                    "BeginOffset": 39, 
                    "EndOffset": 46, 
                    "Text": "2 times", 
                    "Traits": [], 
                    "Score": 0.8623972535133362, 
                    "Type": "DURATION", 
                    "Id": 4, 
                    "RelationshipScore": 0.9996501207351685
                }, 
                {
                    "BeginOffset": 50, 
                    "EndOffset": 53, 
                    "Text": "tab", 
                    "Traits": [], 
                    "Score": 0.784785270690918, 
                    "Type": "FORM", 
                    "Id": 5, 
                    "RelationshipScore": 0.9986748695373535
                }
            ], 
            "Type": "GENERIC_NAME", 
            "Id": 0
        }
    ], 
    "UnmappedAttributes": []
}使用 适用于 Java 的 AWS SDK检测医疗实体
以下示例使用 Java 的 DetectEntitiesV2 操作。要运行示例,请安装 适用于 Java 的 AWS SDK。有关安装的说明 适用于 Java 的 AWS SDK,请参阅设置适用于 Java 的 AWS 开发工具包。
import com.amazonaws.auth.AWSCredentials; import com.amazonaws.auth.AWSCredentialsProvider; import com.amazonaws.auth.AWSStaticCredentialsProvider; import com.amazonaws.auth.BasicAWSCredentials; import com.amazonaws.client.builder.AwsClientBuilder; import com.amazonaws.services.comprehendmedical.AWSComprehendMedical; import com.amazonaws.services.comprehendmedical.AWSComprehendMedicalClient; import com.amazonaws.services.comprehendmedical.model.DetectEntitiesRequest; import com.amazonaws.services.comprehendmedical.model.DetectEntitiesResult; public class SampleAPICall { public static void main() { AWSCredentialsProvider credentials = new AWSStaticCredentialsProvider(new BasicAWSCredentials("YOUR AWS ACCESS KEY", "YOUR AWS SECRET")); AWSComprehendMedical client = AWSComprehendMedicalClient.builder() .withCredentials(credentials) .withRegion("YOUR REGION") .build(); DetectEntitiesV2Request request = new DetectEntitiesV2Request(); request.setText("cerealx 84 mg daily"); DetectEntitiesV2Result result = client.detectEntitiesV2(request); result.getEntities().forEach(System.out::println); } }
输出包含在输入文本中找到的三个实体,以及它们在输入文本中的位置。每个实体还列出了正确识别该实体的可信度。以下输出显示了前面示例中的 Generic_Name、Dosage 和 Frequency 实体。
{Id: 0,BeginOffset: 0,EndOffset: 3,Score: 0.9940211,Text: Bob,Category: 
PROTECTED_HEALTH_INFORMATION,Type: NAME,Traits: [],}
{Id: 2,BeginOffset: 23,EndOffset: 30,Score: 0.99914634,Text: aspirin,Category: MEDICATION,Type: GENERIC_NAME,Traits: [],Attributes: 
[{Type: DOSAGE,Score: 0.9630807,RelationshipScore: 0.99969745,Id: 1,BeginOffset: 14,EndOffset: 19,Text: 50 mg,Traits: []}]}使用 AWS SDK for Python (Boto)检测医疗实体
以下示例使用 Python 的 DetectEntitiesV2 操作。要运行示例,请安装 AWS CLI。有关更多信息,请参阅 步骤 2:设置 AWS Command Line Interface (AWS CLI)。
import boto3 client = boto3.client(service_name='comprehendmedical', region_name='YOUR REGION') result = client.detect_entities(Text= 'cerealx 84 mg daily') entities = result['Entities'] for entity in entities: print('Entity', entity)
输出包含在输入文本中找到的三个实体,以及它们在输入文本中的位置。每个实体还列出了正确识别该实体的可信度。以下输出显示了前面示例中的 Generic_Name、Dosage 和 Frequency 实体。
('Entity', {u'Category': u'MEDICATION', u'BeginOffset': 0, u'EndOffset': 7, 
            u'Text': u'cerealx', u'Traits': [], u'Score': 0.8877691626548767, u'Attributes': [{u'BeginOffset': 8, u'EndOffset': 13, 
            u'Text': u'84 mg', u'Traits': [], u'Score': 0.9337134957313538, u'Type': u'DOSAGE', u'Id': 1, u'RelationshipScore': 0.9995118379592896}, 
            {u'BeginOffset': 14, u'EndOffset': 19, u'Text': u'daily', u'Traits': [], u'Score': 0.990627646446228, u'Type': u'FREQUENCY', 
            u'Id': 2, u'RelationshipScore': 0.9987651109695435}], u'Type': u'BRAND_NAME', u'Id': 0})