|
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