class Stats
    attr_accessor :folder
  
  def analizeFile( f )
    @file_symbols_stats = "symbols_stats.txt"
    @file_word_length = "word_length_stats.txt"
    @file_word_stats = "words_stats.txt"

      
    @symbols        = Hash.new
    @symbols_total  = 0
    
    @words          = Hash.new
    @words_total    = 0
    @words_len      = Hash.new
    @wordsN         = Hash.new
    @wordsN_total   = Hash.new
    
    @combinations = Hash.new
    @combinations_total = Hash.new
    
    for i in 1..10
      @wordsN[i]        = Hash.new
      @wordsN_total[i]  = 0
    end
    
    for i in 2..10
      @combinations[i]          = Hash.new
      @combinations_total[i]    = 0
    end
        
    for i in 1..10 do
      @words_len[i] = 0
    end
    
    @file = File.new(@folder + f, 'r')
    
    while (ln = @file.gets)
      ln.each_char do |c|
        next if c.ord == 10 #skip '\n'

        incValueAtHash @symbols, c
      end
      
      ln.split.each do |word|
        @words_total += 1 
        self.processWord word
        
        foldername = File.basename @folder
        
        if @words_total % 10000 == 0
          puts "#{foldername} + 10 000 words processed. #{@words_total} in total"
        end
        
      end
    end
    
  end
  
  def processWordCombinations( word )
    len = word.length
    
    return if len < 3
    
    for i in 2..10
      w = getCombinationsOf i, word

      w.each do |item|  
        incValueAtHash @combinations[i], item
        @combinations_total[i] += 1
      end
    end

  end
  
  def getCombinationsOf( num, word )
    max = word.length - num
    ret = Array.new
    
    for i in 0..max
      ret << word[i..i+ num - 1]
    end
    
    ret
  end
  
  def processWord( word )
    
    return if word.length > 10
    
    @symbols_total += word.length
    
    incValueAtHash @words_len, word.length
    incValueAtHash @words, word
    
    incValueAtHash @wordsN[word.length], word
    @wordsN_total[word.length] += 1
    
    #processWordCombinations word
  end
  
  def incValueAtHash(hash, hash_key)
    if hash.include? hash_key
			hash[hash_key] += 1
		else
			hash[hash_key] = 1
    end
  end
  
  #
  # Print summary
  #
  
  def showSummary
    puts "TOTAL WORDS = " + @words_total.to_s
    puts "SYMBOLS TOTAL = " + @symbols_total.to_s
    
    puts "WORD LENGTH STATS"
    
    summaryFor @words_len, @words_total
    
    puts "_______________________________________"
    puts "WORDS STATS____________________________"
    sortedSummaryFor @words_all_stats, @words_total, 50
    
    puts "_______________________________________"
    puts "SYMBOLS STATS__________________________"
    sortedSummaryFor @symbols, @symbols_total
    
    for i in 1..10
      puts "_______________________________________"
      puts i.to_s + " SYMBOL WORDS_________________________"
      sortedSummaryFor @words[i], nil, 10
    end
    
    for i in 3..10
      puts "______________"
      puts "COMBINATIONS " + i.to_s
      puts "______________"    
      sortedSummaryFor @combinations[i], @combinations_total[i], 10
      puts "TOTAL COMBINATIONS : " + @combinations[i].length.to_s
    end
  end
  
  def save()
      file = File.new(@folder + @file_symbols_stats, "w")
      saveSortedByValues(@symbols, @symbols_total, file)
      
      file = File.new(@folder + @file_word_length, "w")
      saveSortedByKeys(@words_len, @words_total, file)
      
      file = File.new(@folder + @file_word_stats, "w")
      saveSortedByValues(@words, @words_total, file)
      
      for i in 1..10
          file = File.new(@folder + "words_#{i}.txt", "w")
          saveSortedByValues(@wordsN[i], @wordsN_total[i], file)
      end
      
      #for i in 2..10
      #    file = File.new(@folder + "combinations_#{i}.txt", "w")
      #    saveSortedByValues(@combinations[i], @combinations_total[i], file)
      #end
      
  end
  
  def saveSortedByValues( hash, total, file)
    hash.sort_by {|k, v| v}.reverse.each do |k, v|
        ratio = ( Float(v) / Float(total) * 10000.0 ).to_i
        
        if ratio > 0
            file.puts "#{k} #{ratio}"
        end
    end
  end
  
  def saveSortedByKeys( hash, total, file)
      hash.sort_by {|k, v| k}.each do |k, v|
          ratio = ( Float(v) / Float(total) * 10000.0 ).to_i
          if ratio > 0
              file.puts "#{k} #{ratio}"
          end
      end
  end
  
  def summaryFor( hash, total)
      hash.each do |the_key, the_value|
        puts the_key.to_s + ' – ' + the_value.to_s +  " = " + ( 100.0 * the_value / total).round(2).to_s + "%"
      end  
  end
  
  def sortedSummaryFor( hash, total = nil, first = nil)
    i = 0;
    #custom_counter = 0
    hash.sort_by {|the_key, the_value| the_value}.reverse.each do |the_key, the_value|
      
      if first
        if i > first
          i += 1
          next
        end
      end
      
      if total == nil
        puts i.to_s + " __ " + the_key.to_s + ' – ' + the_value.to_s
      else
        puts i.to_s + " __ " + the_key.to_s + ' – ' + the_value.to_s + " = " + ( 100.0 * the_value / total).round(2).to_s + "%"
      end
      
      i += 1
    end
  end
  
  
  
end
