Class: SportDb::LangChecker

Inherits:
Object
  • Object
show all
Includes:
LogUtils::Logging
Defined in:
lib/sportdb/lang.rb

Instance Method Summary collapse

Constructor Details

#initializeLangChecker

Returns a new instance of LangChecker.



206
207
# File 'lib/sportdb/lang.rb', line 206

def initialize
end

Instance Method Details

#analyze(path) ⇒ Object



209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
# File 'lib/sportdb/lang.rb', line 209

def analyze( path )
  # return lang code e.g. en, de, es

  text = File.read_utf8( path )
  
  ### todo/fix:
  # remove comment lines and end of line comments from text
  
  en = count_words_in_text( SportDb.lang.words_en, text )
  de = count_words_in_text( SportDb.lang.words_de, text )
  es = count_words_in_text( SportDb.lang.words_es, text )
    
  lang_counts = [
    [ 'en', en ],
    [ 'de', de ],
    [ 'es', es ]
  ]
  
  # sort by word count (reverse sort e.g. highest count goes first)
  lang_counts = lang_counts.sort {|l,r| r[1] <=> l[1] }
  
  # dump stats
  
  logger.debug "lang checker:"
  lang_counts.each_with_index do |item,index|
    ## e.g. 1. en: 20 words
    ##      2. de: 2 words
    logger.debug " #{index+1}. #{item[0]}: #{item[1]}"
  end
  
  logger.info "lang checker - using lang >>#{lang_counts[0][0]}<<"
  
  ## return lang code w/ highest count
  lang_counts[0][0]
end