import java.util.*; import java.text.DecimalFormat; // Milestone 3: bigram/unigram/total tallies from String/TokenReader class Tally { final static String ALPHABET = " abcdefghijklmnopqrstuvwxyz"; final static int NSYMBOLS = ALPHABET.length(); final int SPACE = 0; // array index for space final int OFFSET = 'a' - 1; // offset to shift char code to array index int[] unigram = new int[NSYMBOLS]; int[][] bigram = new int[NSYMBOLS][NSYMBOLS]; int total = 0; // create new Tally with tallies from s Tally(String s) { tallybigram(s); tallyunigram(); } // create new Tally with tallies from in Tally(TokenReader in) { for (String s = in.readLine(); !in.eof(); s = in.readLine()) tallybigram(s); tallyunigram(); } // add bigram tallies of t to current tallies private void tallybigram(String t) { char[] s = t.toLowerCase().toCharArray(); // below, "array code" = the indices into $unigram$, $bigram$ // = positions of characters in $alphabet$ int prev = SPACE; // array code for previous char, initially ' ' int c; // array code for next char to process for (int i = 0; i=0 && top.charAt(where) != c) where--; if (where>=0) c = bottom.charAt(where); s[i] = (char) (c + bias); } return new String(s); } // slower version: if add "missing" character to ends of // top and bottom, then it will be found, unchanged! // problem: adding to the end of a String is slow -- // it takes time proportional to length of the String. static String transform2(String top, String bottom, String text) { top = top.toLowerCase(); bottom = bottom.toLowerCase(); String lower = text.toLowerCase(); String s = ""; for (int i = 0; i < text.length(); i++) { char c = lower.charAt(i); int bias = text.charAt(i) - c; // lower-to-upper shift, if any int where = (top + c).indexOf(c); c = (bottom + c).charAt(where); s += (char) (c + bias); } return s; } // Milestone 3 -- see class Tally // Milestone 4 // return L1 distance between equal-sized arrays a, b static int distance(int[] a, int[] b) { int d = 0; for (int i = 0; i < a.length; i++) d += Math.abs(a[i] - b[i]); return d; } // return L1 distance between equal-sized arrays a, b static int distance(int[][] a, int[][] b) { int d = 0; for (int i = 0; i