如何在java

时间:2017-08-12 18:36:31

标签: java

我是Java的初学者,我正在尝试构建一个聊天机器人。查询和响应在2D字符串数组chatbot [] []中进行硬编码。

这是我使用的完整代码,已经使用了两个用于JTextArea'输入和对话'的对象。对象输入已附加到JPanel,用于从用户获取文本,然后在2D字符串数组中搜索。如果找到,则从同一数组中抛出响应。

现在我的要求是当响应是任何URL时,它应该显示为链接,以便用户可以直接点击并转到该站点(http://google.com以获取当前场景)。建议如何实现这一点。或者需要对代码进行任何修改。 addText是定义为将整个会话添加到不可编辑的对话文本区域的方法。

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JScrollPane;

import java.awt.event.KeyListener; 
import java.awt.event.KeyEvent;
import java.lang.Math;
import java.awt.Color;
import java.awt.Toolkit;

public class test implements KeyListener{

JFrame j=new JFrame("Temenos ChatBot");
JPanel p=new JPanel();
JTextArea dialog=new JTextArea(20,50); //it will contain the whole conversation(non editable)
JTextArea input=new JTextArea(1,50); //where we user input our query(editable)
JScrollPane scroll=new JScrollPane( 
    dialog,
    JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
    JScrollPane.HORIZONTAL_SCROLLBAR_NEVER
);


String[][] chatbot={

{"hi","hello","hola","ola","howdy"}, //check for query
{"hi","hello","hey"}, //provides output of the query

{"google","provide link for google","www.google.com"}, //Query
{"http://google.com"}, //response


//default 
{"we can't find your query",
"(Sorry, we could not understand)"}
};


public static void main(String[]args){
    new test();
}

private void icon(){
    j.setIconImage(Toolkit.getDefaultToolkit().getImage(getClass().getResource("dy1MzZ-W.png")));//change the icon, paste the icon in default package
}

public test(){

    j.setSize(600,400);
    j.setResizable(false);
    j.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    dialog.setEditable(false);
    input.addKeyListener(this);     

    p.add(scroll);
    p.add(input);
    p.setBackground(new Color(0,150,255));
    j.add(p);

    j.setVisible(true);
    icon();
}
public void keyPressed(KeyEvent e){
    if(e.getKeyCode()==KeyEvent.VK_ENTER){
        input.setEditable(false); 
        //-----grab quote-----------
        String quote=input.getText(); //takes whatever text user is inputting
        input.setText("");
        addText("->You:\t"+quote); 
        quote.trim(); 
        while(
            quote.charAt(quote.length()-1)=='!' ||
            quote.charAt(quote.length()-1)=='.' ||
            quote.charAt(quote.length()-1)=='?'
        ){
            quote=quote.substring(0,quote.length()-1); 
        }
        quote.trim();
        byte response=0; //byte is data type that will check for response
        /*
        0:we're searching through chatBot[][] for matches
        1:we didn't find anything
        2:we did find something
        */
        //-----check for matches----
        int j=0;//which group we're checking
        while(response==0){
            if(inArray(quote.toLowerCase(),chatbot[j*2])){
                response=2;
                int r=(int)Math.floor(Math.random()*chatbot[(j*2)+1].length);
                addText("\n->A.I.sha:\t"+chatbot[(j*2)+1][r]); 
            }
            j++;
            if(j*2==chatbot.length-1 && response==0){
                response=1;
            } //if it has come to end then set response to 1
        }

        //-----default--------------
        if(response==1){
            int r=(int)Math.floor(Math.random()*chatbot[chatbot.length-1].length);
            addText("\n->A.I.sha:\t"+chatbot[chatbot.length-1][r]); 
        }
        addText("\n");
    }
}

public void keyReleased(KeyEvent e){
    if(e.getKeyCode()==KeyEvent.VK_ENTER){
        input.setEditable(true);
    }
}

public void keyTyped(KeyEvent e){}

public void addText(String str){
    dialog.setText(dialog.getText()+str);
}//it will add whatever text dialogue box is having

public boolean inArray(String in,String[] str){
    boolean match=false;
    for(int i=0;i<str.length;i++){
        if(str[i].equals(in)){
            match=true;
        } //Searches for string 'in' in string array,if it finds return true
    }
    return match;
}
 }

2 个答案:

答案 0 :(得分:0)

您可以使用Uri的解析静态方法

Uri myUri = Uri.parse("http://google.com")

答案 1 :(得分:0)

如果你正在制作一个GUI,你可以跟随另一个堆栈溢出答案,例如这个 How to add hyperlink in JLabel

或者,您可以让java打开浏览器到该网页:

 // Create Desktop object
 Desktop d=Desktop.getDesktop();
 // Browse a URL, say google.com
 d.browse(new URI("http://google.com"));