import java.io.*;
import java.nio.*;
import java.util.*;
import java.net.*;
import java.math.*;
//_____________________________________________________________
public class books
{ 
   String gen = "BRAJYO BRA ALHYM AO HJMYM VAO HARE";
   String s1 = "ALHYMHJMYMHARE";
   String s2 = "BRAJYOBRAAOVAO";
   int total = 0;
   int total7 = 0;
   int totalb = 0;
   int totalltrs = 0;
   int[] totaladj = new int[29];
   int totaladj2 = 0;
   int totalnouns = 0;
   int totalends = 0;
   int totalthree = 0;
   int totalthird = 0;
   int totalpi = 0;
   int totale = 0;
   int totala = 0;
   int ispie = 0;
   int thepie = 0;
   int[] arypie = new int[8];
   int myall = 0;
   int[] myallarray = new int[64];
   StringBuffer mysubset = new StringBuffer();
   String out = "";
   int[] four = new int[16];
//_____________________________________________________________
public static final void main(String[] args) throws Exception
{
   long t1 = System.nanoTime();
   books bnk = new books();
   bnk.start();
   //bnk.start3();
   long t2 = System.nanoTime();
   System.out.println("seconds = " + ((t2-t1)/1e9));
}
//_____________________________________________________________
books() throws Exception 
{
}
//_____________________________________________________________
void start3() 
{
   String str = "BRAJYO BRA ALHYM AO HJMYM VAO HARE";
   System.out.println(pie(str));
}
//_____________________________________________________________
void start()throws Exception
{
   if ( makedir("list")) start2();
   String[] list = getdir("list");
   System.out.println("count = " + list.length);
   for (int i = 0; i < list.length; i++)
   {
      System.out.println(i + " " + list[i].substring(5));
      checksentencelen(list[i]);
   }
   System.out.println("");
   System.out.println("number of pseudo sentences = " + totalb);
   System.out.println("");
   if (total7 == 0)
   {
      System.out.println(total + " " + total7 + " infinite");
   }
   else
   {
      System.out.println(total + " " + total7 + " " + (total/total7));
   }
   if (totaladj2 == 0)
   {
      System.out.println("adjacent = " + totaladj2 + " infinite");
   }
   else
   {
      System.out.println("adj = " + totaladj2 + " " + (totalb / totaladj2));
   }
   if (totalnouns == 0)
   {
      System.out.println("nouns = " + totalnouns + " infinite");
   }
   else
   {
      System.out.println("nouns = " + totalnouns + " " + (totalb / totalnouns));
   }
   if (totalends == 0)
   {
      System.out.println("ends = " + totalends + " infinite");
   }
   else
   {
      System.out.println("ends = " + totalends + " " + (totalb / totalends));
   }
   if (totalthree == 0)
   {
      System.out.println("three = " + totalthree + " infinite");
   }
   else
   {
      System.out.println("three = " + totalthree + " " + (totalb / totalthree));
   }
   if (totalthird == 0)
   {
      System.out.println("third = " + totalthird + " infinite");
   }
   else
   {
      System.out.println("third = " + totalthird + " " + (totalb / totalthird));
   }
   if (totalpi == 0)
   {
      System.out.println("pi = " + totalpi + " infinite");
   }
   else
   {
      System.out.println("pi = " + totalpi + " " + (totalb / totalpi));
   }
   if (totale == 0)
   {
      System.out.println("e = " + totale + " infinite");
   }
   else
   {
      System.out.println("e = " + totale + " " + (totalb / totale));
   }
   if (totala == 0)
   {
      System.out.println("alpha = " + totala + " infinite ");
   }
   else
   {
      System.out.println("alpha = " + totala + " " + (totalb / totala));
   }
   if (ispie == 0)
   {
      System.out.println("isall = " + ispie + " infinite");
   }
   else
   {
      System.out.println("ispie = " + ispie + " " + (totalb / ispie));
   }
   for (int i = 0; i < arypie.length; i++)
   {
      System.out.println(i + " " + arypie[i]);
   }
}
//___________________________________________________________________________
void start2()
{
   String s = "http://www.textfiles.com/etext/FICTION/";
   String[] files = getUrl(s).split("\r\n");
   for (int i = 0; i < files.length; i++)
   {
      int pos = files[i].indexOf("A HREF=");
      if (pos >= 0)
      {
         int pos2 = files[i].indexOf("\"",pos+9);
         String s2 = files[i].substring(pos+8,pos2);
         String s3 = s + s2;
         System.out.println(s3);
         String file = getUrl(s3);
         saveFile("list/" + s2, file, false);
      }
   }
}
//_______________________________________________________________________
int checksentencelen(String name)
{
   int pipos = 0;
   int pival = 0;
   StringBuffer buf = new StringBuffer();
   String[] lines = getFile(name).split("\r\n");
   for (int i = 0; i < lines.length; i++)
   {
      buf.append(lines[i] + " ");
   }
   String full = buf.toString();
   boolean cont = true;
   int pos = 0;
   int posold = 0;
   int count1 = 0;
   int count2 = 0;
   while (cont)
   {  
      posold = pos;
      pos = full.indexOf(".",pos+1);
      if (pos < 0) break;
      String sentence = full.substring(posold,pos);
      String[] words = sentence.split(" ");
      count1++;
      if (words.length % 7 == 0) count2++;
   }
   total += count1;
   total7 += count2;
   String[] words = full.split(" ");
   buf = new StringBuffer();
   for (int i = 0; i < words.length; i++)
   {
      String newwords = clean(words[i]);
      if (!newwords.equals("")) buf.append(newwords + " ");
   }
   String[] thewords = buf.toString().split(" ");
   for (int i = 0; i < thewords.length - 6; i++)
   {
      StringBuffer buf2 = new StringBuffer();
      for (int j = 0; j < 7; j++)
      {
         if (j > 0) buf2.append(" ");
         buf2.append(thewords[i+j]);
      }
      String sentence = buf2.toString();
      String[] thewords2 = sentence.split(" ");
      String noblanks = sentence.replace(" ","");
      int cnt = 0;
      myall = 0;
      if (noblanks.length() % 7 == 0) myall += 1;
      cnt = checkadjacent(sentence);
      if (cnt >= 7) 
      {
         myall += 2;
         totaladj2++;
      }
      totaladj[cnt] ++;
      String s1 = thewords2[2] + thewords2[4] + thewords2[6];
      String s2 = thewords2[0] + thewords2[1] + thewords2[3] + thewords2[5];
      if (checknouns(s1,s2)) 
      {
         myall += 4;
         totalnouns++;
      }
      if (noblanks.length() % 7 == 0) totalltrs++;
      totalb++;     
      if (checkends(noblanks,sentence,thewords2) == 255)
      {
         myall += 8;
         totalends++;
      }
      if (checkthree(thewords2[0],thewords2[1],thewords2[2]) == 15)
      {
         myall += 16;
         totalthree++;
      }
      if (checkthird(thewords2[2]) == 15)
      {
         myall += 32;
         totalthird++;
      }
      String mypie = pie(sentence);
      int thepie = 0;
      if (mypie.length() >= 5) mypie = mypie.substring(0,5);
      if (mypie.equals("31415")) 
      {
         pipos = i;
         pival = value(sentence);
         totalpi++;
         thepie += 1;
      }
      else if (mypie.equals("27183")) 
      {
          thepie += 2;
          totale++;
      }
              int eval = value(sentence);
              String  ans2 = Integer.toString(pival) + Integer.toString(eval);
              int ans = Integer.parseInt(ans2);
              ans = ans * ans;
              String strans = Integer.toString(ans);
              String strsq = strans.replace(".","");
              if (strsq.length() >= 6) strsq = strsq.substring(0,6);
              if (strsq.equals("729736")) 
              {
                 thepie += 4;
                 totala++;
              }
      
      if (thepie == 7) ispie++;
      arypie[thepie]++;
      myallarray[myall]++;
   }
   return(count2);
}
//________________________________________________________
int checkadjacent(String str)
{
   int count = 0;
   String[] words = str.split(" ");
   String[] adj = adjacent(7).split("\r\n");
   for (int i = 0; i < adj.length; i++)
   {
      String[] val = adj[i].split(",");
      StringBuffer buf = new StringBuffer();
      for (int j = 0; j < val.length; j++)
      {
         int pos = Integer.parseInt(val[j]);
         buf.append(words[pos] + " ");
      }
      String noblanks = buf.toString().replace(" ","");
      if (noblanks.length() % 7 == 0) count++;
   }
   return(count);
}
//_______________________________________________________
boolean checknouns(String nouns, String other)
{
   int val = 0;
   if (nouns.length() % 7 == 0) val += 1;
   if (other.length() % 7 == 0) val += 2;
   if (place(nouns) % 7 == 0) val += 4;
   if (value(nouns) % 7 == 0) val += 8;
   four[val]++;
   if (nouns.length() % 7 == 0)
   {
      if (other.length() % 7 == 0)
      {
         if (place(nouns) % 7 == 0)
         {
            if (value(nouns) % 7 == 0)
            {
               return(true);
            }
         }
      }     
   }
   return(false);
}
//______________________________________________________________
int checkends(String noblanks, String sentence, String[] words)
{
   int val = 0;
   String left = "";
   String right = "";
   String middle = "";
   String lmr = "";
   int len = noblanks.length();
   int half = len / 2;
   if (noblanks.length() % 2 == 0)
   {
      left = noblanks.substring(0,1) + noblanks.substring(half-1,half+0);
      right = noblanks.substring(half,half+1) + noblanks.substring(len-1);
      middle = noblanks.substring(half-1,half+1);
   }
   else
   {
      left = noblanks.substring(0,1) + noblanks.substring(half,half+1);
      right = noblanks.substring(half,half+1) + noblanks.substring(len-1);
      middle = noblanks.substring(half, half+1);
   }
   if (value(left) % 7 == 0) val += 1;
   if (value(right) % 7 == 0) val += 2;
   lmr = noblanks.substring(0,1) + middle + noblanks.substring(len-1);
   if (value(lmr) % 7 == 0) val += 4;
   String lastword = words[words.length-1];
   String flf = words[0].substring(0,1) + lastword.substring(0,1);
   if (value(flf) % 7 == 0) val += 8;
   String fll = words[0].substring(words[0].length()-1) + lastword.substring(lastword.length()-1);
   if (value(fll) % 7 == 0) val += 16;
   if (value(flf+fll) % 7 == 0) val += 32;
   String other = firstlast(words[1]) + firstlast(words[2]);
   other += firstlast(words[3]) + firstlast(words[4]);
   other += firstlast(words[5]);
   if (value(other) % 7 == 0) val += 64;
   if (value(flf+fll+other) % 7 == 0) val += 128;
   return(val);
}
//_______________________________________________________
int checkthree(String word1 ,String word2, String word3)
{
   String word = word1 + word2 + word3;
   int val = 0;
   int all = 0;
   int odd = 0;
   int even = 0;
   int times = 0;
   for (int i = 0; i < word.length(); i++)
   {
      String s = word.substring(i,i+1);
      int place = place(s);
      if (place % 2 == 0)
      {
         even += place;
      }
      else
      {
         odd += place;
      }
      all += place;
      times += (i+1) * place;
   }
      if (even % 7 == 0) val += 1;
      if (odd % 7 == 0) val += 2;
      if (all % 7 == 0) val += 4;
      if (times % 7 == 0) val += 8;
   return(val);
}
//____________________________________________________________
int checkthird(String word)
{
   int val = 0;
   int value = value(word);
   String valuestr = Integer.toString(value);
   int total = 0;
   int total2 = 0;
   int total3 = 0;
   int total4 = 0;
   for (int i = 0; i < valuestr.length(); i++)
   {
      String s = valuestr.substring(i,i+1);
      int vals = Integer.parseInt(s);
      total += vals;
   }
   if (total % 7 == 0) val += 1;
   for (int i = 0; i < word.length(); i++)
   {
      String s = word.substring(i,i+1);
      int value3 = value(s);
      String num = Integer.toString(value3);
      for (int j = 0; j < num.length(); j++)
      {
         String s2 = num.substring(j,j+1);
         int value2 = Integer.parseInt(s2);
         total2 += value2;
      }
   }
   if (total2 % 7 == 0) val += 2;
   for (int i = 0; i < word.length(); i++)
   {
      String s = word.substring(i,i+1);
      int value3 = place(s);
      String num = Integer.toString(value3);
      for (int j = 0; j < num.length(); j++)
      {
         String s2 = num.substring(j,j+1);
         int value2 = Integer.parseInt(s2);
         if (value2 > 0) total4++;
         total3 += value2;
      }
   }
   if (total3 % 7 == 0) val += 4;
   if (total4 % 7 == 0) val += 8;
   return(val);
}
//______________________________________________________________________
String firstlast(String word)
{
   String out = word.substring(0,1) + word.substring(word.length()-1);
   return(out);
}
//_______________________________________________________________________
String pie(String str)
{
   String[] words = str.split(" ");
   int[] values = new int[words.length];
   for (int i = 0; i < words.length; i++)
   {
      values[i] = value(words[i]);
   }
   String all = str.replace(" ","");
   int[] ltrvalues = new int[all.length()];
   for (int i = 0; i < all.length(); i++)
   {
      String s = all.substring(i,i+1);
      ltrvalues[i] = value(s);
   }
   BigInteger bg1 = new BigInteger("1");
   BigInteger bga = new BigInteger("1");
   for (int i = 0; i < values.length; i++)
   {
      BigInteger bgb = new BigInteger(Integer.toString(values[i]));
      bga = bga.multiply(bgb);
   }
   BigInteger bge = new BigInteger(Integer.toString(words.length));
   bga = bga.multiply(bge);
   BigInteger bgc = new BigInteger("1");
   
   for (int i = 0; i < ltrvalues.length; i++)
   {
      BigInteger bgd = new BigInteger(Integer.toString(ltrvalues[i]));
      bgc = bgc.multiply(bgd);
   }
   BigInteger bgf = new BigInteger(Integer.toString(all.length()));
   bgc = bgc.multiply(bgf);
   BigInteger bg0 = new BigInteger("0");
   if (bga.compareTo(bg0) != 0) bgc = bgc.divide(bga);
   return(bgc.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 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());
}
//________________________________________________________________
String clean(String in)
{
   byte[] bb = in.toUpperCase().getBytes();
   StringBuffer buf = new StringBuffer();
   for (int i = 0; i < bb.length; i++)
   {
      if ((bb[i] >= 'a') & (bb[i] <= 'z'))
      {
         buf.append((char)bb[i]);
      }
      if ((bb[i] >= 'A') & (bb[i] <= 'Z'))
      {
         buf.append((char)bb[i]);
      }

   }
   return(buf.toString());
}
//___________________________________________________________________________
String getUrl(String name)
{
   StringBuffer buf = new StringBuffer();
  try
  {
   String line;
   URL url = new URL(name);
   InputStream is = url.openStream();
   BufferedReader br = new BufferedReader(new InputStreamReader(is));
   while ((line = br.readLine()) != null) 
   {
      buf.append(line + "\r\n");
   }
  }
  catch (Exception e)
  {
   System.out.println(e);
  }
   return(buf.toString());
}
//___________________________________________________________________________
boolean makedir(String name) throws Exception
{
   File theDir = new File(name);
   if (!theDir.exists())
   {
      theDir.mkdir();
      return(true);
   }
   else
   {
      return(false);
   }
}   
//
//___________________________________________________________________________
String getsubset(int len)
{
   mysubset = new StringBuffer();
   subit(len, 0, "");
   return(mysubset.toString());
}
//___________________________________________________________________________
void subit(int val, int start, String str)
{
   for (int i = start; i < val; i++)
   {
      String str2 = str + Integer.toString(i) + ",";
      mysubset.append(str2 + "\r\n");
      subit(val, i+1, str2);
   }
}
//___________________________________________________________________________
String adjacent(int num)
{
    String[] out = getsubset(num).split("\r\n");
    StringBuffer buf = new StringBuffer();
    for (int i = 0; i < out.length; i++)
    {
       boolean isok = true;
       String[] nums = out[i].split(",");
       for (int j = 1; j < nums.length; j++)
       {
          int a = Integer.parseInt(nums[j]);
          int b = Integer.parseInt(nums[j-1])+1;
          if (a != b)
          {
             isok = false;
             break;
          }         
       }
       if (isok)
       {
          buf.append(out[i] + "\r\n");
       }
    }
    return(buf.toString());
}
//___________________________________________________________________________
String[] getdir(String dir)
{
   StringBuffer buf = new StringBuffer();
  try
  {
   File file = new File(dir); //("c:/temp/");
   File[] files = file.listFiles();
   for (int i = 0; i < files.length; i++) 
   {
      //System.out.println(files[i]);
      buf.append(files[i] + "\r\n");
   }
  }
  catch (Exception e)
  {
   System.out.println(e);
  }
   return(buf.toString().split("\r\n"));
}
//__________________________________________________
int value(String str)
{
   int total = 0;
   out = "";
   for (int i = 0; i < str.length(); i++)
   {
      int v = 0;
      String st2 = str.substring(i,i+1);
      byte[] st3 = st2.getBytes();
      byte st = st3[0];
      if (st == 'a') v = 1;
      else if (st == 'b') v = 2;
      else if (st == 'c') v = 3;
      else if (st == 'd') v = 4;
      else if (st == 'e') v = 5;
      else if (st == 'f') v = 6;
      else if (st == 'g') v = 7;
      else if (st == 'h') v = 8;
      else if (st == 'i') v = 9;
      else if (st == 'j') v = 10;
      else if (st == 'k') v = 20;
      else if (st == 'l') v = 30;    
      else if (st == 'm') v = 40;
      else if (st == 'n') v = 50;
      else if (st == 'o') v = 60;
      else if (st == 'p') v = 70;
      else if (st == 'q') v = 80;
      else if (st == 'r') v = 90;
      else if (st == 's') v = 100;
      else if (st == 't') v = 200;
      else if (st == 'u') v = 300;
      else if (st == 'v') v = 400;
      else if (st == 'w') v = 500;
      else if (st == 'x') v = 600;
      else if (st == 'y') v = 700;
      else if (st == 'z') v = 800;
      else if (st == 'A') v = 1;
      else if (st == 'B') v = 2;
      else if (st == 'G') v = 3;
      else if (st == 'D') v = 4;
      else if (st == 'H') v = 5;
      else if (st == 'V') v = 6;
      else if (st == 'Z') v = 7;
      else if (st == 'C') v = 8;
      else if (st == 'T') v = 9;
      else if (st == 'Y') v = 10;
      else if (st == 'K') v = 20;
      else if (st == 'L') v = 30;
      else if (st == 'M') v = 40;
      else if (st == 'N') v = 50;
      else if (st == 'S') v = 60;
      else if (st == 'I') v = 70;
      else if (st == 'F') v = 80;
      else if (st == 'E') v = 90;
      else if (st == 'Q') v = 100;
      else if (st == 'R') v = 200;
      else if (st == 'J') v = 300;
      else if (st == 'O') v = 400;
      total += v;
      if (i > 0) out += " + ";
      out += Integer.toString(v);
   }
   return(total);
}
//__________________________________________________
int place(String str)
{
   int total = 0;
   out = "";
   for (int i = 0; i < str.length(); i++)
   {
      int v = 0;
      String st2 = str.substring(i,i+1);
      byte[] st3 = st2.getBytes();
      byte st = st3[0];
      if (st == 'a') v = 1;
      else if (st == 'b') v = 2;
      else if (st == 'c') v = 3;
      else if (st == 'd') v = 4;
      else if (st == 'e') v = 5;
      else if (st == 'f') v = 6;
      else if (st == 'g') v = 7;
      else if (st == 'h') v = 8;
      else if (st == 'i') v = 9;
      else if (st == 'j') v = 10;
      else if (st == 'k') v = 11;
      else if (st == 'l') v = 12;    
      else if (st == 'm') v = 13;
      else if (st == 'n') v = 14;
      else if (st == 'o') v = 15;
      else if (st == 'p') v = 16;
      else if (st == 'q') v = 17;
      else if (st == 'r') v = 18;
      else if (st == 's') v = 19;
      else if (st == 't') v = 20;
      else if (st == 'u') v = 21;
      else if (st == 'v') v = 22;
      else if (st == 'w') v = 23;
      else if (st == 'x') v = 24;
      else if (st == 'y') v = 25;
      else if (st == 'z') v = 26;
      else if (st == 'A') v = 1;
      else if (st == 'B') v = 2;
      else if (st == 'G') v = 3;
      else if (st == 'D') v = 4;
      else if (st == 'H') v = 5;
      else if (st == 'V') v = 6;
      else if (st == 'Z') v = 7;
      else if (st == 'C') v = 8;
      else if (st == 'T') v = 9;
      else if (st == 'Y') v = 10;
      else if (st == 'K') v = 11;
      else if (st == 'L') v = 12;
      else if (st == 'M') v = 13;
      else if (st == 'N') v = 14;
      else if (st == 'S') v = 15;
      else if (st == 'I') v = 16;
      else if (st == 'F') v = 17;
      else if (st == 'E') v = 18;
      else if (st == 'Q') v = 19;
      else if (st == 'R') v = 20;
      else if (st == 'J') v = 21;
      else if (st == 'O') v = 22;
      total += v;
      if (i > 0) out += " + ";
      out += Integer.toString(v);
   }
   return(total);
}
}//__________________________________________________________________________
