標籤:
很多人都玩過,其中就有與機器人聊天的功能:
這個圖靈機器人網站提供了相關的API介面,可以在程式中利用,過程如下:
1.在圖靈機器人網(http://www.tuling123.com/openapi/cloud/home.jsp)註冊賬戶,然後他就會給你相應的API KEY,這在程式中需要用到:
2.在Java程式中接入機器人:
String APIKEY = "官網給你的api key";
String INFO = URLEncoder.encode("你好,美女", "utf-8");//這裡可以輸入問題
String getURL = "http://www.tuling123.com/openapi/api?key=" + APIKEY + "&info=" + INFO;
URL getUrl = new URL(getURL);
HttpURLConnection connection = (HttpURLConnection) getUrl.openConnection();
connection.connect();
// 取得輸入資料流,並使用Reader讀取
BufferedReader reader = new BufferedReader(new InputStreamReader( connection.getInputStream(), "utf-8"));
StringBuffer sb = new StringBuffer();
String line = "";
while ((line = reader.readLine()) != null) {
sb.append(line);
}
reader.close();
// 中斷連線
connection.disconnect();
這段代碼上部分是連到圖靈機器人的官網,下部分是讀取他的回複資訊。你要問的問題放在了INFO這個String裡面。
3.一個簡單的聊天程式:
import java.awt.FlowLayout;import java.awt.Font;import java.awt.GridLayout;import java.awt.Image;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.KeyEvent;import java.awt.event.KeyListener;import java.beans.EventHandler;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.net.HttpURLConnection;import java.net.URL;import java.net.URLEncoder;import javax.swing.Icon;import javax.swing.ImageIcon;import javax.swing.JButton;import javax.swing.JColorChooser;import javax.swing.JDialog;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JPanel;import javax.swing.JTextArea;import javax.swing.JTextField;public class 圖靈機器人 { public 圖靈機器人() { } public static void main(String[] args) throws IOException { String information = new String("Hello"); String mechine = new String(); JFrame frame = new JFrame("與機器人聊天"); JPanel panel = new JPanel(new GridLayout(3,1)); JPanel questionPanel = new JPanel(new FlowLayout()); JPanel buttonPanel = new JPanel(); JPanel answerPanel = new JPanel(new FlowLayout()); JLabel question = new JLabel("問題"); JTextField enterQuestion = new JTextField(20); JLabel answer = new JLabel("機器人回答"); JTextArea enterAnswer = new JTextArea(3,25); JButton submit = new JButton("提交");// ImageIcon imgIcon = new ImageIcon("images/robot.png");// Icon img = imgIcon;// JLabel imgLabel = new JLabel(); frame.setSize(600, 400); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); enterAnswer.setFont(new Font("宋體",Font.BOLD,15)); enterQuestion.setFont(new Font("宋體",Font.BOLD,15));// imgIcon.setImage(imgIcon.getImage().getScaledInstance(35,35,Image.SCALE_DEFAULT)); enterAnswer.setLineWrap(true);// imgLabel.setIcon(img); questionPanel.add(question); questionPanel.add(enterQuestion);// answerPanel.add(imgLabel); answerPanel.add(answer); answerPanel.add(enterAnswer); buttonPanel.add(submit); panel.add(questionPanel); panel.add(answerPanel); panel.add(buttonPanel); frame.add(panel); submit.addActionListener(new ActionListener(){ @Override public void actionPerformed(ActionEvent e) { String answer = new String(); String q = enterQuestion.getText(); try { answer = machine(q); } catch (IOException e1) { e1.printStackTrace(); } enterAnswer.setText(answer); } }); enterQuestion.addKeyListener(new KeyListener() { @Override public void keyTyped(KeyEvent e) { // TODO Auto-generated method stub } @Override public void keyPressed(KeyEvent e) { if(e.getKeyCode()==10 || e.getKeyCode()==38) { String answer = new String(); String q = enterQuestion.getText(); try { answer = machine(q); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } enterAnswer.setText(answer); } } @Override public void keyReleased(KeyEvent e) { // TODO Auto-generated method stub } }); } private static String machine(String quesiton) throws IOException { //接入機器人,輸入問題 String APIKEY = "官網給你的api key"; String INFO = URLEncoder.encode(quesiton, "utf-8");//這裡可以輸入問題 String getURL = "http://www.tuling123.com/openapi/api?key=" + APIKEY + "&info=" + INFO; URL getUrl = new URL(getURL); HttpURLConnection connection = (HttpURLConnection) getUrl.openConnection(); connection.connect(); // 取得輸入資料流,並使用Reader讀取 BufferedReader reader = new BufferedReader(new InputStreamReader( connection.getInputStream(), "utf-8")); StringBuffer sb = new StringBuffer(); String line = ""; while ((line = reader.readLine()) != null) { sb.append(line); } reader.close(); // 中斷連線 connection.disconnect(); String[] ss = new String[10]; String s = sb.toString(); String answer; ss = s.split(":"); answer = ss[ss.length-1]; answer = answer.substring(1,answer.length()-2); return answer; }}
:
總結:這機器人還是挺好玩的,可以開發出其他功能,剩下就看自己的想象力了。
Java接入圖靈機器人,實現與機器人聊天