1 / 10

Compsci 201 Recitation 6

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

jerry
Download Presentation

Compsci 201 Recitation 6

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

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

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

  4. 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

  5. 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); }

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

  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 inthashCode() { return 15; }

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

  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!

  10. Have a good weekend!

More Related