Skip to content

Commit 18e2e92

Browse files
committed
update java to single script
1 parent 15d278d commit 18e2e92

File tree

2 files changed

+98
-116
lines changed

2 files changed

+98
-116
lines changed

articles/applied-ai-services/form-recognizer/quickstarts/try-v3-java-sdk.md

Lines changed: 93 additions & 110 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,9 @@ manager: nitinme
77
ms.service: applied-ai-services
88
ms.subservice: forms-recognizer
99
ms.topic: quickstart
10-
ms.date: 03/08/2022
10+
ms.date: 03/15/2022
1111
ms.author: lajanuar
1212
recommendations: false
13-
ms.custom: ignite-fall-2021, mode-api
1413
---
1514
<!-- markdownlint-disable MD025 -->
1615

@@ -19,7 +18,7 @@ ms.custom: ignite-fall-2021, mode-api
1918
>[!NOTE]
2019
> Form Recognizer v3.0 is currently in public preview. Some features may not be supported or have limited capabilities.
2120
22-
[Reference documentation](https://azuresdkdocs.blob.core.windows.net/$web/java/azure-ai-formrecognizer/4.0.0-beta.4/index.html) | [Library source code](https://github.com/Azure/azure-sdk-for-java/tree/azure-ai-formrecognizer_4.0.0-beta.4/sdk/formrecognizer/azure-ai-formrecognizer/) | [Package (Maven)](https://search.maven.org/artifact/com.azure/azure-ai-formrecognizer/4.0.0-beta.4/jar) | [Samples](https://github.com/Azure/azure-sdk-for-java/blob/main/sdk/formrecognizer/azure-ai-formrecognizer/src/samples/README.md)
21+
[Reference documentation](/java/api/overview/azure/ai-formrecognizer-readme?view=azure-java-preview&preserve-view=true) | [Library source code](https://github.com/Azure/azure-sdk-for-java/tree/azure-ai-formrecognizer_4.0.0-beta.4/sdk/formrecognizer/azure-ai-formrecognizer/) | [Package (Maven)](https://search.maven.org/artifact/com.azure/azure-ai-formrecognizer/4.0.0-beta.4/jar) | [Samples](https://github.com/Azure/azure-sdk-for-java/blob/main/sdk/formrecognizer/azure-ai-formrecognizer/src/samples/README.md)
2322

2423
Get started with Azure Form Recognizer using the Java programming language. Azure Form Recognizer is a cloud-based Azure Applied AI Service that uses machine learning to extract key-value pairs, text, and tables from your documents. You can easily call Form Recognizer models by integrating our client library SDks into your workflows and applications. We recommend that you use the free service when you're learning the technology. Remember that the number of free pages is limited to 500 per month.
2524

@@ -36,6 +35,7 @@ In this quickstart you'll use following features to analyze and extract data and
3635
## Prerequisites
3736

3837
* Azure subscription - [Create one for free](https://azure.microsoft.com/free/cognitive-services/).
38+
3939
* The latest version of [Visual Studio Code](https://code.visualstudio.com/) or your preferred IDE. *See* [Java in Visual Studio Code](https://code.visualstudio.com/docs/languages/java).
4040

4141
>[!TIP]
@@ -96,11 +96,13 @@ This quickstart uses the Gradle dependency manager. You can find the client libr
9696
mavenCentral()
9797
}
9898
dependencies {
99-
implementation(group = "com.azure", name = "azure-ai-formrecognizer", version = "4.0.0-beta.3")
99+
implementation(group = "com.azure", name = "azure-ai-formrecognizer", version = "4.0.0-beta.4")
100100
}
101101
```
102102

103-
#### Create a Java file
103+
#### Create a Java application
104+
105+
To interact with the Form Recognizer service, you'll need to create an instance of the `DocumentAnalysisClient` class. To do so, you'll create an `AzureKeyCredential` with your key from the Azure portal and a `DocumentAnalysisClient` instance with the `AzureKeyCredential` and your Form Recognizer `endpoint`.
104106

105107
1. From the form-recognizer-app directory, run the following command:
106108

@@ -120,44 +122,7 @@ This quickstart uses the Gradle dependency manager. You can find the client libr
120122
> * Open a PowerShell window in your project directory by holding down the Shift key and right-clicking the folder.
121123
> * Type the following command **New-Item FormRecognizer.java**.
122124

123-
1. Open the `FormRecognizer.java` file in your preferred editor or IDE and add the following `import` statements:
124-
125-
```java
126-
import com.azure.ai.formrecognizer.*;
127-
import com.azure.ai.formrecognizer.models.AnalyzeResult;
128-
import com.azure.ai.formrecognizer.models.DocumentLine;
129-
import com.azure.ai.formrecognizer.models.AnalyzedDocument;
130-
import com.azure.ai.formrecognizer.models.DocumentOperationResult;
131-
import com.azure.ai.formrecognizer.models.DocumentWord;
132-
import com.azure.ai.formrecognizer.models.DocumentTable;
133-
import com.azure.core.credential.AzureKeyCredential;
134-
import com.azure.core.util.polling.SyncPoller;
135-
136-
import java.util.List;
137-
import java.util.Arrays;
138-
```
139-
140-
#### Create the **FormRecognizer** class:
141-
142-
Next, you'll need to create a public class for your project:
143-
144-
```java
145-
public class FormRecognizer {
146-
// All project code goes here...
147-
}
148-
```
149-
150-
> [!TIP]
151-
> If you would like to try more than one code sample:
152-
>
153-
> * Select one of the sample code blocks below to copy and paste into your application.
154-
> * [**Build and run your application**](#build-and-run-your-application).
155-
> * Comment out that sample code block but keep the set-up code and library directives.
156-
> * Select another sample code block to copy and paste into your application.
157-
> * [**Build and run your application**](#build-and-run-your-application).
158-
> * You can continue to comment out, copy/paste, build, and run the sample blocks of code.
159-
160-
#### Select a code sample to copy and paste into your application's main method:
125+
1. Open the `FormRecognizer.java` file and select one of the following code samples to copy and pasted into your application:
161126

162127
* [**General document**](#general-document-model)
163128

@@ -180,83 +145,101 @@ Extract text, tables, structure, key-value pairs, and named entities from docume
180145
> * We've added the file URI value to the `documentUrl` variable in the main method.
181146
> * For simplicity, all the entity fields that the service returns are not shown here. To see the list of all supported fields and corresponding types, see our [General document](../concept-general-document.md#named-entity-recognition-ner-categories) concept page.
182147

183-
Add the following code to the `FormRecognizer` class. Make sure you update the key and endpoint variables with values from your Form Recognizer instance in the Azure portal:
148+
**Add the following code sample to the `FormRecognizer` class. Make sure you update the key and endpoint variables with values from your Form Recognizer instance in the Azure portal:
184149

185150
```java
186151

187-
private static final String key = "PASTE_YOUR_FORM_RECOGNIZER_SUBSCRIPTION_KEY_HERE";
188-
private static final String endpoint = "PASTE_YOUR_FORM_RECOGNIZER_ENDPOINT_HERE";
189-
190-
public static void main(String[] args) {
191-
192-
DocumentAnalysisClient client = new DocumentAnalysisClientBuilder()
193-
.credential(new AzureKeyCredential(key))
194-
.endpoint(endpoint)
195-
.buildClient();
196-
197-
String documentUrl = "https://raw.githubusercontent.com/Azure-Samples/cognitive-services-REST-api-samples/master/curl/form-recognizer/sample-layout.pdf";
198-
String modelId = "prebuilt-document";
199-
SyncPoller < DocumentOperationResult, AnalyzeResult> analyzeDocumentPoller =
200-
client.beginAnalyzeDocumentFromUrl(modelId, documentUrl);
201-
202-
AnalyzeResult analyzeResult = analyzeDocumentPoller.getFinalResult();
203-
204-
// pages
205-
analyzeResult.getPages().forEach(documentPage -> {
206-
System.out.printf("Page has width: %.2f and height: %.2f, measured with unit: %s%n",
207-
documentPage.getWidth(),
208-
documentPage.getHeight(),
209-
documentPage.getUnit());
210-
211-
// lines
212-
documentPage.getLines().forEach(documentLine ->
213-
System.out.printf("Line %s is within a bounding box %s.%n",
214-
documentLine.getContent(),
215-
documentLine.getBoundingBox().toString()));
152+
import com.azure.ai.formrecognizer.*;
153+
import com.azure.ai.formrecognizer.models.AnalyzeResult;
154+
import com.azure.ai.formrecognizer.models.DocumentLine;
155+
import com.azure.ai.formrecognizer.models.AnalyzedDocument;
156+
import com.azure.ai.formrecognizer.models.DocumentOperationResult;
157+
import com.azure.ai.formrecognizer.models.DocumentWord;
158+
import com.azure.ai.formrecognizer.models.DocumentTable;
159+
import com.azure.core.credential.AzureKeyCredential;
160+
import com.azure.core.util.polling.SyncPoller;
216161

217-
// words
218-
documentPage.getWords().forEach(documentWord ->
219-
System.out.printf("Word %s has a confidence score of %.2f%n.",
220-
documentWord.getContent(),
221-
documentWord.getConfidence()));
222-
});
162+
import java.util.List;
163+
import java.util.Arrays;
223164

224-
// tables
225-
List <DocumentTable> tables = analyzeResult.getTables();
226-
for (int i = 0; i < tables.size(); i++) {
227-
DocumentTable documentTable = tables.get(i);
228-
System.out.printf("Table %d has %d rows and %d columns.%n", i, documentTable.getRowCount(),
229-
documentTable.getColumnCount());
230-
documentTable.getCells().forEach(documentTableCell -> {
231-
System.out.printf("Cell '%s', has row index %d and column index %d.%n",
232-
documentTableCell.getContent(),
233-
documentTableCell.getRowIndex(), documentTableCell.getColumnIndex());
165+
public class FormRecognizer {
166+
167+
private static final String key = "PASTE_YOUR_FORM_RECOGNIZER_SUBSCRIPTION_KEY_HERE";
168+
private static final String endpoint = "PASTE_YOUR_FORM_RECOGNIZER_ENDPOINT_HERE";
169+
170+
public static void main(String[] args) {
171+
172+
DocumentAnalysisClient client = new DocumentAnalysisClientBuilder()
173+
.credential(new AzureKeyCredential(key))
174+
.endpoint(endpoint)
175+
.buildClient();
176+
177+
String documentUrl = "https://raw.githubusercontent.com/Azure-Samples/cognitive-services-REST-api-samples/master/curl/form-recognizer/sample-layout.pdf";
178+
String modelId = "prebuilt-document";
179+
SyncPoller < DocumentOperationResult, AnalyzeResult> analyzeDocumentPoller =
180+
client.beginAnalyzeDocumentFromUrl(modelId, documentUrl);
181+
182+
AnalyzeResult analyzeResult = analyzeDocumentPoller.getFinalResult();
183+
184+
// pages
185+
analyzeResult.getPages().forEach(documentPage -> {
186+
System.out.printf("Page has width: %.2f and height: %.2f, measured with unit: %s%n",
187+
documentPage.getWidth(),
188+
documentPage.getHeight(),
189+
documentPage.getUnit());
190+
191+
// lines
192+
documentPage.getLines().forEach(documentLine ->
193+
System.out.printf("Line %s is within a bounding box %s.%n",
194+
documentLine.getContent(),
195+
documentLine.getBoundingBox().toString()));
196+
197+
// words
198+
documentPage.getWords().forEach(documentWord ->
199+
System.out.printf("Word %s has a confidence score of %.2f%n.",
200+
documentWord.getContent(),
201+
documentWord.getConfidence()));
202+
});
203+
204+
// tables
205+
List <DocumentTable> tables = analyzeResult.getTables();
206+
for (int i = 0; i < tables.size(); i++) {
207+
DocumentTable documentTable = tables.get(i);
208+
System.out.printf("Table %d has %d rows and %d columns.%n", i, documentTable.getRowCount(),
209+
documentTable.getColumnCount());
210+
documentTable.getCells().forEach(documentTableCell -> {
211+
System.out.printf("Cell '%s', has row index %d and column index %d.%n",
212+
documentTableCell.getContent(),
213+
documentTableCell.getRowIndex(), documentTableCell.getColumnIndex());
214+
});
215+
System.out.println();
216+
}
217+
218+
// Entities
219+
analyzeResult.getEntities().forEach(documentEntity -> {
220+
System.out.printf("Entity category : %s, sub-category %s%n: ",
221+
documentEntity.getCategory(), documentEntity.getSubCategory());
222+
System.out.printf("Entity content: %s%n: ", documentEntity.getContent());
223+
System.out.printf("Entity confidence: %.2f%n", documentEntity.getConfidence());
224+
});
225+
226+
// Key-value pairs
227+
analyzeResult.getKeyValuePairs().forEach(documentKeyValuePair -> {
228+
System.out.printf("Key content: %s%n", documentKeyValuePair.getKey().getContent());
229+
System.out.printf("Key content bounding region: %s%n",
230+
documentKeyValuePair.getKey().getBoundingRegions().toString());
231+
232+
if (documentKeyValuePair.getValue() != null) {
233+
System.out.printf("Value content: %s%n", documentKeyValuePair.getValue().getContent());
234+
System.out.printf("Value content bounding region: %s%n", documentKeyValuePair.getValue().getBoundingRegions().toString());
235+
}
234236
});
235-
System.out.println();
236237
}
237-
238-
// Entities
239-
analyzeResult.getEntities().forEach(documentEntity -> {
240-
System.out.printf("Entity category : %s, sub-category %s%n: ",
241-
documentEntity.getCategory(), documentEntity.getSubCategory());
242-
System.out.printf("Entity content: %s%n: ", documentEntity.getContent());
243-
System.out.printf("Entity confidence: %.2f%n", documentEntity.getConfidence());
244-
});
245-
246-
// Key-value pairs
247-
analyzeResult.getKeyValuePairs().forEach(documentKeyValuePair -> {
248-
System.out.printf("Key content: %s%n", documentKeyValuePair.getKey().getContent());
249-
System.out.printf("Key content bounding region: %s%n",
250-
documentKeyValuePair.getKey().getBoundingRegions().toString());
251-
252-
if (documentKeyValuePair.getValue() != null) {
253-
System.out.printf("Value content: %s%n", documentKeyValuePair.getValue().getContent());
254-
System.out.printf("Value content bounding region: %s%n", documentKeyValuePair.getValue().getBoundingRegions().toString());
255-
}
256-
});
257238
}
258239
```
259240

241+
242+
260243
## Layout model
261244

262245
Extract text, selection marks, text styles, table structures, and bounding region coordinates from documents.

articles/applied-ai-services/form-recognizer/quickstarts/try-v3-python-sdk.md

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ ms.topic: quickstart
1010
ms.date: 03/15/2022
1111
ms.author: lajanuar
1212
recommendations: false
13-
ms.custom: ignite-fall-2021, mode-api
1413
---
1514
<!-- markdownlint-disable MD025 -->
1615
# Get started: Form Recognizer Python SDK v3.0 | Preview
@@ -65,7 +64,7 @@ pip install azure-ai-formrecognizer==3.2.0b3
6564
To interact with the Form Recognizer service, you'll need to create an instance of the `DocumentAnalysisClient` class. To do so, you'll create an `AzureKeyCredential` with your key from the Azure portal and a `DocumentAnalysisClient` instance with the `AzureKeyCredential` and your Form Recognizer `endpoint`.
6665

6766
1. Create a new Python file called **form_recognizer_quickstart.py** in your preferred editor or IDE.
68-
67+
6968
1. Open the **form_recognizer_quickstart.py** file and select one of the following code samples to copy and pasted into your application:
7069

7170
* [**General document**](#general-document-model)
@@ -90,7 +89,7 @@ Extract text, tables, structure, key-value pairs, and named entities from docume
9089
> * For simplicity, all the entity fields that the service returns are not shown here. To see the list of all supported fields and corresponding types, see our [General document](../concept-general-document.md#named-entity-recognition-ner-categories) concept page.
9190
9291
<!-- markdownlint-disable MD036 -->
93-
**Add the following sample code to your form_recognizer_quickstart.py application:**
92+
**Add the following code sample to your form_recognizer_quickstart.py application. Make sure you update the key and endpoint variables with values from your Form Recognizer instance in the Azure portal**
9493

9594
```python
9695

@@ -240,7 +239,7 @@ Extract text, selection marks, text styles, table structures, and bounding regio
240239
> * We've added the file URL value to the `formUrl` variable in the `analyze_layout` function.
241240
> * To analyze a given file at a URL, you'll use the `begin_analyze_document_from_url` method and pass in `prebuilt-layout` as the model Id. The returned value is a `result` object containing data about the submitted document.
242241
243-
**Add the following sample code to your form_recognizer_quickstart.py application:**
242+
**Add the following code sample to your form_recognizer_quickstart.py application. Make sure you update the key and endpoint variables with values from your Form Recognizer instance in the Azure portal**
244243

245244
```python
246245

@@ -371,7 +370,7 @@ Analyze and extract common fields from specific document types using a prebuilt
371370
> * To analyze a given file at a URI, you'll use the `beginAnalyzeDocuments` method and pass `PrebuiltModels.Invoice` as the model Id. The returned value is a `result` object containing data about the submitted document.
372371
> * For simplicity, all the key-value pairs that the service returns are not shown here. To see the list of all supported fields and corresponding types, see our [Invoice](../concept-invoice.md#field-extraction) concept page.
373372
374-
**Add the following sample code to your form_recognizer_quickstart.py application:**
373+
**Add the following code sample to your form_recognizer_quickstart.py application. Make sure you update the key and endpoint variables with values from your Form Recognizer instance in the Azure portal**
375374

376375
```python
377376
# import libraries
@@ -397,7 +396,7 @@ def format_bounding_box(bounding_box):
397396
def analyze_invoice():
398397

399398
invoiceUrl = "https://raw.githubusercontent.com/Azure-Samples/cognitive-services-REST-api-samples/master/curl/form-recognizer/sample-invoice.pdf"
400-
399+
401400
# create your `DocumentAnalysisClient` instance and `AzureKeyCredential` variable
402401
document_analysis_client = DocumentAnalysisClient(
403402
endpoint=endpoint, credential=AzureKeyCredential(key)

0 commit comments

Comments
 (0)