本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。
自訂分類的即時分析 (API)
您可以使用 Amazon Comprehend API 搭配自訂模型執行即時分類。首先,您會建立端點來執行即時分析。建立端點之後,您會執行即時分類。
本節中的範例使用 Unix、Linux 和 macOS 的命令格式。用於 Windows 時,請以插入號 (^) 取代每一行結尾處的 Unix 接續字元斜線 (\)。
如需佈建端點輸送量和相關成本的資訊,請參閱使用 Amazon Comprehend 端點。
建立自訂分類的端點
下列範例顯示使用 的 CreateEndpoint API 操作 AWS CLI。
aws comprehend create-endpoint \ --desired-inference-units
number of inference units
\ --endpoint-nameendpoint name
\ --model-arn arn:aws:comprehend:region
:account-id
:model/example
\ --tags Key=My1stTag
,Value=Value1
Amazon Comprehend 會回應下列事項:
{ "EndpointArn": "
Arn
" }
執行即時自訂分類
為自訂分類模型建立端點後,您可以使用端點來執行 ClassifyDocument API 操作。您可以使用 text
或 bytes
參數提供文字輸入。使用 bytes
參數輸入其他輸入類型。
對於影像檔案和 PDF 檔案,您可以使用 DocumentReaderConfig
參數覆寫預設的文字擷取動作。如需詳細資訊,請參閱設定文字擷取選項
為了獲得最佳結果,請將輸入類型與分類器模型類型配對。如果您將原生文件提交至純文字模型,或將純文字檔案提交至原生文件模型,則 API 回應會包含警告。如需詳細資訊,請參閱訓練分類模型。
使用 AWS Command Line Interface
下列範例示範如何使用 classify-document CLI 命令。
使用 分類文字 AWS CLI
下列範例會在文字區塊上執行即時分類。
aws comprehend classify-document \ --endpoint-arn arn:aws:comprehend:
region
:account-id
:endpoint/endpoint name
\ --text 'From the Tuesday, April 16th, 1912 edition of The Guardian newspaper: The maiden voyage of the White Star liner Titanic, the largest ship ever launched ended in disaster. The Titanic started her trip from Southampton for New York on Wednesday. Late on Sunday night she struck an iceberg off the Grand Banks of Newfoundland. By wireless telegraphy she sent out signals of distress, and several liners were near enough to catch and respond to the call.'
Amazon Comprehend 會回應下列事項:
{ "Classes": [ { "Name": "string", "Score": 0.9793661236763 } ] }
使用 分類半結構化文件 AWS CLI
若要分析 PDF、Word 或映像檔案的自訂分類,請在 bytes
參數中使用輸入檔案執行 classify-document
命令。
下列範例使用 映像做為輸入檔案。它使用 fileb
選項來編碼映像檔案位元組的 base-64。如需詳細資訊,請參閱 AWS Command Line Interface 《 使用者指南》中的二進位大型物件。
此範例也會傳入名為 的 JSON 檔案,config.json
以設定文字擷取選項。
$
aws comprehend classify-document \
>
--endpoint-arn
arn
\
>
--language-code
en
\
>
--bytes
fileb://image1.jpg\
>
--document-reader-config file://config.json
config.json 檔案包含下列內容。
{ "DocumentReadMode": "FORCE_DOCUMENT_READ_ACTION", "DocumentReadAction": "TEXTRACT_DETECT_DOCUMENT_TEXT" }
Amazon Comprehend 會回應下列事項:
{ "Classes": [ { "Name": "string", "Score": 0.9793661236763 } ] }
如需詳細資訊,請參閱《Amazon Comprehend API 參考》中的 ClassifyDocument。