From: Simon F. <sim...@us...> - 2005-03-16 07:45:32
|
Update of /cvsroot/jcrypt/JCrypt/cs199mbc/plugins In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv1832 Added Files: AmericanEnglishLang.java DotProduct.java Log Message: adding dot product plugin/removing playfair --- NEW FILE: AmericanEnglishLang.java --- package cs199mbc.plugins; public class AmericanEnglishLang { public int fromTotal() { return 440354; } public int[] getFreqs() { return m; } public String toString() { return "American English"; } public AmericanEnglishLang() { m = new int[26]; m[0] = 33802; m[1] = 8884; m[2] = 10967; m[3] = 19540; m[4] = 53910; m[5] = 9082; m[6] = 9915; m[7] = 27244; m[8] = 28654; m[9] = 886; m[10] = 5061; m[11] = 18156; m[12] = 10750; m[13] = 29801; m[14] = 35860; m[15] = 7155; m[16] = 330; m[17] = 24467; m[18] = 24817; m[19] = 41429; m[20] = 14335; m[21] = 3397; m[22] = 11448; m[23] = 745; m[24] = 9452; m[25] = 267; } private int[] m; } --- NEW FILE: DotProduct.java --- /* DotProduct Class Copyright (C) 2005 Silas Snider This program is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ package cs199mbc.plugins; import java.util.*; import cs199mbc.Informer; public class DotProduct implements Informer { public static final String toName = "DotProduct", toString = "Dot Product"; private String text; private int[] alphaCounts = new int[26]; private String alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; public DotProduct() { Arrays.fill(alphaCounts, 0); } public String getInfo(String inp) { return doStuff(inp); } public String doStuff(String s) { int temp = -1; s = s.toUpperCase(); for (int i = 0; i < s.length(); i++) { if ((temp = alpha.indexOf(s.charAt(i))) != -1) { alphaCounts[temp]++; } } int[] l = new AmericanEnglishLang().getFreqs(); int prd; int[] prods = new int[26]; for (int i = 0; i < 26; i++) { l = shift(l); prd = 0; for (int j = 0; j < 26; j++) { prd += (alphaCounts[j] * l[j]); } prods[i] = prd; } int max = 0; for (int i = 0; i < 26; i++) { if (prods[i] > prods[max]) { max = i; } } StringBuffer f = new StringBuffer(); f.append("Dot Product"); f.append("Best fit is: "); f.append(alpha.charAt(max)); f.append("\n"); for (int i = 0; i < 26; i++) { f.append("Shifted by "); f.append(alpha.charAt(i)); f.append(": "); f.append(prods[i]); f.append("\n"); } return f.toString(); } private int[] shift(int[] l) { int hld = l[l.length - 1]; for (int i = (l.length - 2); i > -1; i--) { l[i + 1] = l[i]; } l[0] = hld; return l; } } |