Java 用 Azure ドキュメント インテリジェンス クライアント ライブラリ - バージョン 4.1.2

Azure Document Intelligence (旧称 Form Recognizer) は、機械学習を使用してドキュメントのテキストと構造化データを分析するクラウド サービスです。 これには、次のメイン機能が含まれています。

  • レイアウト - テキスト、テーブル構造、選択マーク、および境界領域座標をドキュメントから抽出します。
  • ドキュメント - 一般的な事前構築済みドキュメント モデルを使用して、ドキュメントのエンティティ、キーと値のペア、テーブル、および選択マークを分析します。
  • 事前構築済み - 事前構築済みモデルを使用して、特定の種類の一般的なドキュメント (領収書、請求書、名刺、ID ドキュメント、US W2 税フォームなど) のデータを分析します。
  • カスタム - テキスト、フィールド値、選択マーク、およびテーブル データをドキュメントから抽出するカスタム モデルを構築します。 カスタム モデルは独自のデータを使用して構築されるため、ドキュメントに合わせて調整されます。
  • 読み取り - テキスト言語情報に加えて、ページの単語や行など、テキスト要素に関する情報を読み取ります。
  • 分類子 - ドキュメントを定義済みのクラスに分類するカスタム分類子を構築します。

ソースコード | パッケージ (Maven) | API リファレンス ドキュメント | 製品ドキュメント | サンプル

作業の開始

前提条件

パッケージをインクルードする

BOM ファイルを含める

ライブラリの GA バージョンに依存するには、azure-sdk-bom をプロジェクトに含めます。 次のスニペットでは、{bom_version_to_target} プレースホルダーをバージョン番号に置き換えます。 BOM の詳細については、 AZURE SDK BOM README に関するページを参照してください。

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>com.azure</groupId>
            <artifactId>azure-sdk-bom</artifactId>
            <version>{bom_version_to_target}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

次に、バージョン タグを使用せずに、依存関係のセクションに直接依存関係を含めます。

<dependencies>
  <dependency>
    <groupId>com.azure</groupId>
    <artifactId>azure-ai-formrecognizer</artifactId>
  </dependency>
</dependencies>

直接依存関係を含める

BOM に存在しない特定のバージョンのライブラリに依存する場合は、次のように直接依存関係をプロジェクトに追加します。

<dependency>
    <groupId>com.azure</groupId>
    <artifactId>azure-ai-formrecognizer</artifactId>
    <version>4.1.2</version>
</dependency>

注: このバージョンのクライアント ライブラリは、既定でサービスの "2023-07-31" バージョンになります。

SDK のバージョンとサービスのサポートされる API バージョンの関係を次の表に示します。

SDK バージョン サポートされている API バージョンのサービス
3.0.x 2.0
3.1.X - 3.1.12 2.0、2.1 (既定値)
4.0.0 2.0、2.1、2022-08-31 (既定値)
4.1.0 2.0、2.1、2022-08-31、2023-07-31 (既定値)

注: バージョン 4.0.X 以降では、Form Recognizer サービスの最新機能を活用するために新しいクライアント セットが導入されました。 クライアント ライブラリ バージョン 3.1.X 以降から最新バージョンにアプリケーション コードを更新する方法の詳細については、 移行ガイド を参照してください。 詳細については、「 Changelog」を参照してください。 次の表では、各クライアントとそのサポートされている API バージョンの関係について説明します。

API バージョン サポートされるクライアント
2023-07-31 DocumentAnalysisClient と DocumentModelAdministrationClient
2022-08-31 DocumentAnalysisClient と DocumentModelAdministrationClient
2.1 FormRecognizerClient と FormTrainingClient
2.0 FormRecognizerClient と FormTrainingClient

Form Recognizer リソースを作成する

Form Recognizerでは、マルチサービス アクセスとシングルサービス アクセスの両方がサポートされます。 1 つのエンドポイント/キーで複数のコグニティブ サービスにアクセスする予定の場合は、Cognitive Service のリソースを作成します。 Form Recognizer アクセスのみの場合は、Form Recognizer リソースを作成します。

を使用して、いずれかのリソースを作成できます。

オプション 1:Azure portal

オプション 2:Azure CLI

CLI を使用してForm Recognizer リソースを作成する方法の例を次に示します。

# Create a new resource group to hold the Form Recognizer resource -
# if using an existing resource group, skip this step
az group create --name <your-resource-group> --location <location>
# Create Form Recognizer
az cognitiveservices account create \
    --name <your-form-recognizer-resource-name> \
    --resource-group <your-resource-group> \
    --kind FormRecognizer \
    --sku <sku> \
    --location <location> \
    --yes

クライアントを認証する

Form Recognizer サービスと対話するには、ドキュメント分析クライアントのインスタンスを作成する必要があります。 を使用 DocumentAnalysisClientBuilderして、非同期クライアントと同期クライアントの両方を作成できます。 を buildClient() 呼び出すと同期クライアントが作成され、 を buildAsyncClient 呼び出すと非同期クライアントが作成されます。

エンドポイントと、クライアント オブジェクトをインスタンス化するためのキーが必要です。

エンドポイントの参照

Form Recognizer リソースのエンドポイントは、Azure portalまたは Azure CLI で確認できます。

# Get the endpoint for the resource
az cognitiveservices account show --name "resource-name" --resource-group "resource-group-name" --query "endpoint"

AzureKeyCredential を使用してドキュメント分析クライアントを作成する

認証を使用 AzureKeyCredential するには、 キー を文字列として AzureKeyCredential に指定します。 このキーは、作成したForm Recognizer リソースのAzure portal、または次の Azure CLI コマンドを実行して、Form Recognizer リソースからキーを取得します。

az cognitiveservices account keys list --resource-group <your-resource-group-name> --name <your-resource-name>

クライアントを認証するには、資格情報パラメーターとして API キーを使用します。

DocumentAnalysisClient documentAnalysisClient = new DocumentAnalysisClientBuilder()
    .credential(new AzureKeyCredential("{key}"))
    .endpoint("{endpoint}")
    .buildClient();
DocumentModelAdministrationClient client =
    new DocumentModelAdministrationClientBuilder()
        .credential(new AzureKeyCredential("{key}"))
        .endpoint("{endpoint}")
        .buildClient();

Azure Active Directory 資格情報を使用してドキュメント分析クライアントを作成する

Azure SDK for Java では Azure Identity パッケージがサポートされているため、Microsoft ID プラットフォームから資格情報を簡単に取得できます。

AAD を使用した認証には、次のような初期セットアップが必要です。

  • Azure Identity パッケージを追加する
<dependency>
    <groupId>com.azure</groupId>
    <artifactId>azure-identity</artifactId>
    <version>1.10.0</version>
</dependency>

セットアップ後、使用する azure-identity から 資格情報 の種類を選択できます。 たとえば、 DefaultAzureCredential を使用してクライアントを認証できます。AAD アプリケーションのクライアント ID、テナント ID、クライアント シークレットの値を環境変数として設定します(AZURE_CLIENT_ID、AZURE_TENANT_ID、AZURE_CLIENT_SECRET)。

承認は、 DefaultAzureCredential を使用するのが最も簡単です。 実行中の環境で使用するのに最適な資格情報が見つかります。 Form Recognizerで Azure Active Directory 承認を使用する方法の詳細については、関連するドキュメントを参照してください

DocumentAnalysisClient documentAnalysisClient = new DocumentAnalysisClientBuilder()
    .endpoint("{endpoint}")
    .credential(new DefaultAzureCredentialBuilder().build())
    .buildClient();

主要な概念

DocumentAnalysisClient

DocumentAnalysisClientDocumentAnalysisAsyncClient は、 メソッドと beginAnalyzeDocumentFromUrl メソッドを使用してカスタム モデルと事前構築済みモデルを使用して入力ドキュメントを分析するための同期操作と非同期操作の両方をbeginAnalyzeDocument提供します。 サポートされているモデルの完全な一覧 については、こちらを参照してください

DocumentAnalysisClient の使用方法を示すサンプル コード スニペット については、こちらを参照してください。 サポートされている機能、ロケール、ドキュメントの種類など、ドキュメントの分析の詳細については、 こちらを参照してください

DocumentModelAdministrationClient

DocumentModelAdministrationClientDocumentModelAdministrationAsyncClient は、同期操作と非同期操作の両方を提供します

  • カスタム ドキュメント分析モデルを構築して、カスタム ドキュメントで見つかったテキストの内容、フィールド、値を分析します。 ドキュメント モデルのビルドの例を参照してください。 DocumentModelDetailsモデルが分析できるドキュメントの種類と、抽出するフィールドとスキーマを示す が返されます。
  • アカウントで作成されたモデルを管理するには、アカウントの構築、一覧表示、削除、およびカスタム モデルの制限を確認します。 モデル の管理の例を参照してください。
  • Form Recognizer リソース間でカスタム モデルをコピーする。
  • 既存の構築済みモデルのコレクションから構成済みモデルを作成する。
  • Form Recognizer リソースに関連付けられているドキュメント モデル操作を一覧表示します。

サンプル コード スニペットは、DocumentModelAdministrationClient をここに示すために用意されています。

長時間にわたって実行される操作

実行時間の長い操作は、操作を開始するためにサービスに送信された最初の要求で構成される操作です。その後、間隔を指定してサービスをポーリングし、操作が完了したか失敗したか、成功したかどうかを判断して結果を取得します。

モデルの構築、ドキュメントからの値の分析、モデルのコピーと作成を行うメソッドは、実行時間の長い操作としてモデル化されます。 クライアントは、 または インスタンスを begin<MethodName> 返すメソッドを SyncPoller 公開 PollerFlux します。 呼び出し元は、 メソッドから返された操作に対して を呼び出 getFinalResult() すことによって、操作が完了するまで待機する begin<MethodName> 必要があります。 以下に、実行時間の長い操作を使用する方法を示すサンプル コード スニペットを示 します

次のセクションでは、次のような最も一般的なForm Recognizer タスクをいくつか取り上げて、いくつかのコード スニペットを示します。

レイアウトの抽出

モデルを構築しなくても、テキスト、テーブル構造、およびラジオ ボタンやチェック ボックスなどの選択マークとその境界ボックス座標をドキュメントから抽出します。

// analyze document layout using file input stream
File layoutDocument = new File("local/file_path/filename.png");
Path filePath = layoutDocument.toPath();
BinaryData layoutDocumentData = BinaryData.fromFile(filePath, (int) layoutDocument.length());

SyncPoller<OperationResult, AnalyzeResult> analyzeLayoutResultPoller =
    documentAnalysisClient.beginAnalyzeDocument("prebuilt-layout", layoutDocumentData);

AnalyzeResult analyzeLayoutResult = analyzeLayoutResultPoller.getFinalResult();

// pages
analyzeLayoutResult.getPages().forEach(documentPage -> {
    System.out.printf("Page has width: %.2f and height: %.2f, measured with unit: %s%n",
        documentPage.getWidth(),
        documentPage.getHeight(),
        documentPage.getUnit());

    // lines
    documentPage.getLines().forEach(documentLine ->
        System.out.printf("Line '%s' is within a bounding box %s.%n",
            documentLine.getContent(),
            documentLine.getBoundingPolygon().toString()));

    // selection marks
    documentPage.getSelectionMarks().forEach(documentSelectionMark ->
        System.out.printf("Selection mark is '%s' and is within a bounding box %s with confidence %.2f.%n",
            documentSelectionMark.getSelectionMarkState().toString(),
            documentSelectionMark.getBoundingPolygon().toString(),
            documentSelectionMark.getConfidence()));
});

// tables
List<DocumentTable> tables = analyzeLayoutResult.getTables();
for (int i = 0; i < tables.size(); i++) {
    DocumentTable documentTable = tables.get(i);
    System.out.printf("Table %d has %d rows and %d columns.%n", i, documentTable.getRowCount(),
        documentTable.getColumnCount());
    documentTable.getCells().forEach(documentTableCell -> {
        System.out.printf("Cell '%s', has row index %d and column index %d.%n", documentTableCell.getContent(),
            documentTableCell.getRowIndex(), documentTableCell.getColumnIndex());
    });
    System.out.println();
}

一般的なドキュメント モデルを使用する

Form Recognizer サービスによって提供される一般的なドキュメント モデルを使用して、ドキュメントのキーと値のペア、テーブル、スタイル、および選択マークを分析します。 次のように、modelId="prebuilt-document" を beginAnalyzeDocumentFromUrl メソッドに渡して、一般ドキュメント モデルを選択します。

String documentUrl = "{document-url}";
String modelId = "prebuilt-document";
SyncPoller<OperationResult, AnalyzeResult> analyzeDocumentPoller =
    documentAnalysisClient.beginAnalyzeDocumentFromUrl(modelId, documentUrl);

AnalyzeResult analyzeResult = analyzeDocumentPoller.getFinalResult();

for (int i = 0; i < analyzeResult.getDocuments().size(); i++) {
    final AnalyzedDocument analyzedDocument = analyzeResult.getDocuments().get(i);
    System.out.printf("----------- Analyzing document %d -----------%n", i);
    System.out.printf("Analyzed document has doc type %s with confidence : %.2f%n",
        analyzedDocument.getDocType(), analyzedDocument.getConfidence());
}

analyzeResult.getPages().forEach(documentPage -> {
    System.out.printf("Page has width: %.2f and height: %.2f, measured with unit: %s%n",
        documentPage.getWidth(),
        documentPage.getHeight(),
        documentPage.getUnit());

    // lines
    documentPage.getLines().forEach(documentLine ->
        System.out.printf("Line '%s' is within a bounding box %s.%n",
            documentLine.getContent(),
            documentLine.getBoundingPolygon().toString()));

    // words
    documentPage.getWords().forEach(documentWord ->
        System.out.printf("Word '%s' has a confidence score of %.2f.%n",
            documentWord.getContent(),
            documentWord.getConfidence()));
});

// tables
List<DocumentTable> tables = analyzeResult.getTables();
for (int i = 0; i < tables.size(); i++) {
    DocumentTable documentTable = tables.get(i);
    System.out.printf("Table %d has %d rows and %d columns.%n", i, documentTable.getRowCount(),
        documentTable.getColumnCount());
    documentTable.getCells().forEach(documentTableCell -> {
        System.out.printf("Cell '%s', has row index %d and column index %d.%n",
            documentTableCell.getContent(),
            documentTableCell.getRowIndex(), documentTableCell.getColumnIndex());
    });
    System.out.println();
}

// Key-value
analyzeResult.getKeyValuePairs().forEach(documentKeyValuePair -> {
    System.out.printf("Key content: %s%n", documentKeyValuePair.getKey().getContent());
    System.out.printf("Key content bounding region: %s%n",
        documentKeyValuePair.getKey().getBoundingRegions().toString());

    System.out.printf("Value content: %s%n", documentKeyValuePair.getValue().getContent());
    System.out.printf("Value content bounding region: %s%n", documentKeyValuePair.getValue().getBoundingRegions().toString());
});

事前構築済みモデルを使用する

Form Recognizer サービスによって提供される事前構築済みモデルを使用して、領収書、請求書、名刺、ID ドキュメントなどの選択したドキュメントの種類からフィールドを抽出します。 サポートされている事前構築済みモデルは次のとおりです。

たとえば、売上領収書のフィールドをメソッドに分析するには、次のようにします beginAnalyzeDocumentFromUrl

String receiptUrl = "https://raw.githubusercontent.com/Azure/azure-sdk-for-java/main/sdk/formrecognizer"
    + "/azure-ai-formrecognizer/src/samples/resources/sample-documents/receipts/contoso-allinone.jpg";

SyncPoller<OperationResult, AnalyzeResult> analyzeReceiptPoller =
    documentAnalysisClient.beginAnalyzeDocumentFromUrl("prebuilt-receipt", receiptUrl);

AnalyzeResult receiptResults = analyzeReceiptPoller.getFinalResult();

for (int i = 0; i < receiptResults.getDocuments().size(); i++) {
    AnalyzedDocument analyzedReceipt = receiptResults.getDocuments().get(i);
    Map<String, DocumentField> receiptFields = analyzedReceipt.getFields();
    System.out.printf("----------- Analyzing receipt info %d -----------%n", i);
    DocumentField merchantNameField = receiptFields.get("MerchantName");
    if (merchantNameField != null) {
        if (DocumentFieldType.STRING == merchantNameField.getType()) {
            String merchantName = merchantNameField.getValueAsString();
            System.out.printf("Merchant Name: %s, confidence: %.2f%n",
                merchantName, merchantNameField.getConfidence());
        }
    }

    DocumentField merchantPhoneNumberField = receiptFields.get("MerchantPhoneNumber");
    if (merchantPhoneNumberField != null) {
        if (DocumentFieldType.PHONE_NUMBER == merchantPhoneNumberField.getType()) {
            String merchantAddress = merchantPhoneNumberField.getValueAsPhoneNumber();
            System.out.printf("Merchant Phone number: %s, confidence: %.2f%n",
                merchantAddress, merchantPhoneNumberField.getConfidence());
        }
    }

    DocumentField transactionDateField = receiptFields.get("TransactionDate");
    if (transactionDateField != null) {
        if (DocumentFieldType.DATE == transactionDateField.getType()) {
            LocalDate transactionDate = transactionDateField.getValueAsDate();
            System.out.printf("Transaction Date: %s, confidence: %.2f%n",
                transactionDate, transactionDateField.getConfidence());
        }
    }

    DocumentField receiptItemsField = receiptFields.get("Items");
    if (receiptItemsField != null) {
        System.out.printf("Receipt Items: %n");
        if (DocumentFieldType.LIST == receiptItemsField.getType()) {
            List<DocumentField> receiptItems = receiptItemsField.getValueAsList();
            receiptItems.stream()
                .filter(receiptItem -> DocumentFieldType.MAP == receiptItem.getType())
                .map(documentField -> documentField.getValueAsMap())
                .forEach(documentFieldMap -> documentFieldMap.forEach((key, documentField) -> {
                    if ("Name".equals(key)) {
                        if (DocumentFieldType.STRING == documentField.getType()) {
                            String name = documentField.getValueAsString();
                            System.out.printf("Name: %s, confidence: %.2fs%n",
                                name, documentField.getConfidence());
                        }
                    }
                    if ("Quantity".equals(key)) {
                        if (DocumentFieldType.DOUBLE == documentField.getType()) {
                            Double quantity = documentField.getValueAsDouble();
                            System.out.printf("Quantity: %f, confidence: %.2f%n",
                                quantity, documentField.getConfidence());
                        }
                    }
                }));
        }
    }
}

事前構築済みモデルを使用した詳細とサンプルについては、次を参照してください。

ドキュメント モデルを構築する

独自のドキュメントの種類で機械学習モデルを構築します。 結果のモデルでは、構築されたドキュメントの種類の値を分析できます。 トレーニング ドキュメントを格納している Azure Storage BLOB コンテナーにコンテナー SAS URL を指定します。 この設定の詳細については、サービスの クイックスタート ドキュメントを参照してください

Form Recognizer Studio プレビューを使用して、トレーニング フォームのラベル付きファイルを作成できます。 コンテナーの設定と必要なファイル構造の詳細については、 こちらを参照してください

// Build custom document analysis model
String blobContainerUrl = "{SAS_URL_of_your_container_in_blob_storage}";
// The shared access signature (SAS) Url of your Azure Blob Storage container with your forms.
String prefix = "{blob_name_prefix}}";
SyncPoller<OperationResult, DocumentModelDetails> buildOperationPoller =
    documentModelAdminClient.beginBuildDocumentModel(blobContainerUrl,
        DocumentModelBuildMode.TEMPLATE,
        prefix,
        new BuildDocumentModelOptions().setModelId("my-build-model").setDescription("model desc"),
        Context.NONE);

DocumentModelDetails documentModelDetails = buildOperationPoller.getFinalResult();

// Model Info
System.out.printf("Model ID: %s%n", documentModelDetails.getModelId());
System.out.printf("Model Description: %s%n", documentModelDetails.getDescription());
System.out.printf("Model created on: %s%n%n", documentModelDetails.getCreatedOn());
documentModelDetails.getDocumentTypes().forEach((key, documentTypeDetails) -> {
    System.out.printf("Document type: %s%n", key);
    documentTypeDetails.getFieldSchema().forEach((name, documentFieldSchema) -> {
        System.out.printf("Document field: %s%n", name);
        System.out.printf("Document field type: %s%n", documentFieldSchema.getType().toString());
        System.out.printf("Document field confidence: %.2f%n", documentTypeDetails.getFieldConfidence().get(name));
    });
});

カスタム モデルを使用してドキュメントを分析する

ドキュメントのキーと値のペアとテーブル データを分析します。 これらのモデルは独自のデータを使用して構築されているため、ドキュメントに合わせて調整されます。 カスタム モデルが構築されたのと同じドキュメントの種類のドキュメントのみを分析する必要があります。

String documentUrl = "{document-url}";
String modelId = "{custom-built-model-ID}";
SyncPoller<OperationResult, AnalyzeResult> analyzeDocumentPoller =
    documentAnalysisClient.beginAnalyzeDocumentFromUrl(modelId, documentUrl);

AnalyzeResult analyzeResult = analyzeDocumentPoller.getFinalResult();

for (int i = 0; i < analyzeResult.getDocuments().size(); i++) {
    final AnalyzedDocument analyzedDocument = analyzeResult.getDocuments().get(i);
    System.out.printf("----------- Analyzing custom document %d -----------%n", i);
    System.out.printf("Analyzed document has doc type %s with confidence : %.2f%n",
        analyzedDocument.getDocType(), analyzedDocument.getConfidence());
    analyzedDocument.getFields().forEach((key, documentField) -> {
        System.out.printf("Document Field content: %s%n", documentField.getContent());
        System.out.printf("Document Field confidence: %.2f%n", documentField.getConfidence());
        System.out.printf("Document Field Type: %s%n", documentField.getType());
        System.out.printf("Document Field found within bounding region: %s%n",
            documentField.getBoundingRegions().toString());
    });
}

analyzeResult.getPages().forEach(documentPage -> {
    System.out.printf("Page has width: %.2f and height: %.2f, measured with unit: %s%n",
        documentPage.getWidth(),
        documentPage.getHeight(),
        documentPage.getUnit());

    // lines
    documentPage.getLines().forEach(documentLine ->
        System.out.printf("Line '%s' is within a bounding box %s.%n",
            documentLine.getContent(),
            documentLine.getBoundingPolygon().toString()));

    // words
    documentPage.getWords().forEach(documentWord ->
        System.out.printf("Word '%s' has a confidence score of %.2f.%n",
            documentWord.getContent(),
            documentWord.getConfidence()));
});

// tables
List<DocumentTable> tables = analyzeResult.getTables();
for (int i = 0; i < tables.size(); i++) {
    DocumentTable documentTable = tables.get(i);
    System.out.printf("Table %d has %d rows and %d columns.%n", i, documentTable.getRowCount(),
        documentTable.getColumnCount());
    documentTable.getCells().forEach(documentTableCell -> {
        System.out.printf("Cell '%s', has row index %d and column index %d.%n",
            documentTableCell.getContent(),
            documentTableCell.getRowIndex(), documentTableCell.getColumnIndex());
    });
    System.out.println();
}

モデルを管理する

Form Recognizer アカウントでモデルを管理します。

AtomicReference<String> modelId = new AtomicReference<>();

// First, we see how many models we have, and what our limit is
ResourceDetails resourceDetails = documentModelAdminClient.getResourceDetails();
System.out.printf("The resource has %s models, and we can have at most %s models",
    resourceDetails.getCustomDocumentModelCount(), resourceDetails.getCustomDocumentModelLimit());

// Next, we get a paged list of all of our models
PagedIterable<DocumentModelSummary> customDocumentModels = documentModelAdminClient.listDocumentModels();
System.out.println("We have following models in the account:");
customDocumentModels.forEach(documentModelSummary -> {
    System.out.printf("Model ID: %s%n", documentModelSummary.getModelId());
    modelId.set(documentModelSummary.getModelId());

    // get custom document analysis model info
    DocumentModelDetails documentModel = documentModelAdminClient.getDocumentModel(documentModelSummary.getModelId());
    System.out.printf("Model ID: %s%n", documentModel.getModelId());
    System.out.printf("Model Description: %s%n", documentModel.getDescription());
    System.out.printf("Model created on: %s%n", documentModel.getCreatedOn());
    documentModel.getDocumentTypes().forEach((key, documentTypeDetails) -> {
        documentTypeDetails.getFieldSchema().forEach((field, documentFieldSchema) -> {
            System.out.printf("Field: %s", field);
            System.out.printf("Field type: %s", documentFieldSchema.getType());
            System.out.printf("Field confidence: %.2f", documentTypeDetails.getFieldConfidence().get(field));
        });
    });
});

// Delete Model
documentModelAdminClient.deleteDocumentModel(modelId.get());

ドキュメントを分類する

Form Recognizer サービスでは、トレーニング データ セットに基づいてドキュメントを一連の定義済みカテゴリに分類できるカスタム ドキュメント分類子がサポートされています。 ドキュメントは、 の メソッドまたは beginClassifyDocumentFromUrl メソッドDocumentAnalysisClientを使用して、カスタム分類子でbeginClassifyDocument分類できます。 次の例は、カスタム分類子を使用してドキュメントを分類する方法を示しています。

String documentUrl = "{file_source_url}";
String classifierId = "{custom_trained_classifier_id}";

documentAnalysisClient.beginClassifyDocumentFromUrl(classifierId, documentUrl, Context.NONE)
    .getFinalResult()
    .getDocuments()
    .forEach(analyzedDocument -> System.out.printf("Doc Type: %s%n", analyzedDocument.getDocType()));

詳細な例については、 サンプルを参照してください。

トラブルシューティング

全般

クライアントForm Recognizer例外が発生HttpResponseExceptionします。 たとえば、無効なファイル ソースの URL を指定しようとすると、失敗の原因を示すエラーと共に HttpResponseException が発生します。 次のコード スニペットでは、例外をキャッチし、エラーに関する追加情報を表示することで、エラーが適切に処理されます。

try {
    documentAnalysisClient.beginAnalyzeDocumentFromUrl("prebuilt-receipt", "invalidSourceUrl");
} catch (HttpResponseException e) {
    System.out.println(e.getMessage());
    // Do something with the exception
}

クライアントのログ記録を有効にする

Azure SDK for Java には、アプリケーション エラーのトラブルシューティングと解決の迅速化に役立つ一貫したログ記録のストーリーが用意されています。 生成されたログでは、最終状態に達する前のアプリケーションのフローがキャプチャされ、根本原因を特定するのに役立ちます。 ログ記録の有効化に関するガイダンスについては、ログ Wiki を参照してください。

既定の HTTP クライアント

すべてのクライアント ライブラリでは、Netty HTTP クライアントが既定で使用されます。 上記の依存関係を追加して、Netty HTTP クライアントを使用するようにクライアント ライブラリを自動的に構成します。 HTTP クライアントの構成と変更については、HTTP クライアントの Wiki で詳しく説明されています。

次の手順

次のセクションでは、Form Recognizer API で使用される一般的なパターンを示すコード スニペットをいくつか示します。 これらのコード サンプルは、Azure Form Recognizer クライアント ライブラリでの一般的なシナリオ操作を示しています。

非同期 API

これまでに示したすべての例では同期 API が使用されていますが、非同期 API も完全にサポートされています。 を使用する必要があります DocumentAnalysisAsyncClient

DocumentAnalysisAsyncClient documentAnalysisAsyncClient = new DocumentAnalysisClientBuilder()
    .credential(new AzureKeyCredential("{key}"))
    .endpoint("{endpoint}")
    .buildAsyncClient();

その他のドキュメント

Form Recognizer Java SDK で使用される一般的なパターンを示すいくつかのコード スニペットについては、「サンプル README」を参照してください。 Azure Cognitive Services Form Recognizerの詳細なドキュメントについては、Form Recognizerドキュメントを参照してください。

共同作成

このプロジェクトでは、共同作成と提案を歓迎しています。 ほとんどの共同作成では、共同作成者使用許諾契約書 (CLA) にご同意いただき、ご自身の共同作成内容を使用する権利を Microsoft に供与する権利をお持ちであり、かつ実際に供与することを宣言していただく必要があります。

pull request を送信すると、CLA を提供して PR (ラベル、コメントなど) を適宜装飾する必要があるかどうかを CLA ボットが自動的に決定します。 ボットによって提供される手順にそのまま従ってください。 この操作は、Microsoft の CLA を使用するすべてのリポジトリについて、1 回だけ行う必要があります。

このプロジェクトでは、Microsoft オープン ソースの倫理規定を採用しています。 詳細については、倫理規定についてよくあるご質問を参照するか、opencode@microsoft.com 宛てにご質問またはコメントをお送りください。

インプレッション数