Skip to content

Commit c47075f

Browse files
authored
Adding AWS Comprehend example for ABAP SDK (#7619)
1 parent 387d729 commit c47075f

File tree

7 files changed

+198
-0
lines changed

7 files changed

+198
-0
lines changed

.doc_gen/metadata/comprehend_metadata.yaml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,14 @@ comprehend_DetectSentiment:
291291
snippet_tags:
292292
- python.example_code.comprehend.ComprehendDetect
293293
- python.example_code.comprehend.DetectSentiment
294+
SAP ABAP:
295+
versions:
296+
- sdk_version: 1
297+
github: sap-abap/services/cpd
298+
excerpts:
299+
- description:
300+
snippet_tags:
301+
- cpd.abapv1.detect_sentiment
294302
services:
295303
comprehend: {DetectSentiment}
296304
comprehend_DetectSyntax:

.tools/readmes/config.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,7 @@
197197
"service_folder_overrides": {
198198
"bedrock-runtime": "sap-abap/services/bdr",
199199
"bedrock-agent-runtime": "sap-abap/services/bdz",
200+
"comprehend": "sap-abap/services/cpd",
200201
"dynamodb": "sap-abap/services/dyn",
201202
},
202203
}

sap-abap/services/cpd/README.md

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
# Amazon Comprehend code examples for the SDK for SAP ABAP
2+
3+
## Overview
4+
5+
Shows how to use the AWS SDK for SAP ABAP to work with Amazon Comprehend.
6+
7+
<!--custom.overview.start-->
8+
<!--custom.overview.end-->
9+
10+
_Amazon Comprehend uses natural language processing (NLP) to extract insights about the content of documents without the need of any special preprocessing._
11+
12+
## ⚠ Important
13+
14+
* Running this code might result in charges to your AWS account. For more details, see [AWS Pricing](https://aws.amazon.com/pricing/) and [Free Tier](https://aws.amazon.com/free/).
15+
* Running the tests might result in charges to your AWS account.
16+
* We recommend that you grant your code least privilege. At most, grant only the minimum permissions required to perform the task. For more information, see [Grant least privilege](https://docs.aws.amazon.com/IAM/latest/UserGuide/best-practices.html#grant-least-privilege).
17+
* This code is not tested in every AWS Region. For more information, see [AWS Regional Services](https://aws.amazon.com/about-aws/global-infrastructure/regional-product-services).
18+
19+
<!--custom.important.start-->
20+
<!--custom.important.end-->
21+
22+
## Code examples
23+
24+
### Prerequisites
25+
26+
For prerequisites, see the [README](../../README.md#Prerequisites) in the `sap-abap` folder.
27+
28+
29+
<!--custom.prerequisites.start-->
30+
<!--custom.prerequisites.end-->
31+
32+
### Single actions
33+
34+
Code excerpts that show you how to call individual service functions.
35+
36+
- [DetectSentiment](zcl_aws1_cpd_actions.clas.abap#L32)
37+
38+
39+
<!--custom.examples.start-->
40+
<!--custom.examples.end-->
41+
42+
## Run the examples
43+
44+
### Instructions
45+
46+
47+
<!--custom.instructions.start-->
48+
<!--custom.instructions.end-->
49+
50+
51+
52+
### Tests
53+
54+
⚠ Running tests might result in charges to your AWS account.
55+
56+
57+
To find instructions for running these tests, see the [README](../../README.md#Tests)
58+
in the `sap-abap` folder.
59+
60+
61+
62+
<!--custom.tests.start-->
63+
<!--custom.tests.end-->
64+
65+
## Additional resources
66+
67+
- [Amazon Comprehend Developer Guide](https://docs.aws.amazon.com/comprehend/latest/dg/what-is.html)
68+
- [Amazon Comprehend API Reference](https://docs.aws.amazon.com/comprehend/latest/APIReference/welcome.html)
69+
- [SDK for SAP ABAP Amazon Comprehend reference](https://docs.aws.amazon.com/sdk-for-sap-abap/v1/api/latest/comprehend/index.html)
70+
71+
<!--custom.resources.start-->
72+
<!--custom.resources.end-->
73+
74+
---
75+
76+
Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
77+
78+
SPDX-License-Identifier: Apache-2.0
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<abapGit version="v1.0.0" serializer="LCL_OBJECT_DEVC" serializer_version="v1.0.0">
3+
<asx:abap xmlns:asx="http://www.sap.com/abapxml" version="1.0">
4+
<asx:values>
5+
<DEVC>
6+
<CTEXT>Package for Amazon Comprehend</CTEXT>
7+
</DEVC>
8+
</asx:values>
9+
</asx:abap>
10+
</abapGit>
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
" Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
" SPDX-License-Identifier: Apache-2.0
3+
4+
class ZCL_AWS1_CPD_ACTIONS definition
5+
public
6+
final
7+
create public .
8+
9+
public section.
10+
METHODS detectsentiment
11+
EXPORTING VALUE(oo_result) TYPE REF TO /aws1/cl_cpddetectsentimentrsp .
12+
protected section.
13+
private section.
14+
15+
ENDCLASS.
16+
17+
18+
19+
CLASS ZCL_AWS1_CPD_ACTIONS IMPLEMENTATION.
20+
21+
22+
METHOD detectsentiment.
23+
CONSTANTS cv_pfl TYPE /aws1/rt_profile_id VALUE 'ZCODE_DEMO'.
24+
25+
DATA(lo_session) = /aws1/cl_rt_session_aws=>create( cv_pfl ).
26+
DATA(lo_cpd) = /aws1/cl_cpd_factory=>create( lo_session ).
27+
28+
DATA(lv_text) = |I love unicorns!| .
29+
DATA(lv_language_code) = |en| .
30+
31+
32+
" snippet-start:[cpd.abapv1.detect_sentiment]
33+
TRY.
34+
oo_result = lo_cpd->detectsentiment(
35+
iv_languagecode = lv_language_code
36+
iv_text = lv_text
37+
).
38+
39+
MESSAGE |Detected sentiment: { oo_result->get_sentiment( ) }| TYPE 'I'.
40+
41+
CATCH /aws1/cx_cpdtextsizelmtexcdex INTO DATA(lo_cpdex) .
42+
MESSAGE 'The size of the input text exceeds the limit. Use a smaller document.' TYPE 'E'.
43+
44+
ENDTRY.
45+
" snippet-end:[cpd.abapv1.detect_sentiment]
46+
ENDMETHOD.
47+
ENDCLASS.
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
" Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
" SPDX-License-Identifier: Apache-2.0
3+
4+
CLASS ltc_zcl_aws1_cpd_actions DEFINITION FOR TESTING
5+
DURATION SHORT
6+
RISK LEVEL HARMLESS.
7+
8+
PRIVATE SECTION.
9+
DATA ao_cpd_actions TYPE REF TO zcl_aws1_cpd_actions.
10+
METHODS: detectsentiment FOR TESTING.
11+
ENDCLASS. "ltc_Zcl_Aws1_Cpd_Actions
12+
13+
14+
CLASS ltc_zcl_aws1_cpd_actions IMPLEMENTATION.
15+
16+
METHOD detectsentiment.
17+
ao_cpd_actions = NEW zcl_aws1_cpd_actions( ).
18+
DATA lo_output TYPE REF TO /aws1/cl_cpddetectsentimentrsp.
19+
DATA(lv_expected_output) = |POSITIVE|.
20+
21+
ao_cpd_actions->detectsentiment(
22+
IMPORTING
23+
oo_result = lo_output ).
24+
25+
DATA(lv_found) = abap_true.
26+
IF lo_output->has_sentiment( ) = abap_true.
27+
IF lo_output->ask_sentiment( ) = lv_expected_output.
28+
lv_found = abap_true.
29+
ENDIF.
30+
ENDIF.
31+
32+
cl_abap_unit_assert=>assert_true(
33+
act = lv_found
34+
msg = |Sentiment detection failed| ).
35+
ENDMETHOD.
36+
37+
ENDCLASS.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<abapGit version="v1.0.0" serializer="LCL_OBJECT_CLAS" serializer_version="v1.0.0">
3+
<asx:abap xmlns:asx="http://www.sap.com/abapxml" version="1.0">
4+
<asx:values>
5+
<VSEOCLASS>
6+
<CLSNAME>ZCL_AWS1_CPD_ACTIONS</CLSNAME>
7+
<LANGU>E</LANGU>
8+
<DESCRIPT>Comprehend Code Example</DESCRIPT>
9+
<STATE>1</STATE>
10+
<CLSCCINCL>X</CLSCCINCL>
11+
<FIXPT>X</FIXPT>
12+
<UNICODE>X</UNICODE>
13+
<WITH_UNIT_TESTS>X</WITH_UNIT_TESTS>
14+
</VSEOCLASS>
15+
</asx:values>
16+
</asx:abap>
17+
</abapGit>

0 commit comments

Comments
 (0)