/**
 * File: TrigramCount.java
 * @author A.Kurd <alan.kurd@trincoll.edu>
 * 
 * Description: Implements a Java 1.1 version of a GUI tool for cycling through an encrypted message and collecting
*  how often a group of three letters appear in the message.  It then orders the trigrams in descending order based
*  on their frequency.
 * Credits: This program is modelled after the frequency test of the CryptoToolJ program by Ralph Morelli.
 *
 * Copyright: This program is in the public domain. You can do whatever you want with
 *  it as long as I get credit for my work and as long as you offer your changes to me
 *  so I can possible add them to the "official" version.
 */
package hcrypto.analyzer;

import hcrypto.cipher.*;
import java.awt.*;

public class TrigramCount {  

    public  String message; 
    private TextArea text;     // Pointers to the text to be analyzed
    public  char frequencies [] [] = new char [128] [3];  //collects the trigrams that appear in the message
    public  int acc [] = new int [128];		//collects the frequency of each trigram

    public  void TrigramCount(String text) {
        message = text;
    } //trigramCount

/*This method counts the frequency of the trigrams in a message.  It cycles through the whole message collecting groups
   of three letters.  If that groups already exists in the frequency array, it then increments the occurence of the trigram by 1 in 
    the accumulate array (acc[]).  Later these are ordered in descending order based on trigram frequency.*/

    public  void count(String text) {        
        int i;
        for (int k = 0; (k+2)<128; k++){
            char ch = text.charAt(k);
            char ch2=text.charAt(k+1);
            char ch3=text.charAt(k+2);   
            for (i = 0; i < k; i++) 
               if (frequencies[i] [0] == ch && frequencies[i] [1] == ch2 && frequencies[i] [2] == ch3){
                  acc [i]++;    
                  break;}  //if and for inside
            if (i == k){    
               frequencies[k] [0] = ch;
               frequencies[k] [1] = ch2;
               frequencies[k] [2] = ch3;
               acc [k] = 1;
            }//if
        }//for outside

      int temp;
      char temp1, temp2, temp3;
      for (int pass = 1; pass < frequencies.length; pass++)
         for (int pair = 1; pair < frequencies.length; pair++)
            if (acc[pair -1] < acc[pair]){
               temp = acc[pair-1];
               temp1 = frequencies[pair-1][0];
               temp2 = frequencies[pair-1][1];
               temp3 = frequencies[pair-1][2];
               acc[pair-1] = acc[pair];
               frequencies[pair-1][0] = frequencies[pair][0];
               frequencies[pair-1][1] = frequencies[pair][1];
               frequencies[pair-1][2] = frequencies[pair][2];
               acc[pair] = temp;
               frequencies[pair][0] = temp1;
               frequencies[pair][1] = temp2;
               frequencies[pair][2] = temp3;
            }//if      
    }  //count
}//class