Presentation is loading. Please wait.

Presentation is loading. Please wait.

Compsci 201 Recitation 6 Professor Peck Jimmy Wei 2/14/2014 1.

Similar presentations


Presentation on theme: "Compsci 201 Recitation 6 Professor Peck Jimmy Wei 2/14/2014 1."— Presentation transcript:

1 Compsci 201 Recitation 6 Professor Peck Jimmy Wei 2/14/2014 1

2 In this Recitation Markov! Google form: http://goo.gl/VhLvcW 2

3 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(); IModel model = new MarkovModel(); SimpleViewer view = new SimpleViewer(); SimpleViewer view = new SimpleViewer(); view.setModel(model); view.setModel(model);} public static void main(String[] args) { IModel model = new MarkovModel(); IModel model = new MarkovModel(); SimpleViewer view = new SimpleViewer(); SimpleViewer view = new SimpleViewer(); view.setModel(model); view.setModel(model);} public static void main(String[] args) { IModel model = new MapMarkovModel(); IModel model = new MapMarkovModel(); SimpleViewer view = new SimpleViewer(); SimpleViewer view = new SimpleViewer(); view.setModel(model); view.setModel(model);} public static void main(String[] args) { IModel model = new MapMarkovModel(); IModel model = new MapMarkovModel(); SimpleViewer view = new SimpleViewer(); SimpleViewer view = new SimpleViewer(); view.setModel(model); view.setModel(model);} 3

4 Markov Here some of the code for the brute() method in MarkovModel; use it to answer #2-5: public void brute(int k, int numLetters) { int start = myRandom.nextInt(myString.length() – k + 1); String str = myString.substring(start, start + k); String wrapAroundString = myString + myString.substring(0, k); ArrayList list = new ArrayList (); for (int i=0; i<numLetters, i++) { list.clear(); int pos = 0; while ( (pos = wrapAroundString.indexOf(str, pos)) != -1 && pos < myString.length()) { char ch = wrapAroundString.charAt(pos + k); list.add(ch);pos++;} // more code below public void brute(int k, int numLetters) { int start = myRandom.nextInt(myString.length() – k + 1); String str = myString.substring(start, start + k); String wrapAroundString = myString + myString.substring(0, k); ArrayList list = new ArrayList (); for (int i=0; i<numLetters, i++) { list.clear(); int pos = 0; while ( (pos = wrapAroundString.indexOf(str, pos)) != -1 && pos < myString.length()) { char ch = wrapAroundString.charAt(pos + k); list.add(ch);pos++;} // more code below 4

5 Markov In MapMarkovModel, you will create a map with String keys and ArrayList 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 ()); } ArrayList list = myMap.get(ngram); list.add(VALUE_NEEDED_HERE);} for (int j=0; j<myString.length(); j++) { String ngram = myWrapAroundString.substring(j, j+k); if (!myMap.containsKey(ngram)) { myMap.put(ngram, new ArrayList ()); } ArrayList list = myMap.get(ngram); list.add(VALUE_NEEDED_HERE);} 5

6 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; }} 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; }} 6

7 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 int hashCode() { return 15; } public int hashCode() { return 15; } 7

8 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 int hashCode() { int sum = 0; for (int k=0; k<myWords.length; k++) { sum += myWords[k].hashCode(); } return sum; } public int hashCode() { int sum = 0; for (int k=0; k<myWords.length; k++) { sum += myWords[k].hashCode(); } return sum; } sum = 100 * sum + myWords[k].hashCode(); 8

9 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! 9

10 Have a good weekend! 10


Download ppt "Compsci 201 Recitation 6 Professor Peck Jimmy Wei 2/14/2014 1."

Similar presentations


Ads by Google