2016-05-26 3 views
0

私のコードでは、基本的な "チャットボット"と非常に基本的な顔(顔が笑顔や眉をひそめたり、退屈することがあります)を生成するクラスがあり、最後に私はドライバークラスを持っています。Javaでは、特定の単語を検出することでどのような処理を行うことができますか?

私の質問です:私はそれが(フレーズで)特定の単語を検出したときに退屈行動/笑顔/しかめ面を生成するためのコードを取得することが可能である

これは私がこれまで持っているものです。

チャットボットクラス:その後

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

import java.awt.Color; 

import java.awt.event.KeyListener; 
import java.awt.event.KeyEvent; 

import java.lang.Math; 


public class ChatBot extends JFrame implements KeyListener{ 
JPanel p = new JPanel(); 
JTextArea dialog = new JTextArea(20,50); 
JTextArea input = new JTextArea(1,50); 
JScrollPane scroll = new JScrollPane(
    dialog, 
    JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, 
    JScrollPane.HORIZONTAL_SCROLLBAR_NEVER 
); 

String[][] chatBot={ 
    //standard greetings 
    {"hi","hello","hola","ola","howdy" , "wassup", "sup", "bonjour", }, 
    {"hi","hello","hey"}, 
    //question greetings 
    {"how are you","how r you","how r u","how are u, how are you?, how are you ?", "wassup"}, 
    {"good","doing well", "not bad", "meh", "I could be better", "However you want me to be "}, 
    //yes 
    {"yes"}, 
    {"no"}, 
    //default 
    {"how was your day?"} 
}; 

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

public ChatBot(Changingface x){ 
    super("Chat Bot"); 
    setSize(600,400); 
    setResizable(false); 
    setDefaultCloseOperation(EXIT_ON_CLOSE); 

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

    p.add(scroll); 
    p.add(input); 
    p.setBackground(new Color(75,14,215)); 
    add(p); 

    setVisible(true); 

    x= new ChangingFace();//need help with this, teacher told me to make a parameter for the constructor and then make code that 
         //was able to detect a word in a phrase and respond accordingly to that word/phrase 

    if(input.getText("day")){//this is how far I got 

     } 


} 

public void keyPressed(KeyEvent e){ 
    if(e.getKeyCode()==KeyEvent.VK_ENTER){ 
     input.setEditable(false); 
     //-----grab quote----------- 
     String quote=input.getText(); 
     input.setText(""); 
     addText("-->You:\t"+quote); 
     quote.trim(); 
     while(
      quote.charAt(quote.length()-1)=='!' || 
      quote.charAt(quote.length()-1)=='.' || 
      quote.charAt(quote.length()-1)=='?'// how are you? how are you ? 
     ){ 
      quote=quote.substring(0,quote.length()-1); 
      quote.trim(); 
     } 
     byte response=0; 
     /* 
     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-->Chatty McChatface:\t"+chatBot[(j*2)+1][r]); 
      } 
      j++; 
      if(j*2==chatBot.length-1 && response==0){ 
       response=1; 
      } 
     } 

     //-----default-------------- 
     if(response==1){ 
      int r=(int)Math.floor(Math.random()*chatBot[chatBot.length-1].length); 
      addText("\n-->Chatty McChatface:\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); 
} 

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; 
     } 
    } 
    return match; 
} 

}

私は私の顔のクラスを持っている:

再び

public class ChangingFaceTester{ 
public static void main(String[] args){ 
    new ChangingFace(); 
    new ChatBot(); 
} 
} 

私の質問:

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

public class ChangingFace extends JFrame implements ActionListener 
{ 
int whichFeel = 0; 
private JButton happyButton = new JButton("Cheeky Smirk"); 
private JButton thinkButton = new JButton("Bored"); 
private JButton sadButton = new JButton("Somethin fishy"); 

public ChangingFace() 
{ 
    // set the title 
    setTitle("Face behind the ChatBot"); 

// choose a Flow Layout policy 
setLayout(new FlowLayout()); 

// add the buttons to the frame 
add(happyButton); 
add(thinkButton); 
add(sadButton); 

// set the background to cyan 
getContentPane().setBackground(Color.cyan); 

// enable the buttons to listen for a mouse-click 
happyButton.addActionListener(this); 
thinkButton.addActionListener(this); 
sadButton.addActionListener(this); 

// configure the frame 
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
setSize(720, 420);//250,200 
setLocation(100,100);//300,300 
setVisible(true); 
} 

public void paint(Graphics g) 
    { 
    // call the paint method of the superclass, Jframe 
    super.paint(g); 
    // paint the face 
    g.setColor(Color.red);//red yellow blue orange pink cyan magenta black white gray lightGray darkGray 
         //http://mathbits.com/MathBits/Java/Graphics/Color.htm 
    g.drawOval(85,75,75,75);//85,75,75,75 
    g.setColor(Color.darkGray); 
    g.fillOval(100,95,10,10);//100,95,10,10 
    g.fillOval(135,95,10,10);//135,95,10,10 
    g.drawString("Savage OGB lgt", 79,181); 
    g.drawRect(121, 150, 3, 20);//121,150,3,20 
    if(whichFeel == 1) 
    { 
    // draw a smiling mouth 
    g.drawArc(102,115,40,25,0,-180); 
    } 
    else if(whichFeel == 3) 
    { 
    // draw a frowning mouth 
    g.drawArc(102,115,40,25,0,180); 
    } 
    else if(whichFeel == 2) 
    { 
    // draw a thinking mouth 
    g.drawLine(102, 130, 142, 130); 
    } 
} 
    public void actionPerformed(ActionEvent e) 
{ 
if(e.getSource() == happyButton) 
{ 
whichFeel = 1; 
repaint(); 
} 
if(e.getSource() == thinkButton) 
{ 
whichFeel = 2; 
repaint(); 
} 
if(e.getSource() == sadButton) 
{ 
whichFeel = 3; 
repaint(); 
    } 
} 
} 

とドライバクラスどのように私は/しかめ面を笑顔に顔を得ることができます/ユーザー入力からフレーズ/ワードを検出することにより、(退屈行為)と思いますか?

答えて

0

String.contains()メソッドを使用できます。

if(input.getText().contains("beer"){ 
    x.setWhichFeel(1); // x is the variable name you gave your ChangingFace 
}      // instance in the chatbot class 

編集:

public class ChangingFace(){ 

    private int whichFeel; 

    public void setWhichFeel(int num){ 
     whichFeel = num; 
    } 
} 

このメソッドはChangeFaceクラスでwhichFeel変数を変更するチャットボットクラスから呼び出すことができます。チャットボットは一つの引数を必要とするので、

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

編集2:あなたが持っているあなたの主な方法で

public ChatBot(Changingface x){ 
} 

チャットボットのためのあなたのコンストラクタは一つの引数、ChangingFaceオブジェクトが必要ですChangingFaceオブジェクトを渡す必要があります。

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

また、ドライバクラスには主な方法があります。主な方法は1つだけあります。あなたのchatBoxオブジェクトがどんなクラスであってもChangingFaceオブジェクトをパラメータとして受け入れなければならないことは忘れないでください。さらに検討の際

、ChangingFaceTesterクラスのmainメソッドを維持するが、それは好き構造:

編集3

public class ChangingFaceTester{ 
    public static void main(String[] args){ 
     new ChatBot(new ChangingFace()); 
    } 
} 
チャットボットクラス
+0

次に、あなたがセッターを持っている必要がありますwhichFeelを含むクラスで。編集を参照してください。 – Ineedhelp

+0

ではありませんwhichFeel – Rocky

+0

それは意味がありますか? – Rocky

関連する問題