#!/usr/bin/ruby -w

require 'set'

class Converter
    
  def convert( data_dir, out_file_name, symbols, special_symbols, lesson )
    @special_symbols_set = special_symbols
    @symbols_set = symbols
    files = Dir[data_dir + "*"]
    
    @out_file = File.new(out_file_name, 'w')
    if @out_file == nil
          puts "Converter : can't open file for output"
    end
    
    i = 0 
    files.each do |f|
      filename = File.basename f
      
      if filename[0] == "_"         # if file starts with "_" it should be our "system" file
        puts "skipping " + filename
      else
        puts "lesson #{lesson} file [#{i.to_s} from #{files.length.to_s}] - " + filename
        add_file(f)
      end
      
      
      i += 1
    end
    
    @out_file.close
    
  end

  def is_valid_word(word)
      word.each_char { |ch|
          if @symbols_set.include? ch
              #do nothing
          else
              return false
          end
      }
      
      return true
  end

  def add_file(f)
      return if File.directory? f
      
    	File.open(f, 'r') do |file_from|
    		while (ln = file_from.gets)
          begin
              lnd = ln.downcase
              ln_tmp = ""
              ln_final = ""

              lnd.each_char { |ch|
                  if @special_symbols_set.include? ch
                      ln_tmp += " "
                  else
                      ln_tmp += ch
                  end
              }


              ln_tmp.split(" ").each { |word|
                  if is_valid_word word
                      ln_final += word + " "
                  end

              }
              

              @out_file.puts ln_final
              rescue Exception #=> e

          end

    		end
    	end
    
  end

end
