From a2de2a8e737c5de348c777faf4b7f67a82d0dfea Mon Sep 17 00:00:00 2001 From: Jennifer Cao Date: Fri, 3 Oct 2025 18:47:19 +0000 Subject: [PATCH 1/7] Finish method def for comprehend example --- sap-abap/services/cpd/package.devc.xml | 10 ++++ .../cpd/zcl_aws1_cpd_actions.clas.abap | 46 +++++++++++++++++++ .../cpd/zcl_aws1_cpd_actions.clas.xml | 16 +++++++ 3 files changed, 72 insertions(+) create mode 100644 sap-abap/services/cpd/package.devc.xml create mode 100644 sap-abap/services/cpd/zcl_aws1_cpd_actions.clas.abap create mode 100644 sap-abap/services/cpd/zcl_aws1_cpd_actions.clas.xml diff --git a/sap-abap/services/cpd/package.devc.xml b/sap-abap/services/cpd/package.devc.xml new file mode 100644 index 00000000000..c5fef40c774 --- /dev/null +++ b/sap-abap/services/cpd/package.devc.xml @@ -0,0 +1,10 @@ + + + + + + Package for Amazon Comprehend + + + + diff --git a/sap-abap/services/cpd/zcl_aws1_cpd_actions.clas.abap b/sap-abap/services/cpd/zcl_aws1_cpd_actions.clas.abap new file mode 100644 index 00000000000..bf18905f20e --- /dev/null +++ b/sap-abap/services/cpd/zcl_aws1_cpd_actions.clas.abap @@ -0,0 +1,46 @@ +" Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +" SPDX-License-Identifier: Apache-2.0 + +class ZCL_AWS1_CPD_ACTIONS definition + public + final + create public . + +public section. +protected section. +private section. + METHODS detectsentiment + RETURNING VALUE(oo_result) TYPE REF TO /aws1/cl_cpddetectsentimentrsp . +ENDCLASS. + + + +CLASS ZCL_AWS1_CPD_ACTIONS IMPLEMENTATION. + + + METHOD detectsentiment. + CONSTANTS cv_pfl TYPE /aws1/rt_profile_id VALUE 'ZCODE_DEMO'. + + DATA(lo_session) = /aws1/cl_rt_session_aws=>create( cv_pfl ). + DATA(lo_cpd) = /aws1/cl_cpd_factory=>create( lo_session ). + + DATA(lv_text) = |I love unicorns!| . + DATA(lv_language_code) = |EN| . + + + " snippet-start:[cpd.abapv1.detect_sentiment] + TRY. + oo_result = lo_cpd->detectsentiment( + iv_languagecode = lv_language_code + iv_text = lv_text + ). + + MESSAGE 'Detected sentiment.' TYPE 'I'. + + CATCH /aws1/cx_cpdserverexc INTO DATA(lo_cpdex) . + MESSAGE 'CPD exception' TYPE 'E'. + + ENDTRY. + " snippet-end:[cpd.abapv1.detect_sentiment] + ENDMETHOD. +ENDCLASS. diff --git a/sap-abap/services/cpd/zcl_aws1_cpd_actions.clas.xml b/sap-abap/services/cpd/zcl_aws1_cpd_actions.clas.xml new file mode 100644 index 00000000000..fc7e4be8e89 --- /dev/null +++ b/sap-abap/services/cpd/zcl_aws1_cpd_actions.clas.xml @@ -0,0 +1,16 @@ + + + + + + ZCL_AWS1_CPD_ACTIONS + E + Comprehend Code Example + 1 + X + X + X + + + + From 4ba9dd8f563cd8affa361d0c4d92e8833d52e1f4 Mon Sep 17 00:00:00 2001 From: Jennifer Cao Date: Fri, 3 Oct 2025 18:55:40 +0000 Subject: [PATCH 2/7] Activating the class --- ...zcl_aws1_cpd_actions.clas.testclasses.abap | 25 +++++++++++++++++++ .../cpd/zcl_aws1_cpd_actions.clas.xml | 1 + 2 files changed, 26 insertions(+) create mode 100644 sap-abap/services/cpd/zcl_aws1_cpd_actions.clas.testclasses.abap diff --git a/sap-abap/services/cpd/zcl_aws1_cpd_actions.clas.testclasses.abap b/sap-abap/services/cpd/zcl_aws1_cpd_actions.clas.testclasses.abap new file mode 100644 index 00000000000..6395a1c9fc5 --- /dev/null +++ b/sap-abap/services/cpd/zcl_aws1_cpd_actions.clas.testclasses.abap @@ -0,0 +1,25 @@ + +CLASS ltc_zcl_aws1_cpd_actions DEFINITION FOR TESTING + DURATION SHORT + RISK LEVEL HARMLESS. + + PRIVATE SECTION. + DATA: + f_cut TYPE REF TO zcl_aws1_cpd_actions. "class under test + + METHODS: detectsentiment FOR TESTING. +ENDCLASS. "ltc_Zcl_Aws1_Cpd_Actions + + +CLASS ltc_zcl_aws1_cpd_actions IMPLEMENTATION. + + METHOD detectsentiment. + + + + ENDMETHOD. + + + + +ENDCLASS. diff --git a/sap-abap/services/cpd/zcl_aws1_cpd_actions.clas.xml b/sap-abap/services/cpd/zcl_aws1_cpd_actions.clas.xml index fc7e4be8e89..cf647da1aa3 100644 --- a/sap-abap/services/cpd/zcl_aws1_cpd_actions.clas.xml +++ b/sap-abap/services/cpd/zcl_aws1_cpd_actions.clas.xml @@ -10,6 +10,7 @@ X X X + X From 553d9f03fc0fd65f475ec9b29342f24948016b22 Mon Sep 17 00:00:00 2001 From: Jennifer Cao Date: Fri, 3 Oct 2025 20:27:10 +0000 Subject: [PATCH 3/7] added unit test for detect sentiment --- ...zcl_aws1_cpd_actions.clas.testclasses.abap | 29 ++++++++++++++----- 1 file changed, 21 insertions(+), 8 deletions(-) diff --git a/sap-abap/services/cpd/zcl_aws1_cpd_actions.clas.testclasses.abap b/sap-abap/services/cpd/zcl_aws1_cpd_actions.clas.testclasses.abap index 6395a1c9fc5..9d5fbd3d19c 100644 --- a/sap-abap/services/cpd/zcl_aws1_cpd_actions.clas.testclasses.abap +++ b/sap-abap/services/cpd/zcl_aws1_cpd_actions.clas.testclasses.abap @@ -1,11 +1,13 @@ +" Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +" SPDX-License-Identifier: Apache-2.0 CLASS ltc_zcl_aws1_cpd_actions DEFINITION FOR TESTING DURATION SHORT RISK LEVEL HARMLESS. PRIVATE SECTION. - DATA: - f_cut TYPE REF TO zcl_aws1_cpd_actions. "class under test + CONSTANTS cv_pfl TYPE /aws1/rt_profile_id VALUE 'ZCODE_DEMO'. + CONSTANTS cv_languagecode VALUE |EN|. METHODS: detectsentiment FOR TESTING. ENDCLASS. "ltc_Zcl_Aws1_Cpd_Actions @@ -14,12 +16,23 @@ ENDCLASS. "ltc_Zcl_Aws1_Cpd_Actions CLASS ltc_zcl_aws1_cpd_actions IMPLEMENTATION. METHOD detectsentiment. - - - + DATA lo_output TYPE REF TO /aws1/cl_cpddetectsentimentrsp. + DATA(lv_expected_output) = |POSITIVE|. + + ao_cpd_actions->detectsentiment( + IMPORTING + oo_result = lo_output ). + + DATA(lv_found) = abap_true. + IF lo_output->has_sentiment() = abap_true. + IF lo_output->get_sentiment() = lv_expected_output. + lv_found = abap_true. + ENDIF. + ENDIF. + + cl_abap_unit_assert=>assert_true( + act = lv_found + msg = |Sentiment detection failed| ). ENDMETHOD. - - - ENDCLASS. From 4af5d4c5c1145a3f9758ad2861631c5e22ea39f4 Mon Sep 17 00:00:00 2001 From: Jennifer Cao Date: Mon, 6 Oct 2025 16:32:08 +0000 Subject: [PATCH 4/7] Unit tests passing now Changed the call to ask_sentiment and fixed class declaration issues in unit test --- sap-abap/services/cpd/zcl_aws1_cpd_actions.clas.abap | 9 +++++---- .../cpd/zcl_aws1_cpd_actions.clas.testclasses.abap | 9 ++++----- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/sap-abap/services/cpd/zcl_aws1_cpd_actions.clas.abap b/sap-abap/services/cpd/zcl_aws1_cpd_actions.clas.abap index bf18905f20e..97cf34d7b20 100644 --- a/sap-abap/services/cpd/zcl_aws1_cpd_actions.clas.abap +++ b/sap-abap/services/cpd/zcl_aws1_cpd_actions.clas.abap @@ -7,10 +7,11 @@ class ZCL_AWS1_CPD_ACTIONS definition create public . public section. + METHODS detectsentiment + EXPORTING VALUE(oo_result) TYPE REF TO /aws1/cl_cpddetectsentimentrsp . protected section. private section. - METHODS detectsentiment - RETURNING VALUE(oo_result) TYPE REF TO /aws1/cl_cpddetectsentimentrsp . + ENDCLASS. @@ -25,7 +26,7 @@ CLASS ZCL_AWS1_CPD_ACTIONS IMPLEMENTATION. DATA(lo_cpd) = /aws1/cl_cpd_factory=>create( lo_session ). DATA(lv_text) = |I love unicorns!| . - DATA(lv_language_code) = |EN| . + DATA(lv_language_code) = |en| . " snippet-start:[cpd.abapv1.detect_sentiment] @@ -38,7 +39,7 @@ CLASS ZCL_AWS1_CPD_ACTIONS IMPLEMENTATION. MESSAGE 'Detected sentiment.' TYPE 'I'. CATCH /aws1/cx_cpdserverexc INTO DATA(lo_cpdex) . - MESSAGE 'CPD exception' TYPE 'E'. + MESSAGE 'The size of the input text exceeds the limit. Use a smaller document.' TYPE 'E'. ENDTRY. " snippet-end:[cpd.abapv1.detect_sentiment] diff --git a/sap-abap/services/cpd/zcl_aws1_cpd_actions.clas.testclasses.abap b/sap-abap/services/cpd/zcl_aws1_cpd_actions.clas.testclasses.abap index 9d5fbd3d19c..29af606d68b 100644 --- a/sap-abap/services/cpd/zcl_aws1_cpd_actions.clas.testclasses.abap +++ b/sap-abap/services/cpd/zcl_aws1_cpd_actions.clas.testclasses.abap @@ -6,9 +6,7 @@ CLASS ltc_zcl_aws1_cpd_actions DEFINITION FOR TESTING RISK LEVEL HARMLESS. PRIVATE SECTION. - CONSTANTS cv_pfl TYPE /aws1/rt_profile_id VALUE 'ZCODE_DEMO'. - CONSTANTS cv_languagecode VALUE |EN|. - + DATA ao_cpd_actions TYPE REF TO zcl_aws1_cpd_actions. METHODS: detectsentiment FOR TESTING. ENDCLASS. "ltc_Zcl_Aws1_Cpd_Actions @@ -16,6 +14,7 @@ ENDCLASS. "ltc_Zcl_Aws1_Cpd_Actions CLASS ltc_zcl_aws1_cpd_actions IMPLEMENTATION. METHOD detectsentiment. + ao_cpd_actions = NEW zcl_aws1_cpd_actions( ). DATA lo_output TYPE REF TO /aws1/cl_cpddetectsentimentrsp. DATA(lv_expected_output) = |POSITIVE|. @@ -24,8 +23,8 @@ CLASS ltc_zcl_aws1_cpd_actions IMPLEMENTATION. oo_result = lo_output ). DATA(lv_found) = abap_true. - IF lo_output->has_sentiment() = abap_true. - IF lo_output->get_sentiment() = lv_expected_output. + IF lo_output->has_sentiment( ) = abap_true. + IF lo_output->ask_sentiment( ) = lv_expected_output. lv_found = abap_true. ENDIF. ENDIF. From 5c556f40ea2d567f01dc2073023da06dd823290d Mon Sep 17 00:00:00 2001 From: Jennifer Cao Date: Mon, 6 Oct 2025 16:51:15 +0000 Subject: [PATCH 5/7] added metadata for code example --- .doc_gen/metadata/comprehend_metadata.yaml | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/.doc_gen/metadata/comprehend_metadata.yaml b/.doc_gen/metadata/comprehend_metadata.yaml index b2590b3d34f..f8a44b912f8 100644 --- a/.doc_gen/metadata/comprehend_metadata.yaml +++ b/.doc_gen/metadata/comprehend_metadata.yaml @@ -291,6 +291,14 @@ comprehend_DetectSentiment: snippet_tags: - python.example_code.comprehend.ComprehendDetect - python.example_code.comprehend.DetectSentiment + SAP ABAP: + versions: + - sdk_version: 1 + github: sap-abap/services/cpd + excerpts: + - description: + snippet_tags: + - cpd.abapv1.detect_sentiment services: comprehend: {DetectSentiment} comprehend_DetectSyntax: From 7387ebf763371038fa78f32235887b96e9c2c6b2 Mon Sep 17 00:00:00 2001 From: Jennifer Cao Date: Mon, 6 Oct 2025 18:41:27 +0000 Subject: [PATCH 6/7] Improve usage example and fix wrong exception --- sap-abap/services/cpd/zcl_aws1_cpd_actions.clas.abap | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/sap-abap/services/cpd/zcl_aws1_cpd_actions.clas.abap b/sap-abap/services/cpd/zcl_aws1_cpd_actions.clas.abap index 97cf34d7b20..5b0de5e3f2b 100644 --- a/sap-abap/services/cpd/zcl_aws1_cpd_actions.clas.abap +++ b/sap-abap/services/cpd/zcl_aws1_cpd_actions.clas.abap @@ -36,9 +36,9 @@ CLASS ZCL_AWS1_CPD_ACTIONS IMPLEMENTATION. iv_text = lv_text ). - MESSAGE 'Detected sentiment.' TYPE 'I'. + MESSAGE |Detected sentiment: { oo_result->get_sentiment( ) }| TYPE 'I'. - CATCH /aws1/cx_cpdserverexc INTO DATA(lo_cpdex) . + CATCH /aws1/cx_cpdtextsizelmtexcdex INTO DATA(lo_cpdex) . MESSAGE 'The size of the input text exceeds the limit. Use a smaller document.' TYPE 'E'. ENDTRY. From e8a4cd3aa1f860659a58f2d54b127d5881ffe4a8 Mon Sep 17 00:00:00 2001 From: Jennifer Cao Date: Mon, 13 Oct 2025 18:57:10 +0000 Subject: [PATCH 7/7] fixing generation and generated readme --- .tools/readmes/config.py | 1 + sap-abap/services/cpd/README.md | 78 +++++++++++++++++++++++++++++++++ 2 files changed, 79 insertions(+) create mode 100644 sap-abap/services/cpd/README.md diff --git a/.tools/readmes/config.py b/.tools/readmes/config.py index e6e215717d8..35bd076a6ac 100644 --- a/.tools/readmes/config.py +++ b/.tools/readmes/config.py @@ -197,6 +197,7 @@ "service_folder_overrides": { "bedrock-runtime": "sap-abap/services/bdr", "bedrock-agent-runtime": "sap-abap/services/bdz", + "comprehend": "sap-abap/services/cpd", "dynamodb": "sap-abap/services/dyn", }, } diff --git a/sap-abap/services/cpd/README.md b/sap-abap/services/cpd/README.md new file mode 100644 index 00000000000..8bf7adbf17a --- /dev/null +++ b/sap-abap/services/cpd/README.md @@ -0,0 +1,78 @@ +# Amazon Comprehend code examples for the SDK for SAP ABAP + +## Overview + +Shows how to use the AWS SDK for SAP ABAP to work with Amazon Comprehend. + + + + +_Amazon Comprehend uses natural language processing (NLP) to extract insights about the content of documents without the need of any special preprocessing._ + +## ⚠ Important + +* 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/). +* Running the tests might result in charges to your AWS account. +* 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). +* 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). + + + + +## Code examples + +### Prerequisites + +For prerequisites, see the [README](../../README.md#Prerequisites) in the `sap-abap` folder. + + + + + +### Single actions + +Code excerpts that show you how to call individual service functions. + +- [DetectSentiment](zcl_aws1_cpd_actions.clas.abap#L32) + + + + + +## Run the examples + +### Instructions + + + + + + + +### Tests + +⚠ Running tests might result in charges to your AWS account. + + +To find instructions for running these tests, see the [README](../../README.md#Tests) +in the `sap-abap` folder. + + + + + + +## Additional resources + +- [Amazon Comprehend Developer Guide](https://docs.aws.amazon.com/comprehend/latest/dg/what-is.html) +- [Amazon Comprehend API Reference](https://docs.aws.amazon.com/comprehend/latest/APIReference/welcome.html) +- [SDK for SAP ABAP Amazon Comprehend reference](https://docs.aws.amazon.com/sdk-for-sap-abap/v1/api/latest/comprehend/index.html) + + + + +--- + +Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. + +SPDX-License-Identifier: Apache-2.0