class Ve::Provider::FreelingEn

Constants

BIT_STOP

FIX: This class isn't tested

Public Class Methods

new(config = {}) click to toggle source

TODO: Automatically set FREELINGSHARE if it's not set?

# File lib/providers/freeling_en.rb, line 15
def initialize(config = {})
  @config = {:app => 'analyzer',
             :path => '',
             :flags => ''}.merge(config)

  @config[:app] = `which #{@config[:app]}`.strip!
  local = @config[:app] =~ /local/ ? '/local' : ''
  share_dir = "/usr#{local}/share"
  @config[:freeling_dir_name] = Dir.exist?("#{share_dir}/FreeLing") ? 'FreeLing' : 'freeling'
  @config[:flags] = "-f #{share_dir}/#{@config[:freeling_dir_name]}/config/en.cfg --flush --nonumb --nodate"

  start!
end

Public Instance Methods

parse(text, options = {}) click to toggle source

Talks to the app and returns a parse object

# File lib/providers/freeling_en.rb, line 37
def parse(text, options = {})
  start! if @stdin.nil?
  # Fix Unicode chars
  # TODO: These need to be converted back to the original char in the :literal attribute
  text = text.gsub('’', "'")

  @stdin.puts "#{text}\n#{BIT_STOP}\n"
  output = []

  while line = @stdout.readline
    if line =~ /#{BIT_STOP}/x
      @stdout.readline
      break
    end
    output << line
  end

  Ve::Parse::FreelingEn.new(text, output)
rescue
  Ve::Parse::FreelingEn.new(text, [])
end
works?() click to toggle source

Interface methods

# File lib/providers/freeling_en.rb, line 31
def works?
  p = parse('Wrote')
  "Wrote write VBD 1" == p.tokens.collect { |t| t[:raw] }[0]
end

Private Instance Methods

start!() click to toggle source
# File lib/providers/freeling_en.rb, line 61
def start!
  @stdin, @stdout, @stderr = Open3.popen3("#{@config[:app]} #{@config[:flags]}")

  # TODO: Also filter out non-iso-latin-1 characters
  @stdin.set_encoding('UTF-8', 'ISO-8859-1')
  @stdout.set_encoding('ISO-8859-1', 'UTF-8')
rescue Errno::ENOENT
  # The parser couldn't be started. Probably not installed on this system
end