class FreelingClient::LanguageDetector

Public Class Methods

new(opt = {}) click to toggle source
# File lib/freeling_client/language_detector.rb, line 9
def initialize(opt = {})
  @config = opt.fetch(:config, 'config/freeling/analyzer.cfg')
  @ident = opt.fetch(:ident, '/usr/local/share/freeling/common/lang_ident/ident.dat')
  @timeout = opt.fetch(:timeout, 120)
end

Public Instance Methods

detect(text) click to toggle source

Detects language

Example:

>> detector = FreelingClient::LanguageDetector.new
>> detector.detect("Este texto está en español.")
=> "es"

Arguments:

text: (String)
# File lib/freeling_client/language_detector.rb, line 26
def detect(text)
  output = []
  file = Tempfile.new('foo', encoding: 'utf-8')

  begin
    file.write(text)
    file.close
    stdin, stdout, stderr = Open3.popen3(command(file.path))

    Timeout::timeout(@timeout) {
      until (line = stdout.gets).nil?
        output << line.chomp
      end

      message = stderr.readlines
      unless message.empty?
        raise ExtractionError, message.join("\n")
      end
    }
  rescue Timeout::Error
    raise ExtractionError, "Timeout"
  ensure
    file.close
    file.unlink
  end
  output[0].to_sym
end

Private Instance Methods

command(file_path) click to toggle source
# File lib/freeling_client/language_detector.rb, line 56
def command(file_path)
  "/usr/local/bin/analyzer --outf ident --fidn #{ident} -f #{config} < #{file_path}"
end