#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    2
    Rep Power
    0

    [Homework] Intro Java help; reading text from file input


    I'm having a hard time with the tokens and the logic involved for part b below.
    Essentially, I'm looking for someone to add on to my work.
    Please help.
    haljordan84 hotmail

    Problem statement:

    A classic problem in computer science is called the Turing Test: is a program is so
    ``intelligent'' that it can carry on a conversation, and fool a human into thinking that
    s/he is talking to a real human being? Obviously, writing a program that passes the
    Turing Test is very difficult (if not impossible in general). However, there have been
    various attempts to write programs that tackle this problem in very restricted contexts.
    The most famous is probably Joseph Weizenbaum’s Eliza, which simulates a
    psychotherapist:

    http://en.wikipedia.org/wiki/ELIZA

    For this project, we will write a somewhat different program that “understands'' very
    simple English sentences, and also generates very simple sentences. An example:
    Human: The vampire killed the politician.
    Computer: Why did the vampire kill the politician?
    Human: The vampire likes blood.
    Computer: Tell me more about the vampire.
    Human: The vampire loves his son.
    Computer: Tell me more about his son.
    Human: His son takes computer science classes.
    Computer: Why does his son take computer science classes?
    Etc etc

    As you can see, once you understand the structure of a sentence, it is straightforward to
    generate new sentences that look like reasonable responses. For this project, we’ll only
    look at sentences that have the form
    [subject] [verb] [object]

    Once we recognize a verb, what comes before the verb is the subject, and what comes
    after the verb is the object. A list of 300+ common verbs is provided in the file
    verbs.txt . You will need to set up String arrays to store words from the file. See
    Chapter 9 Slide 13 for arrays of Strings. These verbs will be stored in a “dictionary” of
    verbs; your program will consult this dictionary repeatedly to check if a word from a
    sentence is a recognized verb.

    Part A
    You will need to fill in several methods in the Dictionary class, and
    code in the main method. MatchVerbs sets up the Dictionary class and does some simple
    verb matching. What MatchVerbs should do:
    1) Prompt the user to enter the name of the file of verbs
    2) Create an object of class Dictionary to hold all the verbs. Fill out the constructor
    Dictionary(String fileName) throws Exception
    … to read all the verbs from the file named fileName, into the array wordList[] in the
    Dictionary object.
    You will need to know how to read and write a text file; The dictionary file should
    be in the same directory as your class file. Provided. There’s also a very short file, verbs5.txt, for initial
    testing.)
    3) Print all the words stored in the dictionary by calling the dump() method
    You will need to fill out the dump() method, and call it in the main method.
    4) Prompt the user to enter a word
    5) Check if it is a verb by calling this method in the Dictionary class:
    String inDictionary(String testWord)
    inDictionary() will try to match testWord with another String in the array wordlist[] (in
    the Dictionary object). It will try to match verbs of different forms; suppose “like” is one
    of the elements of wordlist[]. A match will be found if testWord contains
    like
    likes
    liked
    likees
    likeed
    The last two are obviously inaccurate! But we need to be able to match “crashes” with
    “crash” for example.
    If a match is found between (for example) wordList[i] and testWord, inDictionary() will
    return wordList[i]. Otherwise, a String with zero characters (“”) is returned.

    Finally, your program in will print a message identifying whether the user’s word
    matched a verb stored in the dictionary, and the stored form of the verb in the dictionary.
    If the user’s word is a verb, prompt the user to enter another word; if the user’s word is
    not a verb, end the program.

    Some sample runs:
    % ls
    Chat.class verbs5.txt verbs.txt
    % cat verbs5.txt
    kill
    like
    love
    take
    crash
    % java Chat
    Enter name of file of verbs: verbs5.txt
    The dictionary of verbs contains:
    kill
    like
    love
    take
    crash
    Enter word: kill
    kill matches kill
    Enter word: kills
    kills matches kill
    Enter word: killed
    killed matches kill
    Enter word: takes
    takes matches take
    Enter word: taked
    taked matches take
    Enter word: crashes
    crashes matches crash
    Enter word: crashs
    crashs matches crash
    Enter word: killd
    killd matches kill
    Enter word: lovees
    lovees matches love
    Enter word: lovs
    lovs is not a verb
    %
    Note again that inDictionary() is able to match a verb from the dictionary with a verb
    from the user’s sentence, even if the user’s verb has an extra ‘s’, or ‘es’, or ‘d’, or ‘ed’ at
    the end. (This is of course not perfect! But you don’t want to have to take care of all the
    cases, do you?)

    Partb:

    Use the Dictionary class from above to parse
    simple sentences typed by the user, and generate some responses. First a sample run:
    % java Chat
    Enter name of file of verbs: verbs.txt
    Enter sentence: The vampire killed the politician.
    Subject: the vampire
    Verb: killed
    Object: the politician
    Enter response type (1-3): 1
    Tell me more about the vampire.
    Enter sentence: The vampire killed the politician.
    Subject: the vampire
    Verb: killed
    Object: the politician
    Enter response type (1-3): 2
    Tell me more about the politician.
    Enter sentence: The vampire killed the politician.
    Subject: the vampire
    Verb: killed
    Object: the politician
    Enter response type (1-3): 3
    Why did the vampire kill the politician?
    Enter sentence: The vampire likes blood.
    Subject: the vampire
    Verb: likes
    Object: blood
    Enter response type (1-3): 3
    Why does the vampire like blood?
    Enter sentence: The vampire like blood.
    Subject: the vampire
    Verb: like
    Object: blood
    Enter response type (1-3): 2
    Tell me more about blood.
    Enter sentence: The vampire loves his son.
    Subject: the vampire
    Verb: loves
    Object: his son
    Enter response type (1-3): 3
    Why does the vampire love his son?
    Enter sentence: His son takes computer science classes.
    Subject: his son
    Verb: takes
    Object: computer science classes
    Enter response type (1-3): 3
    Why does his son take computer science classes?
    Enter sentence: He instantiates Java classes for fun.
    No verb found
    %

    More formally, your program should do this:
    Prompt the user for the name of a dictionary file containing verbs
    Use the Dictionary class and methods to make a dictionary of verbs (see Part a)
    Prompt the user to enter a sentence
    Use the dictionary of verbs to find the first verb in the sentence
    (Hint: you should first break the sentence into tokens. See Chapter 9 Slide 14.)
    If no verb is found, program ends
    Break up the sentence into [subject] [verb] [object]; display each part
    Prompt the user to enter a response type (1-3)
    Print the response of the requested type:
    Type 1: “Tell me more about “ [subject]
    Type 2: “Tell me more about “ [object]
    Type 3: “Why does/did [subject] [verb] [object]?”
    Prompt the user for the next sentence; etc
    (For Type 3, you may assume that subject is always singular. However, you must
    match present/past tense with the original sentence and the verb should be in the
    infinitive form, i.e., the form stored in the dictionary.)
    Make sure you use the inDictionary() method with the dictionary of verbs to match any
    verbs; otherwise points will be deducted.

    What I have so far: (basically, the first part)

    Code:
    import java.util.Scanner;
    
    public class MatchVerb {
        public static void main(String [] args) throws Exception {
    	Scanner input = new Scanner(System.in);
    
    	System.out.print("Enter name of file of verbs: ");
    	String fName = input.nextLine();
    	Dictionary verbs = new Dictionary(fName);
    
    	System.out.println("The dictionary of verbs contains: ");
    	verbs.dump();
    
    	do {
    	    System.out.print("Enter word: ");
    	    String inWord = input.nextLine();
    	    
    	    String actualWord = verbs.inDictionary(inWord);
    
    	    if (actualWord.length()!=0) {
    		System.out.println(inWord + " matches " + actualWord);
    	    }
    	    else {
    		System.out.println(inWord + " is not a verb");
    		break;
    	    }
    	} while (true);
    	
    
        }
    }
    
    class Dictionary {
        String [] wordList;
        final int MAXWORDS = 10000;
        int currWord = 0;
    
        Dictionary(int numWords) {
    	wordList = new String [numWords];
    	for (int i=0; i<wordList.length; i++) {
    	    wordList[i] = "";
    	}
        }
    
        Dictionary(String fileName) throws Exception {
    	int numWords = 0;
    	String [] tList = new String[MAXWORDS];
    	java.io.File file = new java.io.File(fileName);
    
    	Scanner inFile = new Scanner(file);
    	if (!file.canRead()) {
                System.out.println("Error opening input file; abort");
            }
    
            // add each word of length wordLength to wordList[]
            while (inFile.hasNext()) {
                String inWord = inFile.nextLine();
    	    tList[numWords] = new String(inWord);
    	    numWords++;
    	    if (numWords == MAXWORDS)
    		break;
            } // end while (inFile.hasNext())
    
    	wordList = new String[numWords];
    	for (int i=0; i<wordList.length; i++) {
    	    wordList[i] = new String(tList[i]);
    	}
    	
        }
    
        void dump() {
    	for (int i=0; i<wordList.length; i++) {
    	    System.out.println(wordList[i]);
    	}
        }
    
        void addWord(String newWord) {
    	wordList[currWord] = new String(newWord);
    	currWord = (currWord + 1) % wordList.length;
        }
    
        String inDictionary(String testWord) {
    
    	for (int i=0; i<wordList.length; i++) {
    	    if (wordList[i].equalsIgnoreCase(testWord)) {
    		return wordList[i];
    	    }
    	    if (testWord.startsWith(wordList[i]) &&
    		wordList[i].length() == testWord.length() - 1 &&
    		testWord.endsWith("s")) {
    		return wordList[i];
    	    }
    	    if (testWord.startsWith(wordList[i]) &&
    		wordList[i].length() == testWord.length() - 2 &&
    		testWord.endsWith("es")) {
    		return wordList[i];
    	    }
    	    if (testWord.startsWith(wordList[i]) &&
    		wordList[i].length() == testWord.length() - 1 &&
    		testWord.endsWith("d")) {
    		return wordList[i];
    	    }
    	    if (testWord.startsWith(wordList[i]) &&
    		wordList[i].length() == testWord.length() - 2 &&
    		testWord.endsWith("ed")) {
    		return wordList[i];
    	    }
    	}
    	return "";
        }
    }
    
    /*** 
    
    verbs5.txt
    kill
    like
    love
    take
    crash
    
    ***/
  2. #2
  3. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,696
    Rep Power
    347
    Do you have any specific questions about your problems?

    If you are getting errors, copy the full text and paste them here.

    If the program's output is wrong, copy the output here and explain what is wrong with it and show what it should be.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    2
    Rep Power
    0
    I'm a stats major and am taking this class online (first and only) and it's a major headache. I'm looking for someone to create the methods for part b so I can drop it into the code I wrote for part A.

    Help!
  6. #4
  7. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,696
    Rep Power
    347
    Have you looked at hiring a programmer to write the programs you need?
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    74
    Rep Power
    18
    Originally Posted by NormR
    Have you looked at hiring a programmer to write the programs you need?
    Indeed, this an approach to the classic problem posed by Alan Turing: the Turing Test. Many scholars have tried to tackle this problem, and not many have succeeded to pass this Turing Test with significant result.

    We can help you to check on any errors or problems encountered, but not assisting in "I'm looking for someone to create the methods for part b so I can drop it into the code I wrote for part A. "
    Part A is just a simple read and match program. Part B is where the actual A.I. takes place.

IMN logo majestic logo threadwatch logo seochat tools logo