1. 程式人生 > >基於圖靈機器人的聊天機器人實現(一)

基於圖靈機器人的聊天機器人實現(一)

最近要做一個智慧音箱的專案,可是音效卡一直配置不好。。。所以,還做個啥啊。沒辦法,智慧退而求其次,做一個文字互動的聊天機器人管家,並給它寫個介面。
用java寫,然後在linux裝個jdk就行了。
下面說一下怎麼實現
一.圖靈api介面

 public String chart(String message){
        StringBuffer sb=new StringBuffer();
        Random rand = new Random();
        ArrayList<String> KEYLIST=new ArrayList<String
>(); KEYLIST.add("8d64b5ff5a57435fa63fd0e111f821fc");//註冊圖靈機器人的賬號,每建立一個機器人會給一個key,通過這個key來呼叫api KEYLIST.add("d085ed36e2604506968a3c18c5733116");//因為由每個key都有呼叫次數限制,這裡存了多個key KEYLIST.add("4cdce663b3a44b5bb94cf62ca52de590"); try{ String APIKEY =KEYLIST.get(rand.nextInt(3
));//隨機選擇用哪個key String INFO = URLEncoder.encode(message, "utf-8");//轉換格式 String getURL = "http://www.tuling123.com/openapi/api?key=" + APIKEY + "&info=" + INFO;//拼接請求url URL getUrl = new URL(getURL); HttpURLConnection connection = (HttpURLConnection) getUrl.openConnection(); connection.connect();//連線
// 取得輸入流,並使用Reader讀取 BufferedReader reader = new BufferedReader(new InputStreamReader( connection.getInputStream(), "utf-8")); sb.setLength(0);//每次把Stingbuffer清空 String line = ""; while ((line = reader.readLine()) != null) {//讀取結果 sb.append(line); } reader.close(); // 斷開連線 connection.disconnect(); } catch ( Exception e){ e.printStackTrace(); } String split[]=sb.toString().split(":"); String answer=split[split.length-1]; answer=answer.substring(1,answer.length()-2);//獲取到迴應 return answer; }

二.介面

package com.test;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class myframe extends JFrame {
    private JTextArea jTextAreaofShow;
    private JTextArea jTextAreaofSend;
    private JScrollPane scrollPaneofShow;//滾動框
    private JScrollPane scrollPaneofSend;
    private JPanel panel;
    private JButton jButton;
    private robot robot;
    public  myframe(String title,int x,int y,int w,int h) {
        super(title);
        robot=new robot();//初始化機器人
        //為send部分設定面板
        panel=new JPanel();
        panel.setLayout(new FlowLayout());
        jButton=new JButton("傳送");
        jButton.addActionListener(new buttonlistener());//為button設定監聽器
        jTextAreaofSend=new JTextArea();
        //設定send文字框的大小  2行20列
        jTextAreaofSend.setRows(2);
        jTextAreaofSend.setColumns(50);
        jTextAreaofShow=new JTextArea();
        jTextAreaofShow.setRows(10);
        jTextAreaofShow.setColumns(50);
        jTextAreaofShow.setEditable(false);//設定show文字框不可編輯
        //為兩個文字框設定滾輪
        scrollPaneofSend=new JScrollPane(jTextAreaofSend);
        scrollPaneofShow=new JScrollPane(jTextAreaofShow);
        //send文字框和按鈕新增到面板
        panel.add(scrollPaneofSend);
        panel.add(jButton);
        add(scrollPaneofShow,BorderLayout.CENTER);
        add(panel,BorderLayout.SOUTH);
        setBounds(x,y,w,h);//設定邊界大小
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);//設定關閉事件
    }
    public class buttonlistener implements ActionListener{//監聽器
        @Override
        public void actionPerformed(ActionEvent e) {
            String message=jTextAreaofSend.getText();
            if(message!=null){
                String result=robot.chart(message);
                String show=jTextAreaofShow.getText();
                StringBuilder showbuider=new StringBuilder(show);
                showbuider.append("我說:"+message+"\n");
                showbuider.append("小管家:"+result+"\n");
                jTextAreaofShow.setText(showbuider.toString());
                jTextAreaofSend.setText("");
            }
        }
    }
}

三.主函式

package com.test;

import java.util.ArrayList;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
       myframe myframe=new myframe("機器人管家",100,100,800,500);
       myframe.setVisible(true);
        robot robot=new robot();
        Scanner sc = new Scanner(System.in);
        ArrayList<String> ability=new ArrayList<>();
        while(true){
            System.out.println("你說:");
            String message=sc.nextLine();
            if(message.equals("提醒")){

            }
            String result=robot.chart(message);
            System.out.println("機器人:"+result);
        }


    }
}

四.結果展示
這裡寫圖片描述