hcrypto.analyzer.evolutionary
Class NgramArray

java.lang.Object
  extended by hcrypto.analyzer.evolutionary.NgramArray
All Implemented Interfaces:
Analyzer

public class NgramArray
extends java.lang.Object
implements Analyzer


Field Summary
 
Fields inherited from interface hcrypto.analyzer.Analyzer
DECIPHER_LIMIT
 
Constructor Summary
NgramArray(int N, java.lang.String fileName, Alphabet alph)
          NgramArray(N, filename, alph) - constructor NN = N is the size of the N-gram N=2, 3, or 4 filename is a file containing a large text typical of the language of the cryptotext alphabet = alph describes which letters occur in the N-grams freq = the distribution of letters in text.
NgramArray(int N, java.lang.String fileName, Alphabet alph, int step)
          NgramArray(N, filename, alph) - constructor NN = N is the size of the N-gram N=2, 3, or 4
 
Method Summary
 double absDiffBigramDist(java.lang.String ctext)
          absDiffBigramDist(ctext) Computes a distance between this NgramArray and the distribution of bigrams in the string ctext.
 double freqDist(int[] cnums)
          Computes a distance between this NgramArray and the distribution of N-grams of a crypto text as represented by an array of int values.
 double freqDist(java.lang.String ctext)
          Computes a distance between this NgramArray and the distribution of N-grams in the string ctext.
 Alphabet getAlphabet()
           
 int getAlphSize()
           
 int getArrSize()
           
 double getFreq(char ch)
           
 java.lang.String getMostFreqWord(int k)
           
 double getNgramFreq(java.lang.String ngram)
           
 double getNGramProb(java.lang.String ngram)
          getNgramProb() computes the probability of an ngram.
 int getNN()
           
 java.lang.String getReport()
          From the Analyzer interface.
 int getStepSize()
           
static void main(java.lang.String[] args)
           
 void print()
           
 double recipDist(int[] cnums)
          Computes a distance between this NgramArray and the distribution of N-grams of a crypto text as represented by an array of int values.
 double recipDist(java.lang.String ctext)
          Computes a distance between this NgramArray and the distribution of N-grams in the string ctext.
 double recipDist(java.lang.String ctext, double[] scores)
          Same as previous method except this one returns a double array that remembers the scores at each index of the cryptogram.
 double recipDist(java.lang.String ctext, int[] indx)
          Same as previous method except this one returns an array that remembers the indices of the smallest and greatest nGram scores in the cryptogram.
 double recipDistSkip(java.lang.String ctext)
          Same as previous method except this one skips stepsize characters.
 void run()
          From the Analyzer interface.
 void setStepSize(int newStep)
           
 void setup(java.lang.String fileName)
          From the Analyzer interface.
 double sqrDiffBigramDist(java.lang.String ctext)
          sqrDiffBigramDist(ctext) Computes a distance between this NgramArray and the distribution of bigrams in the string ctext.
 double sqrDist(int[] cnums)
          Computes a distance between this NgramArray and the distribution of N-grams of a crypto text as represented by an array of int values.
 double sqrDist(java.lang.String ctext)
          Computes a distance between this NgramArray and the distribution of N-grams in the string ctext.
 java.lang.String toString()
          Puts characters with frequencies into a string.
 void writeFreqWordsToFile(java.lang.String fileName)
           
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

NgramArray

public NgramArray(int N,
                  java.lang.String fileName,
                  Alphabet alph)
           throws java.lang.Exception
NgramArray(N, filename, alph) - constructor NN = N is the size of the N-gram N=2, 3, or 4 filename is a file containing a large text typical of the language of the cryptotext alphabet = alph describes which letters occur in the N-grams freq = the distribution of letters in text.

Throws:
java.lang.Exception

NgramArray

public NgramArray(int N,
                  java.lang.String fileName,
                  Alphabet alph,
                  int step)
           throws java.lang.Exception
NgramArray(N, filename, alph) - constructor NN = N is the size of the N-gram N=2, 3, or 4

Parameters:
filename - is a file containing a large text typical of the language of the cryptotext
alphabet - = alph describes which letters occur in the N-grams
step - gives the step size >=1 freq = the distribution of letters in text.
Throws:
java.lang.Exception
Method Detail

setup

public void setup(java.lang.String fileName)
From the Analyzer interface. In this case the freq array is calculated. The element freq[index] is the reciprocal of (1 + frequency of the N-gram) represented by index in the text read from a file.

Specified by:
setup in interface Analyzer

getReport

public java.lang.String getReport()
From the Analyzer interface.

Specified by:
getReport in interface Analyzer

run

public void run()
From the Analyzer interface. Everything is done in setup.

Specified by:
run in interface Analyzer

toString

public java.lang.String toString()
Puts characters with frequencies into a string.

Overrides:
toString in class java.lang.Object

print

public void print()

getAlphSize

public int getAlphSize()

getAlphabet

public Alphabet getAlphabet()

getNN

public int getNN()

getNGramProb

public double getNGramProb(java.lang.String ngram)
                    throws java.lang.Exception
getNgramProb() computes the probability of an ngram. The value is the frequency of the ngram divided by the product of the frequencies of its individual letters.

Throws:
java.lang.Exception

getFreq

public double getFreq(char ch)
               throws java.lang.Exception
Throws:
java.lang.Exception

getNgramFreq

public double getNgramFreq(java.lang.String ngram)
                    throws java.lang.Exception
Throws:
java.lang.Exception

getArrSize

public int getArrSize()

setStepSize

public void setStepSize(int newStep)

getStepSize

public int getStepSize()

getMostFreqWord

public java.lang.String getMostFreqWord(int k)

writeFreqWordsToFile

public void writeFreqWordsToFile(java.lang.String fileName)

absDiffBigramDist

public double absDiffBigramDist(java.lang.String ctext)
                         throws java.lang.Exception
absDiffBigramDist(ctext) Computes a distance between this NgramArray and the distribution of bigrams in the string ctext. The distance is obtained by summing the absolute values of the relative frequency of the birams in the text used to create this NgramArray object minus the relative frequency of the corresponding bigram in ctext.

Throws:
java.lang.Exception

sqrDiffBigramDist

public double sqrDiffBigramDist(java.lang.String ctext)
                         throws java.lang.Exception
sqrDiffBigramDist(ctext) Computes a distance between this NgramArray and the distribution of bigrams in the string ctext. The distance is obtained by summing the squares of the relative frequency of the birams in the text used to create this NgramArray object minus the relative frequency of the corresponding bigram in ctext.

Throws:
java.lang.Exception

recipDist

public double recipDist(java.lang.String ctext)
                 throws java.lang.Exception
Computes a distance between this NgramArray and the distribution of N-grams in the string ctext. The distance is obtained by summing the reciprocals of the relative frequency of the N-gram in the text used to creat this NgramArray object.

Throws:
java.lang.Exception

recipDistSkip

public double recipDistSkip(java.lang.String ctext)
                     throws java.lang.Exception
Same as previous method except this one skips stepsize characters.

Throws:
java.lang.Exception

recipDist

public double recipDist(java.lang.String ctext,
                        int[] indx)
                 throws java.lang.Exception
Same as previous method except this one returns an array that remembers the indices of the smallest and greatest nGram scores in the cryptogram.

Throws:
java.lang.Exception

recipDist

public double recipDist(java.lang.String ctext,
                        double[] scores)
                 throws java.lang.Exception
Same as previous method except this one returns a double array that remembers the scores at each index of the cryptogram.

Throws:
java.lang.Exception

recipDist

public double recipDist(int[] cnums)
                 throws java.lang.Exception
Computes a distance between this NgramArray and the distribution of N-grams of a crypto text as represented by an array of int values. The distance is obtained by summing the corresponding reciprocals of the relative frequency of the N-gram in the text used to creat this NgramArray object. This function is more efficient than the previous function when used in an N-gram hill climber.

Throws:
java.lang.Exception

freqDist

public double freqDist(java.lang.String ctext)
                throws java.lang.Exception
Computes a distance between this NgramArray and the distribution of N-grams in the string ctext. The distance is obtained by summing the the relative frequency of the N-gram in the text used to creat this NgramArray object.

Throws:
java.lang.Exception

freqDist

public double freqDist(int[] cnums)
                throws java.lang.Exception
Computes a distance between this NgramArray and the distribution of N-grams of a crypto text as represented by an array of int values. The distance is obtained by summing the corresponding relative frequency of the N-gram in the text used to creat this NgramArray object. This function is more efficient than the previous function when used in an N-gram hill climber.

Throws:
java.lang.Exception

sqrDist

public double sqrDist(java.lang.String ctext)
               throws java.lang.Exception
Computes a distance between this NgramArray and the distribution of N-grams in the string ctext. The distance is obtained by summing the squares of the relative frequency of the N-gram in the text used to creat this NgramArray object.

Throws:
java.lang.Exception

sqrDist

public double sqrDist(int[] cnums)
               throws java.lang.Exception
Computes a distance between this NgramArray and the distribution of N-grams of a crypto text as represented by an array of int values. The distance is obtained by summing the corresponding squares of the relative frequency of the N-gram in the text used to creat this NgramArray object. This function is more efficient than the previous function when used in an N-gram hill climber.

Throws:
java.lang.Exception

main

public static void main(java.lang.String[] args)