Skip to content

Commit 86a84ee

Browse files
authored
Merge pull request #1474 from scmacdon/master
Updated the Comprehend and EventBridge Java V2 examples
2 parents aac5017 + 5c9eac1 commit 86a84ee

File tree

20 files changed

+1213
-1327
lines changed

20 files changed

+1213
-1327
lines changed

javav2/example_code/comprehend/Readme.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ For systems with Bash support, once you set the **CLASSPATH**, you can run a par
2323

2424
## Testing the Amazon Comprehend files
2525

26-
You can test the Java code examples for Amazon Comprehend by running a test file named **AmazonComprehendServiceIntegrationTest**. This file uses JUnit 5 to run the JUnit tests and is located in the **src/test/java** folder. For more information, see [https://junit.org/junit5/](https://junit.org/junit5/).
26+
You can test the Java code examples for Amazon Comprehend by running a test file named **AmazonComprehendTest**. This file uses JUnit 5 to run the JUnit tests and is located in the **src/test/java** folder. For more information, see [https://junit.org/junit5/](https://junit.org/junit5/).
2727

2828
You can run the JUnit tests from a Java IDE, such as IntelliJ, or from the command line by using Maven. As each test is run, you can view messages that inform you if the various tests succeed or fail. For example, the following message informs you that Test 3 passed.
2929

@@ -50,7 +50,7 @@ You will see output from the JUnit tests, as shown here.
5050
[INFO] -------------------------------------------------------
5151
[INFO] T E S T S
5252
[INFO] -------------------------------------------------------
53-
[INFO] Running AmazonComprehendServiceIntegrationTest
53+
[INFO] Running AmazonComprehendTest
5454
Test 1 passed
5555
Test 2 passed
5656
...

javav2/example_code/comprehend/metadata.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,6 @@ files:
2424
- path: src/main/java/com/example/comprehend/medical/DetectPHI.java
2525
services:
2626
- comprehend
27-
- path: src/test/java/AmazonComprehendServiceIntegrationTest.java
27+
- path: src/test/java/AmazonComprehendTest.java
2828
services:
2929
- comprehend
Lines changed: 66 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -1,78 +1,66 @@
1-
// snippet-sourcedescription:[DetectEntities demonstrates how to retrieve named entities.]
2-
// snippet-service:[Amazon Comprehend]
3-
// snippet-keyword:[Java]
4-
// snippet-sourcesyntax:[java]
5-
// snippet-keyword:[Amazon Comprehend]
6-
// snippet-keyword:[Code Sample]
7-
// snippet-sourcetype:[full-example]
8-
// snippet-sourcedate:[6/3/2020]
9-
// snippet-sourceauthor:[scmacdon AWS]
10-
11-
12-
/**
13-
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
14-
*
15-
* This file is licensed under the Apache License, Version 2.0 (the "License").
16-
* You may not use this file except in compliance with the License. A copy of
17-
* the License is located at
18-
*
19-
* http://aws.amazon.com/apache2.0/
20-
*
21-
* This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
22-
* CONDITIONS OF ANY KIND, either express or implied. See the License for the
23-
* specific language governing permissions and limitations under the License.
24-
*/
25-
26-
package com.example.comprehend;
27-
28-
//snippet-start:[comprehend.java2.detect_entities.import]
29-
import software.amazon.awssdk.regions.Region;
30-
import software.amazon.awssdk.services.comprehend.ComprehendClient;
31-
import software.amazon.awssdk.services.comprehend.model.DetectEntitiesRequest;
32-
import software.amazon.awssdk.services.comprehend.model.DetectEntitiesResponse;
33-
import software.amazon.awssdk.services.comprehend.model.Entity;
34-
import software.amazon.awssdk.services.comprehend.model.ComprehendException;
35-
import java.util.Iterator;
36-
import java.util.List;
37-
//snippet-end:[comprehend.java2.detect_entities.import]
38-
39-
40-
public class DetectEntities {
41-
42-
public static void main(String[] args) {
43-
44-
String text = "Amazon.com, Inc. is located in Seattle, WA and was founded July 5, 1994 by Jeff Bezos, enabling 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.";
45-
Region region = Region.US_EAST_1;
46-
ComprehendClient comClient = ComprehendClient.builder()
47-
.region(region)
48-
.build();
49-
50-
System.out.println("Calling DetectEntities");
51-
detectAllEntities(comClient, text);
52-
}
53-
54-
//snippet-start:[comprehend.java2.detect_entities.main]
55-
public static void detectAllEntities(ComprehendClient comClient,String text ) {
56-
57-
try {
58-
DetectEntitiesRequest detectEntitiesRequest = DetectEntitiesRequest.builder()
59-
.text(text)
60-
.languageCode("en")
61-
.build();
62-
63-
DetectEntitiesResponse detectEntitiesResult = comClient.detectEntities(detectEntitiesRequest);
64-
List<Entity> entList = detectEntitiesResult.entities();
65-
Iterator<Entity> lanIterator = entList.iterator();
66-
67-
while (lanIterator.hasNext()) {
68-
Entity entity = lanIterator.next();
69-
System.out.println("Entity text is " + entity.text());
70-
}
71-
72-
} catch (ComprehendException e) {
73-
System.err.println(e.awsErrorDetails().errorMessage());
74-
System.exit(1);
75-
}
76-
//snippet-end:[comprehend.java2.detect_entities.main]
77-
}
78-
}
1+
// snippet-sourcedescription:[DetectEntities demonstrates how to retrieve named entities.]
2+
//snippet-keyword:[AWS SDK for Java v2]
3+
// snippet-service:[Amazon Comprehend]
4+
// snippet-keyword:[Code Sample]
5+
// snippet-sourcetype:[full-example]
6+
// snippet-sourcedate:[11/04/2020]
7+
// snippet-sourceauthor:[scmacdon - AWS]
8+
9+
/*
10+
Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
11+
SPDX-License-Identifier: Apache-2.0
12+
*/
13+
14+
package com.example.comprehend;
15+
16+
//snippet-start:[comprehend.java2.detect_entities.import]
17+
import software.amazon.awssdk.regions.Region;
18+
import software.amazon.awssdk.services.comprehend.ComprehendClient;
19+
import software.amazon.awssdk.services.comprehend.model.DetectEntitiesRequest;
20+
import software.amazon.awssdk.services.comprehend.model.DetectEntitiesResponse;
21+
import software.amazon.awssdk.services.comprehend.model.Entity;
22+
import software.amazon.awssdk.services.comprehend.model.ComprehendException;
23+
import java.util.Iterator;
24+
import java.util.List;
25+
//snippet-end:[comprehend.java2.detect_entities.import]
26+
27+
public class DetectEntities {
28+
29+
public static void main(String[] args) {
30+
31+
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.";
32+
Region region = Region.US_EAST_1;
33+
ComprehendClient comClient = ComprehendClient.builder()
34+
.region(region)
35+
.build();
36+
37+
System.out.println("Calling DetectEntities");
38+
detectAllEntities(comClient, text);
39+
comClient.close();
40+
}
41+
42+
//snippet-start:[comprehend.java2.detect_entities.main]
43+
public static void detectAllEntities(ComprehendClient comClient,String text ) {
44+
45+
try {
46+
DetectEntitiesRequest detectEntitiesRequest = DetectEntitiesRequest.builder()
47+
.text(text)
48+
.languageCode("en")
49+
.build();
50+
51+
DetectEntitiesResponse detectEntitiesResult = comClient.detectEntities(detectEntitiesRequest);
52+
List<Entity> entList = detectEntitiesResult.entities();
53+
Iterator<Entity> lanIterator = entList.iterator();
54+
55+
while (lanIterator.hasNext()) {
56+
Entity entity = lanIterator.next();
57+
System.out.println("Entity text is " + entity.text());
58+
}
59+
60+
} catch (ComprehendException e) {
61+
System.err.println(e.awsErrorDetails().errorMessage());
62+
System.exit(1);
63+
}
64+
//snippet-end:[comprehend.java2.detect_entities.main]
65+
}
66+
}
Lines changed: 68 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -1,77 +1,68 @@
1-
// snippet-sourcedescription:[DetectKeyPhrases demonstrates how to detect key phrases.]
2-
// snippet-service:[Amazon Comprehend]
3-
// snippet-keyword:[Java]
4-
// snippet-sourcesyntax:[java]
5-
// snippet-keyword:[Amazon Comprehend]
6-
// snippet-keyword:[Code Sample]
7-
// snippet-sourcetype:[full-example]
8-
// snippet-sourcedate:[6/3/2020]
9-
// snippet-sourceauthor:[scmacdon AWS]
10-
11-
/**
12-
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
13-
*
14-
* This file is licensed under the Apache License, Version 2.0 (the "License").
15-
* You may not use this file except in compliance with the License. A copy of
16-
* the License is located at
17-
*
18-
* http://aws.amazon.com/apache2.0/
19-
*
20-
* This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
21-
* CONDITIONS OF ANY KIND, either express or implied. See the License for the
22-
* specific language governing permissions and limitations under the License.
23-
*/
24-
25-
package com.example.comprehend;
26-
27-
//snippet-start:[comprehend.java2.detect_keyphrases.import]
28-
import software.amazon.awssdk.regions.Region;
29-
import software.amazon.awssdk.services.comprehend.ComprehendClient;
30-
import software.amazon.awssdk.services.comprehend.model.DetectKeyPhrasesRequest;
31-
import software.amazon.awssdk.services.comprehend.model.DetectKeyPhrasesResponse;
32-
import software.amazon.awssdk.services.comprehend.model.KeyPhrase;
33-
import software.amazon.awssdk.services.comprehend.model.ComprehendException;
34-
import java.util.Iterator;
35-
import java.util.List;
36-
//snippet-end:[comprehend.java2.detect_keyphrases.import]
37-
38-
public class DetectKeyPhrases {
39-
40-
public static void main(String[] args) {
41-
42-
String text = "Amazon.com, Inc. is located in Seattle, WA and was founded July 5, 1994 by Jeff Bezos, enabling 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.";
43-
Region region = Region.US_EAST_1;
44-
ComprehendClient comClient = ComprehendClient.builder()
45-
.region(region)
46-
.build();
47-
48-
System.out.println("Calling DetectKeyPhrases");
49-
detectAllKeyPhrases(comClient, text);
50-
}
51-
52-
//snippet-start:[comprehend.java2.detect_keyphrases.main]
53-
public static void detectAllKeyPhrases(ComprehendClient comClient, String text) {
54-
55-
try {
56-
DetectKeyPhrasesRequest detectKeyPhrasesRequest = DetectKeyPhrasesRequest.builder()
57-
.text(text)
58-
.languageCode("en")
59-
.build();
60-
61-
DetectKeyPhrasesResponse detectKeyPhrasesResult = comClient.detectKeyPhrases(detectKeyPhrasesRequest);
62-
63-
List<KeyPhrase> phraseList = detectKeyPhrasesResult.keyPhrases();
64-
Iterator<KeyPhrase> keyIterator = phraseList.iterator();
65-
66-
while (keyIterator.hasNext()) {
67-
KeyPhrase keyPhrase = keyIterator.next();
68-
System.out.println("Key phrase text is " + keyPhrase.text());
69-
}
70-
71-
} catch (ComprehendException e) {
72-
System.err.println(e.awsErrorDetails().errorMessage());
73-
System.exit(1);
74-
}
75-
//snippet-end:[comprehend.java2.detect_keyphrases.main]
76-
}
77-
}
1+
// snippet-sourcedescription:[DetectKeyPhrases demonstrates how to detect key phrases.]
2+
//snippet-keyword:[AWS SDK for Java v2]
3+
// snippet-service:[Amazon Comprehend]
4+
// snippet-keyword:[Code Sample]
5+
// snippet-sourcetype:[full-example]
6+
// snippet-sourcedate:[11/04/2020]
7+
// snippet-sourceauthor:[scmacdon - AWS]
8+
9+
/*
10+
Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
11+
SPDX-License-Identifier: Apache-2.0
12+
*/
13+
14+
package com.example.comprehend;
15+
16+
//snippet-start:[comprehend.java2.detect_keyphrases.import]
17+
import software.amazon.awssdk.regions.Region;
18+
import software.amazon.awssdk.services.comprehend.ComprehendClient;
19+
import software.amazon.awssdk.services.comprehend.model.DetectKeyPhrasesRequest;
20+
import software.amazon.awssdk.services.comprehend.model.DetectKeyPhrasesResponse;
21+
import software.amazon.awssdk.services.comprehend.model.KeyPhrase;
22+
import software.amazon.awssdk.services.comprehend.model.ComprehendException;
23+
import java.util.Iterator;
24+
import java.util.List;
25+
//snippet-end:[comprehend.java2.detect_keyphrases.import]
26+
27+
public class DetectKeyPhrases {
28+
29+
public static void main(String[] args) {
30+
31+
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.";
32+
Region region = Region.US_EAST_1;
33+
ComprehendClient comClient = ComprehendClient.builder()
34+
.region(region)
35+
.build();
36+
37+
System.out.println("Calling DetectKeyPhrases");
38+
detectAllKeyPhrases(comClient, text);
39+
comClient.close();
40+
}
41+
42+
//snippet-start:[comprehend.java2.detect_keyphrases.main]
43+
public static void detectAllKeyPhrases(ComprehendClient comClient, String text) {
44+
45+
try {
46+
DetectKeyPhrasesRequest detectKeyPhrasesRequest = DetectKeyPhrasesRequest.builder()
47+
.text(text)
48+
.languageCode("en")
49+
.build();
50+
51+
DetectKeyPhrasesResponse detectKeyPhrasesResult = comClient.detectKeyPhrases(detectKeyPhrasesRequest);
52+
53+
List<KeyPhrase> phraseList = detectKeyPhrasesResult.keyPhrases();
54+
Iterator<KeyPhrase> keyIterator = phraseList.iterator();
55+
56+
while (keyIterator.hasNext()) {
57+
KeyPhrase keyPhrase = keyIterator.next();
58+
System.out.println("Key phrase text is " + keyPhrase.text());
59+
}
60+
61+
} catch (ComprehendException e) {
62+
System.err.println(e.awsErrorDetails().errorMessage());
63+
System.exit(1);
64+
}
65+
//snippet-end:[comprehend.java2.detect_keyphrases.main]
66+
}
67+
}
68+

0 commit comments

Comments
 (0)