当前位置:网站首页>A Sina Weibo semantic sentiment analysis tool developed by ABAP

A Sina Weibo semantic sentiment analysis tool developed by ABAP

2022-08-10 15:33:00 Wang Zixi

ABAP Report appearance:

在这里插入图片描述

ZWEIBO_01 的源代码:

*&---------------------------------------------------------------------*
*&  Include           ZWEIBO_01
*&---------------------------------------------------------------------*

TYPES: BEGIN OF ty_result,
          name TYPE string,
          value TYPE string,
       END OF ty_result.

TYPES: tt_result TYPE STANDARD TABLE OF ty_result.

class lcl_analyzer DEFINITION create private.
   PUBLIC SECTION.
      METHODS: analyze IMPORTING iv_company_name TYPE string iv_text TYPE string EXPORTING ev_result TYPE string.
      CLASS-METHODS:get_instance  returning value(RO_INSTANCE) type ref to lcl_analyzer.

   PRIVATE SECTION.
      class-data mo_instance type ref to lcl_analyzer .
      data: mr_http_client TYPE REF TO if_http_client.
      CONSTANTS: gc_baseurl TYPE string VALUE 'http://xxx:9000/service_weibo/analysis?q='.

      METHODS: constructor,
               sendrequest IMPORTING iv_company_name TYPE string iv_text TYPE string EXPORTING ev_text TYPE string.

ENDCLASS.

class lcl_analyzer IMPLEMENTATION.
   method analyze.
      sendrequest( EXPORTING iv_company_name = iv_company_name iv_text = iv_text IMPORTING ev_text = ev_result ).

   ENDMETHOD.

   method constructor.
   endmethod.

   method sendrequest.
     DATA: lv_url TYPE string,
           lv_sysubrc TYPE  sysubrc,
           lv_error TYPE string,
           lv_http_code TYPE i.

     lv_url = gc_baseurl && cl_http_utility=>escape_url( iv_text ).

     CALL METHOD cl_http_client=>create_by_url
      EXPORTING
        url                = lv_url
        "proxy_host = 'PROXY' "proxy_service      = '8080'
        "ssl_id             = 'ANONYM'
      IMPORTING
        client             = mr_http_client
      EXCEPTIONS
        argument_not_found = 1
        plugin_not_active  = 2
        internal_error     = 3
        OTHERS             = 4.

    CHECK sy-subrc = 0.

    CALL METHOD mr_http_client->request->set_method( 'GET' ).

    CALL METHOD mr_http_client->send
      EXCEPTIONS
        http_communication_failure = 1
        http_invalid_state         = 2
        http_processing_failed     = 3.

    IF sy-subrc <> 0.
      CALL METHOD mr_http_client->get_last_error
        IMPORTING
          code    = lv_sysubrc
          message = lv_error.

      mr_http_client->close( ).
      RETURN.
    ENDIF.

    CALL METHOD mr_http_client->receive
      EXCEPTIONS
        http_communication_failure = 1
        http_invalid_state         = 2
        http_processing_failed     = 3.

    IF sy-subrc <> 0.
      CALL METHOD mr_http_client->get_last_error
        IMPORTING
          code    = lv_sysubrc
          message = lv_error.

      mr_http_client->close( ).
      RETURN.
    ENDIF.

    ev_text = mr_http_client->response->get_cdata( ).
    mr_http_client->close( ).
   endmethod.

   method get_instance.
      IF mo_instance IS NOT BOUND.
         CREATE OBJECT mo_instance.
       ENDIF.

      ro_instance = mo_instance.
   endmethod.

ENDCLASS.

CLASS lcl_weibo_tool DEFINITION.

  PUBLIC SECTION.

          METHODS: send FOR EVENT FUNCTION_SELECTED OF CL_GUI_TOOLBAR  importing !FCODE,
                   display IMPORTING iv_text TYPE string,
                   constructor IMPORTING iv_editor TYPE REF TO cl_gui_textedit iv_alv TYPE REF TO CL_GUI_ALV_GRID,
                   set_table CHANGING ct_table TYPE tt_result,
                   get_formatted_string IMPORTING it_table TYPE any table EXPORTING ev_text TYPE string.
  PRIVATE SECTION.
         TYPES: BEGIN OF mytable_line,
           line(1024) TYPE c,
         END OF mytable_line.

         TYPES: tt_mytable TYPE TABLE OF mytable_line.

          DATA: lt_mytable TYPE tt_mytable.
          DATA: mr_editor TYPE REF TO cl_gui_textedit,
                mr_alv TYPE REF TO CL_GUI_ALV_GRID,
                mr_result TYPE REF TO data.
          CONSTANTS: gc_filter TYPE string VALUE '<br>'.


ENDCLASS.

CLASS lcl_weibo_tool IMPLEMENTATION.
   METHOD send.

      DATA: lv_text TYPE string,
            ls_line TYPE mytable_line,
            lv_result TYPE string.

      CHECK fcode = 'CHECK'.
      CLEAR: lt_mytable,lv_text.
      cl_gui_cfw=>flush( ).

      mr_editor->get_text_as_stream( IMPORTING text = lt_mytable ).
      CHECK lt_mytable IS NOT INITIAL.
      get_formatted_string( EXPORTING it_table = lt_mytable IMPORTING ev_text = lv_text ).

      lcl_analyzer=>get_instance( )->analyze( EXPORTING iv_company_name = 'dummy' iv_text = lv_text
                                              IMPORTING ev_result = lv_result ).
      display( EXPORTING iv_text = lv_result ).

   ENDMETHOD.

   method set_table.
      GET REFERENCE OF ct_table INTO mr_result.
   endmethod.

   method display.
     FIELD-SYMBOLS: <table> TYPE tt_result.
     ASSIGN mr_result->* TO <table>.
     CLEAR: <table>.
     DATA: lt_raw TYPE string_table,
           lv_line TYPE string,
           lv_len TYPE i,
           ls_line TYPE ty_result.

     SPLIT iv_text AT gc_filter INTO TABLE lt_raw.
     LOOP AT lt_raw INTO lv_line.
        ls_line-name = lv_line+0(5).
        lv_len = strlen( lv_line ) - 6.
        ls_line-value = lv_line+6(lv_len).
        APPEND ls_line TO <table>.
     ENDLOOP.
     mr_alv->refresh_table_display( ).
   endmethod.

   METHOD constructor.
       mr_editor = iv_editor.
       mr_alv = iv_alv.
   ENDMETHOD.

   method get_formatted_string.
      FIELD-SYMBOLS: <line> TYPE mytable_line.

      DATA: lv_text TYPE string.

      LOOP AT it_table ASSIGNING <line>.
         CONCATENATE lv_text <line>-line INTO lv_text.
      ENDLOOP.

      ev_text = lv_text.
   endmethod.

ENDCLASS.

主程序源代码:

*&---------------------------------------------------------------------*
*& Report  ZWEIBO
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*

REPORT ZWEIBO.

INCLUDE zweibo_01.

DATA: go_toolbar_con TYPE REF TO cl_gui_docking_container,
      go_edit_con TYPE REF TO cl_gui_docking_container,
      go_alv TYPE REF TO CL_GUI_ALV_GRID,
      go_result_con TYPE REF TO cl_gui_docking_container,
      go_editor TYPE REF TO cl_gui_textedit,
      go_toolbar TYPE REF TO CL_GUI_TOOLBAR,
      go_tool TYPE REF TO lcl_weibo_tool,
      l_container TYPE REF TO cl_gui_container,
      l_s_event TYPE cntl_simple_event,
      l_t_events TYPE cntl_simple_events,
      ok_code LIKE sy-ucomm,
      gt_result TYPE tt_result,
      gv_rep_name LIKE sy-repid.

START-OF-SELECTION.
 gv_rep_name = sy-repid.

call screen 0101.

MODULE STATUS_0101 OUTPUT.
  PERFORM init.
  SET TITLEBAR 'ZTITLE'.
  SET PF-STATUS 'ZSTATUS'.
ENDMODULE.

MODULE USER_COMMAND_0101 INPUT.
   CASE ok_code.
      WHEN 'EXIT' OR 'BACK' OR 'QUIT'.
         LEAVE PROGRAM.
   ENDCASE.
ENDMODULE.

FORM init.
   IF go_toolbar_con IS NOT INITIAL.
      go_alv->refresh_table_display( ).
      RETURN.
   ENDIF.

    CREATE OBJECT go_toolbar_con
        EXPORTING
          repid                       = gv_rep_name
          dynnr                       = '0101'
          side                        = cl_gui_docking_container=>dock_at_top
          extension                   = 19
        EXCEPTIONS
          cntl_error                  = 1
          cntl_system_error           = 2
          create_error                = 3
          lifetime_error              = 4
          lifetime_dynpro_dynpro_link = 5
          OTHERS                      = 6.
  ASSERT sy-subrc = 0.
  l_container = go_toolbar_con.

  CREATE OBJECT go_toolbar
      EXPORTING
        parent             = l_container
        display_mode       = cl_gui_toolbar=>m_mode_horizontal
      EXCEPTIONS
        cntl_install_error = 1
        cntl_error         = 2
        cntb_wrong_version = 3
        OTHERS             = 4.
  ASSERT sy-subrc = 0.

  l_s_event-eventid = cl_gui_toolbar=>m_id_function_selected.
  l_s_event-appl_event = abap_false.
  APPEND l_s_event TO l_t_events.

  CALL METHOD go_toolbar->set_registered_events
      EXPORTING
        events                    = l_t_events
      EXCEPTIONS
        cntl_error                = 1
        cntl_system_error         = 2
        illegal_event_combination = 3
        OTHERS                    = 4.
  ASSERT sy-subrc = 0.

  CALL METHOD go_toolbar->add_button
      EXPORTING
        fcode            = 'CHECK'
        icon             = icon_check
        butn_type        = cntb_btype_button
        quickinfo        = 'Send'
      EXCEPTIONS
        cntl_error       = 1
        cntb_btype_error = 2
        cntb_error_fcode = 3
        OTHERS           = 4.

  ASSERT sy-subrc = 0.

   CREATE OBJECT go_edit_con
        EXPORTING
          repid                       = gv_rep_name
          dynnr                       = '0101'
          side                        = cl_gui_docking_container=>dock_at_top
          extension                   = 110
        EXCEPTIONS
          cntl_error                  = 1
          cntl_system_error           = 2
          create_error                = 3
          lifetime_error              = 4
          lifetime_dynpro_dynpro_link = 5
          OTHERS                      = 6
          .
  ASSERT sy-subrc = 0.
  l_container = go_edit_con.

    CREATE OBJECT go_editor
      EXPORTING
        parent                 = l_container
        "wordwrap_mode = cl_gui_textedit=>wordwrap_at_fixed_position "wordwrap_to_linebreak_mode = cl_gui_textedit=>true
      EXCEPTIONS
        error_cntl_create      = 1
        error_cntl_init        = 2
        error_cntl_link        = 3
        error_dp_create        = 4
        gui_type_not_supported = 5
        OTHERS                 = 6.
  ASSERT sy-subrc = 0.

  CREATE OBJECT go_result_con
      EXPORTING
        repid                       = gv_rep_name
        dynnr                       = '0101'
        side                        = cl_gui_docking_container=>dock_at_top
        extension                   = 140
      EXCEPTIONS
        cntl_error                  = 1
        cntl_system_error           = 2
        create_error                = 3
        lifetime_error              = 4
        lifetime_dynpro_dynpro_link = 5
        OTHERS                      = 6.
  ASSERT sy-subrc = 0.

  CREATE OBJECt go_alv
      EXPORTING
        i_parent          = go_result_con
      EXCEPTIONS
        error_cntl_create = 1
        error_cntl_init   = 2
        error_cntl_link   = 3
        error_dp_create   = 4
        OTHERS            = 5.
  ASSERT sy-subrc = 0.

  DATA: ls_layout TYPE LVC_S_LAYO,
        lt_caption TYPE LVC_T_FCAT,
        ls_caption LIKE LINE OF lt_caption,
        ls_default TYPE ty_result.

        ls_layout-cwidth_opt = abap_false.
  ls_caption-fieldname = 'NAME'.
  ls_caption-just = 'C'.
  ls_caption-seltext = 'Name'.
  APPEND ls_caption TO lt_caption.
  ls_caption-fieldname = 'VALUE'.
  ls_caption-outputlen = 120.
  APPEND ls_caption TO lt_caption.

  ls_default-name = '微博内容'.
  APPEND ls_default TO gt_result.
  ls_default-name = '是否相关'.
  APPEND ls_default TO gt_result.
  ls_default-name = '讨论产品'.
  APPEND ls_default TO gt_result.
  ls_default-name = 'Microblogging tendencies'.
  APPEND ls_default TO gt_result.
  ls_default-name = '服务需求'.
  APPEND ls_default TO gt_result.
  ls_default-name = '购买需求'.
  APPEND ls_default TO gt_result.

  CREATE OBJECT go_tool EXPORTING iv_editor = go_editor iv_alv = go_alv.
  go_tool->set_table( CHANGING ct_table = gt_result ).
  SET HANDLER go_tool->send FOR go_toolbar.

   CALL METHOD go_alv->set_table_for_first_display
      EXPORTING
        is_layout                     = ls_layout
      CHANGING
        it_outtab                     = gt_result
        it_fieldcatalog               = lt_caption

      EXCEPTIONS
        invalid_parameter_combination = 1
        program_error                 = 2
        too_many_lines                = 3
        OTHERS                        = 4
            .



ENDFORM.

101 屏幕设计:
在这里插入图片描述

原网站

版权声明
本文为[Wang Zixi]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/222/202208101459029361.html