简体   繁体   中英

Clojure: Java Interop IBM watson conversation service

The conversation service has its java code written as

import com.ibm.watson.developer_cloud.conversation.v1.ConversationService;
import com.ibm.watson.developer_cloud.conversation.v1.model.MessageRequest;
import com.ibm.watson.developer_cloud.conversation.v1.model.MessageResponse;

 /*
 some code written here
*/
MessageRequest newMessage = new MessageRequest.Builder().inputText(input).context(context).build();

My main question is how to write this

 **MessageRequest newMessage = new MessageRequest.Builder().inputText(input).context(context).build();**

in clojure.

This is what i did so far is

(ns clj.core
(:import
  (com.ibm.watson.developer_cloud.conversation.v1 ConversationService)
  (com.ibm.watson.developer_cloud.conversation.v1.model MessageRequest)
  (com.ibm.watson.developer_cloud.conversation.v1.model MessageResponse)))

 (let [username "foo"
        password "bar"
        input "hello"
        context {}
        workspaceId "ibm-watson-id"
        service (ConversationService. "2017-08-26")
        userPass (.setUsernameAndPassword service username password)

        ;obviously this is wrong
        ;dont know how to get this right
        newMessage (.build (.context  context (.inputText input (MessageRequest.Builder.))))



        response (.message service workspaceId newMessage)]
       response)

Please help. Thanks

aha, eventually figured it out, it seems the MessageRequest class has another class called Builder

All i did was to reference this class and tweak the needful

(ns clj.core
 (:import
  (com.ibm.watson.developer_cloud.conversation.v1 ConversationService)
  (com.ibm.watson.developer_cloud.conversation.v1.model MessageRequest)
  (com.ibm.watson.developer_cloud.conversation.v1.model MessageResponse)))

(let [username "foo"
     password "bar"
     input "hello"
     context {}
     workspaceId "ibm-watson-id"
     service (ConversationService. "2017-08-26")
     userPass (.setUsernameAndPassword service username password)

     ;just wanna make it work
     msgReq (MessageRequest$Builder.)
     inputText (.inputText msgReq input)
     content (.context inputText context)
     newMessage (.build content)
     response (.execute (.message service workspaceId newMessage))]
   (println  "Watson Response: " response))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM