欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

用图灵机器人实现的两个机器人对话聊天

程序员文章站 2022-06-04 09:59:05
...

这是使用图灵机器人提供的API来实现的两个机器人对话,挺有意思的,感兴趣的朋友也可以去官网注册一个账号http://www.tuling123.com

下面是具体的实现代码:

public class Test extends Random {
        /**
         * 
         */
        private static final long serialVersionUID = 8120549762361392055L;

        @SuppressWarnings("unused")
        private Long seed;
        private Boolean haveNextNextGaussian;

        synchronized public void setSeed(long seed) {
                this.seed = (seed ^ 0x5DEECE66DL) & ((1L << 48) - 1);
                haveNextNextGaussian = false;
        }

        private String[] userIds = new String[] { "aMuFfwmIa6fsdFfCS87ipA4ljAEKok",
                        "bMuFfwmIa6fsdFfCS87ipA4ljAEKok" };
        private String[] localRebort = new String[] { "aRebort", "bRebort" };
        private Integer needRunTime;
        private Long startTime = 0L;
        private Long endTime = 0L;

        public static void main(String[] args) throws UnsupportedEncodingException {
                Test test = new Test();
                test.runner(test,"你好");
        }

        public void runner(final Test test,final String firstText) {
                Random random = new Random();
                final Integer firstSpeed = random.nextInt(2);
                needRunTime = (Integer) random.nextInt(1 * 60 * 1000 + 1);
                String shouldTime = getTimeStr(needRunTime);
                System.out.println("本次聊天只限" + shouldTime + ",时间到了就得熄灯了!");
                startTime = System.currentTimeMillis();
                try {
                        test.rebort2rebort(test, firstText, firstSpeed);
                } catch (UnsupportedEncodingException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                }
                /*final Timer timer = new Timer();
                timer.schedule(new TimerTask() {
                        @Override
                        public void run() {
                                try {
                                        if (index == 0) {
                                                        test.rebort2rebort(test, firstText, firstSpeed);
                                                index = 1;
                                        } else {
                                                test.rebort2rebort(test, localString, localIndex);
                                        }
                                        timer.cancel();
                                        System.out.println("好了,熄灯了,早点睡吧,别聊天了!");
                                } catch (UnsupportedEncodingException e) {
                                        // TODO Auto-generated catch block
                                        e.printStackTrace();
                                }
                        }
                },0,1000);*/
        }

        private static String getTimeStr(Integer time) {
                time = time / 1000;
                Integer second = 0;
                Integer minute = 0;
                if (time % 60 >= 0) {
                        second = time - time / 60 * 60;
                        time = time / 60;
                        if (time % 60 >= 0) {
                                minute = time - time / 60 * 60;
                                time = time / 60;
                        }
                }
                        return (time == 0L ? "" : (time + "小时")) + (minute==0?"": (minute + "分钟")) + second
                                        + "秒";
        }

        public void rebort2rebort(final Test test, String text, Integer localIndex)
                        throws UnsupportedEncodingException {
                ExpressDoService expressService = new ExpressDoService();
                String respContent;
                String bTalk;
                respContent = expressService.getRebortTalk(text, userIds[localIndex]);
                System.out.println(localRebort[localIndex] + ">>>Said>>>To>>>"
                                + localRebort[(localIndex + 1) / 2] + text);
                BaseRebortResp baseRebortResp = JSONUtil.jsonToBean(respContent,
                                BaseRebortResp.class);
                bTalk = baseRebortResp.getText();
                localIndex = (localIndex + 1) % 2;
                endTime = System.currentTimeMillis();
                if((endTime-startTime-needRunTime)>=0){
                        System.out.println("好了,熄灯了,早点睡吧,别聊天了!");
                        System.exit(0);
                }
                test.rebort2rebort(test, bTalk, localIndex);
        }

        public Integer getNeedRunTime() {
                return needRunTime;
        }

        public void setNeedRunTime(Integer needRunTime) {
                this.needRunTime = needRunTime;
        }
}

 用图灵机器人实现的两个机器人对话聊天