import java.io.*;
import java.util.*;
import java.net.*;
//_____________________________________________________________
public class gen
{ 
    String[] file2 = getFile("desc2.txt").split("\r\n");
   int[] lk2 = new int[28];
//_____________________________________________________________
public static final void main(String[] args) throws Exception
{
   gen bnk = new gen();
   bnk.start();
}
//_____________________________________________________________
gen() throws Exception 
{
}
//___________________________________________________________________________
void start()
{
   String[] file = getFile("torah.txt").split("\r\n");
   String gen2 = file[0];
   String gen3 = gen2.replace(" ","");
   byte[] bb = gen3.getBytes();
   String[] words = gen2.split(" ");
   boolean found = false;
   System.out.println(gen2);
   HashMap hash = new HashMap();
   int hashpos = 0;
   StringBuffer buf = new StringBuffer();
   for (int i = 0; i < file.length; i++)
   {
      String[] words2 = file[i].split(" ");
      for (int j = 0; j < words2.length; j++)
      {
         if (hash.get(words2[j]) == null)
         {
            hash.put(words2[j],hashpos++);
            buf.append(words2[j] + " ");
         }
      }
   }
   words = buf.toString().split(" ");
   int cnt = 0;
   buf = new StringBuffer();
   buf.append("<table border='1'>\r\n");
   buf.append("<tr>");
   for (int i = 28; i >= 1; i--)
   {
      buf.append("<td>" + i + "</td>");
   }
   buf.append("<td>word</td><td>start</td><td>offset</td><td>English</td>");
   buf.append("</tr>");
   for (int i = 0; i < words.length; i++)
   {
      String words3 = words[i];
      if (words[i].length() == 1) words3 = words3 + "AAA";
      else if (words[i].length() == 2) words3 = words3 + "AA";
      else if (words[i].length() == 3) words3 = words3 + "A";
      byte[] bc = words3.getBytes();
      for (int j = 2; j < bb.length; j++)
      {
         for (int k = 0; k < bb.length; k++)
         {
            found = true;
            for (int m = 0; m < bc.length; m++)
            {
               int pos = (k + (m * j)); // % bb.length;
               if (pos >= bb.length) 
               {
                  found = false;
                  continue;
               }
               if (bb[pos] != bc[m])
               {
                  found = false;
                  break;
               }
            }
            if (found)
            {
               cnt++;
               buf.append("<tr>");
               buf.append(show(gen3,words[i],j,k) + "");
               buf.append("<td><font size='7' color='red'>" + convert(words[i]) + "</font></td><td>" + (k+1) + "</td><td>" + j + "</td>");
               buf.append("<td>" + convert(find(words[i])) + "</td>");
               buf.append("</tr>\r\n");
               break;
            }
         }
         if (found) break;
      }
   }
   
   System.out.println(cnt);
   buf.append("<tr>");
   for (int i = lk2.length - 1; i >= 0; i--)
   {
      buf.append("<th>" + lk2[i] + "</th>");
      System.out.print(lk2[i]);
   }
   buf.append("<th colspan='4'>totals</th></tr>\r\n");
   buf.append("</table>");
   saveFile("gen11words.htm",buf.toString(),false);
}
//___________________________________________________________________________
String show(String gen3, String word, int jj, int kk)
{
   int[] lk = new int[gen3.length()];
   StringBuffer buf = new StringBuffer();
   StringBuffer buf2 = new StringBuffer();
   StringBuffer buf3 = new StringBuffer();
   int pos = kk + (word.length() * jj) + 0;
   for (int i = kk; i < pos; i += jj)
   {
      lk[i]++;
      lk2[i]++;
      buf2.append(gen3.substring(i,i+1));  
   }
   if (!buf2.toString().equals(word))System.out.println("ERROR " + buf2.toString());
   for (int i = gen3.length() - 1; i >= 0; i--)
   {
      String s = gen3.substring(i,i+1);
      buf3.append(lk[i]);
      buf.append("<td>");
      if (lk[i] > 0) buf.append("<font size='6' color='red'>");
      buf.append(convert(s));
      if (lk[i] > 0) buf.append("</font>");
      buf.append("</td>");
   }
   return(buf.toString());
}
//___________________________________________________________________________
String find(String fnd)
{
   for (int i = 0; i < file2.length; i++)
   {
      String[] words = file2[i].split(" ");
      if (words[0].trim().equals(fnd))
      {
         return(file2[i]);
      }
   }
   return("");
}
//___________________________________________________________________________
String getFile(String file) 
{
   StringBuffer buf=new StringBuffer();String str;
  try
  {
   BufferedReader in = new BufferedReader (new FileReader (file));
   while((str=in.readLine())!=null)
   {
      buf.append(str+"\r\n");
   }
   in.close();
  }
  catch (Exception e)
  {
   System.out.println(e);
  }
   return (buf.toString());
}
//___________________________________________________________________________
void saveFile(String file, String str, boolean append) 
{
  try
  {
   BufferedWriter out = new BufferedWriter(new FileWriter(file, append));
   out.write(str);
   out.close();
  }
  catch (Exception e)
  {
   System.out.println(e);
  }
}
//_____________________________________________________
String convert(String tr)
{

//if (!hebrew) return(tr);

   String out = "";
   byte[] bb = tr.getBytes();
   for (int i = 0; i < bb.length; i++)
   {
      //out = "";
      //int val = tr.charCodeAt(i);
      int val = bb[i];
      if (val < 0) val += 256;
      if (val == 65) out += "&#x05d0";
      else if (val == 66) out += "&#x05d1";
      else if (val == 71) out += "&#x05d2";
      else if (val == 68) out += "&#x05d3";
      else if (val == 72) out += "&#x05d4";
      else if (val == 86) out += "&#x05d5";
      else if (val == 90) out += "&#x05d6";
      else if (val == 67) out += "&#x05d7";
      else if (val == 84) out += "&#x05d8";
      else if (val == 89) out += "&#x05d9";
      else if (val == 75) out += "&#x05da";
      else if (val == 75) out += "&#x05db";
      else if (val == 76) out += "&#x05dc";
      else if (val == 77) out += "&#x05dd";
      else if (val == 77) out += "&#x05de";
      else if (val == 78) out += "&#x05df";
      else if (val == 78) out += "&#x05e0";
      else if (val == 83) out += "&#x05e1";
      else if (val == 73) out += "&#x05e2";
      else if (val == 70) out += "&#x05e3";
      else if (val == 70) out += "&#x05e4";
      else if (val == 69) out += "&#x05e5";
      else if (val == 69) out += "&#x05e6";
      else if (val == 81) out += "&#x05e7";
      else if (val == 82) out += "&#x05e8";
      else if (val == 74) out += "&#x05e9";
      else if (val == 79) out += "&#x05ea";
      else out += (char)val;
   }
   return(out);
}
}//__________________________________________________________________________
