100 likes | 403 Views
Compsci 201 Recitation 6. Professor Peck Jimmy Wei 2 /14 / 2014. In this Recitation. Markov! Google form : http:// goo.gl / VhLvcW. Markov. If you haven’t yet, snarf Markov Look at the main method in MarkovMain.java
E N D
Compsci 201 Recitation 6 Professor Peck Jimmy Wei 2/14/2014
In this Recitation • Markov! • Google form: • http://goo.gl/VhLvcW
Markov • If you haven’t yet, snarf Markov • Look at the main method in MarkovMain.java • We will implement a new subclass of AbstractModel called MapMarkovModel, then we will change the main method as follows: • Answer #1 public static void main(String[] args) { IModel model = new MarkovModel(); SimpleViewer view = new SimpleViewer(); view.setModel(model); } public static void main(String[] args) { IModel model = new MapMarkovModel(); SimpleViewer view = new SimpleViewer(); view.setModel(model); }
Markov • Here some of the code for the brute() method in MarkovModel; use it to answer #2-5: public void brute(int k, intnumLetters) { int start = myRandom.nextInt(myString.length() – k + 1); String str = myString.substring(start, start + k); String wrapAroundString = myString + myString.substring(0, k); ArrayList<Character> list = new ArrayList<Character>(); for (inti=0; i<numLetters, i++) { list.clear(); intpos = 0; while ( (pos = wrapAroundString.indexOf(str, pos)) != -1 && pos < myString.length()) { char ch = wrapAroundString.charAt(pos + k); list.add(ch); pos++; } // more code below
Markov • In MapMarkovModel, you will create a map with String keys and ArrayList<String> values, with the keys representing n-grams in the file and values representing n-grams following those keys. • Use the following sample map generation code to answer #6-9: for (int j=0; j<myString.length(); j++) { String ngram = myWrapAroundString.substring(j, j+k); if (!myMap.containsKey(ngram)) { myMap.put(ngram, new ArrayList<String>()); } ArrayList<String> list = myMap.get(ngram); list.add(VALUE_NEEDED_HERE); }
Markov • The assignment will also have you implement the WordNgram class representing a sequence of k words (i.e. a word k-gram) • Below is a possible implementation of equals() in WordNgram; use it to answer #10-13: public class WordNgram { private String[] myWords; public boolean equals(Object o) { WordNgram other = (WordNgram) o; for (int k=0; k<myWords.length; k++) { if (!myWords[k].equals(other.myWords[k])) { return false; } } return true; } }
Markov • As we know, since we are implementing equals() we must also implement hashCode() • Below is our default implementation for the hashCode() method; use it to answer #14 public inthashCode() { return 15; }
Markov • Here is another implementation of hashCode(); use it to answer #15: • What if we replaced the body of the for loop with this line? Answer #16: public inthashCode() { int sum = 0; for (int k=0; k<myWords.length; k++) { sum += myWords[k].hashCode(); } return sum; } sum = 100 * sum + myWords[k].hashCode();
Markov • If you’ve made it this far, you’re almost done! One last question—answer #17! • Once you finish, submit the Google form to get credit for today’s recitation!