Eliza java code - PowerPoint PPT Presentation

1 / 17
About This Presentation
Title:

Eliza java code

Description:

there is no method called main ... the init method of ElizaApplet.java is invoked ... elizaOutput.append(' ' elizaInput.getText() 'n' eliza.grabText ... – PowerPoint PPT presentation

Number of Views:127
Avg rating:3.0/5.0
Slides: 18
Provided by: ugrad2
Category:
Tags: append | code | eliza | java

less

Transcript and Presenter's Notes

Title: Eliza java code


1
Eliza java code
2
applets
  • ElizaApplet.java is an applet (rather than a
    regular java program)
  • there is no method called main( )
  • instead, the applet is started when the
    ElizaApplet.html page is opened

3
Eliza.html
  • lthtmlgt
  • ltheadgt
  • lttitlegtElizaAppletlt/titlegt
  • lt/headgt
  • ltbodygt
  • ltapplet codebase "." code "ElizaApplet.class"
    width 400 height 400gtlt/appletgt
  • lt/bodygt
  • lt/htmlgt

4
ElizaApplet.java
  • the init method of ElizaApplet.java is invoked
    when Eliza.html is opened
  • init creates an object eliza, which is an
    instance of the class MyEliza
  • init sets up a text area (elizaOutput) for the
    conversation, and a text field (elizaInput) where
    the user enters input
  • the applet must be able to respond to input events

5
responding to inputs
  • the ElizaApplet class implements ActionListener
    which gives objects of the class the ability to
    respond to inputs from the user
  • the class must have a corresponding
    actionPerformed method, which specifies how the
    program responds to inputs

6
public void actionPerformed(ActionEvent e)
  • if (e.getSource()elizaInput)
  • //what happens if the user presses enter
  • elizaOutput.append("gtgt " elizaInput.getText()
    "\n eliza.grabText((elizaInput.getText()).toL
    owerCase()) "\n")
  • elizaInput.setText("")

7
thats it for ElizaApplet!
8
myEliza.java
  • what happens when the instruction
  • eliza new MyEliza( )
  • is executed as part of the ElizaApplet code?

9
myEliza.java
  • what happens when the instruction
  • eliza new MyEliza( )
  • is executed as part of the ElizaApplet code?
  • - the object eliza is created, and its
    variables greeting, qa, unknowns, and polly are
    initialized

10
unknowns
  • what type of object is unknowns?
  • what does unknowns look like, when it is
    initialized?

11
qa
  • what type of object is qa?
  • what methods does this type of object have?
  • what does qa look like once it is initialized?

12
grabText
  • public String grabText(String said)
  • StringTokenizer patient
  • String theAnswer String curr
  • theAnswer""
  • more code here to calculate Elizas
    response to said
  • return theAnswer

13
calculating Elizas response
  • the string said is broken up into an ordered list
    of words (tokens), part of an object called
    patient
  • if one of the tokens is a keyword, then respond
    with the corresponding response in the hashtable
    and otherwise choose a random generic response

14
tokenizing the string said
  • example
  • said my mother asked about my classes
  • the tokenized version of said
  • patient my, mother, asked, about,
    my classes

15
patient a StringTokenizer Object
  • this object has methods
  • nextToken removes the next token from and
    returns it
  • hasMoreTokens returns true or false, depending
    on whether there are tokens left

16
calling StringTokenizer methods
  • initially,
  • patient is my, mother, asked, about,
    my classes
  • after three invocations of patient.nextToken,
  • patient is about, my classes
  • and patient.hasMoreTokens() is true
  • what about after three more invocations?

17
calculating the answer
  • patient new StringTokenizer(said)
  • while (patient.hasMoreTokens()
    theAnswer.equals(""))
  • curr patient.nextToken()
  • if (qa.containsKey(curr))
  • theAnswer (String)qa.get(curr)
  • if (theAnswer.equals(""))
  • theAnswerunknownsgimme_a_number()
Write a Comment
User Comments (0)
About PowerShow.com