**free
//  WATSONKW1R: This analyzes the approved vendor web sites
//              using IBM Watson.  It finds keywords for each
//              site and puts them into the AVKEYWORDS table.
//
//               Requires HTTPAPI and YAJL

ctl-opt dftactgrp(*no) bnddir('HTTPAPI': 'YAJL');

/include HTTPAPI_H
/include YAJL_H

dcl-pr QCMDEXC extpgm('QCMDEXC');
   command char(32767) options(*varsize) const;
   length  packed(15: 5) const;
   igc     char(3) const options(*nopass);
end-pr;

dcl-ds AVLIST extname('AVLIST') qualified end-ds;

dcl-c userid '< PUT YOUR API KEY HERE >';
dcl-c password '< PUT YOUR PASSWORD HERE >';
dcl-c url 'https://gateway.watsonplatform.net/natural-language-understanding/api/v1/analyze+
          ?version=2018-03-16';

dcl-s request varchar(1000);
dcl-s response varchar(100000);

exec SQL set option naming=*sys, commit=*none;

http_setAuth( HTTP_AUTH_BASIC : userid: password );
http_setOption('network-ccsid': '1208');
http_debug(*on: '/tmp/natural-language-example.txt');

QCMDEXC('CLRPFM avkeywords': 17);

exec SQL declare C1 cursor for
   select * from AVLIST;

exec SQL open C1;

exec SQL fetch next from C1 into :AVLIST;
dow sqlstt='00000';

   // "request" is a JSON string containing the vendor's URL and
   // what we want (keywords) from Natural Language Understanding
   request = createJsonRequest(AVLIST);

   monitor;
      response = http_string('POST': url: request: 'application/json');
   on-error;
      response = '';
   endmon;

   // "response" is a JSON string containing all the keywords Watson found
   // this routine writes any with 0.35 relevance (or higher) to the
   // avkeywords table
   extractKeywords(AVLIST.VNUM: response);

   exec SQL fetch next from C1 into :AVLIST;
enddo;

exec SQL close C1;
*inlr = *on;

dcl-proc createJsonRequest;

   dcl-pi *N varchar(1000);
      VEND likeds(AVLIST) const;
   end-pi;

   dcl-s json varchar(1000);

   yajl_genOpen(*off);
   yajl_beginObj();                           // {
     yajl_addChar('url': %trim(VEND.VURL));   //   "url": the-url,
     yajl_beginObj('features');               //   "features" : {
       yajl_beginObj('keywords');             //      "keywords": {
       yajl_endObj();                         //      }
     yajl_endObj();                           //   }
   yajl_endObj();                             //

   json = yajl_copyBufStr();
   yajl_genClose();

   return json;
end-proc;


dcl-proc extractKeywords;

   dcl-pi *N;
      vnum packed(7: 0) value;
      json varchar(100000) const;
   end-pi;

   dcl-s docNode like(yajl_val);
   dcl-s keywords like(yajl_val);
   dcl-s keyword like(yajl_val);
   dcl-s errMsg varchar(500);
   dcl-s text char(40);
   dcl-s relv packed(5: 3);
   dcl-s i int(10);

   docNode = yajl_string_load_tree(response: errMsg);
   if errMsg = '';

       keywords = yajl_object_find(docNode: 'keywords');
       i = 0;

       dow YAJL_ARRAY_LOOP(keywords: i: keyword);

          text = yajl_get_string(yajl_object_find(keyword: 'text'));
          relv = yajl_get_number(yajl_object_find(keyword: 'relevance'));

          if relv > 0.35;
             exec SQL insert into AVKEYWORDS values(:vnum, UPPER(:text));
          endif;

       enddo;

       yajl_tree_free(docNode);
   endif;

 end-proc; 