class AjaxCat::MosesPair

Public Class Methods

new(name, moses_path, moses_ini_path) click to toggle source
# File lib/ajax-cat/moses_pair.rb, line 4
    def initialize(name, moses_path, moses_ini_path)
  @logger = Logger.new(name)
  @logger.log "starting pair".green
  @request_queue = []
  @name = name
  @queue_lock = Mutex.new
            @fifo_path = "#{Dir.home}/.ajax-cat/#{name}_fifo.fifo"
  system("rm -f #{@fifo_path}; mkfifo #{@fifo_path}")
  t1 = Thread.new{reader()}
  @pipe = IO.popen("#{moses_path} -f #{moses_ini_path} -n-best-list - 300 distinct -include-alignment-in-n-best true -continue-partial-translation true > #{@fifo_path}", "w")
  process_request(Request::Raw.new("start_test"))
  @logger.log "pair started".green
end

Public Instance Methods

process_request(request) click to toggle source
# File lib/ajax-cat/moses_pair.rb, line 23
def process_request(request)
  @logger.log "processing #{request.class.name} '#{request.sentence.blue}'"
  @queue_lock.synchronize do
    request.lock.lock
    @request_queue.push(request)
    process_string(request.prepare_moses_request)
  end
  #TODO: avoid active waiting somehow
  waiting = 0
  until request.processed 
    sleep 0.001
    waiting += 1
    if waiting > 10000
      @logger.log "FAILED TO PROCESS REQUEST"
      break
    end
  end
  @logger.log "processed #{request.class.name} '#{request.sentence.blue}'"
  request.result
end
process_string(str) click to toggle source
# File lib/ajax-cat/moses_pair.rb, line 18
def process_string(str)
        @pipe.write("#{str}\nxxxxnonsensestringxxx\n")
        @pipe.flush
end
reader() click to toggle source
# File lib/ajax-cat/moses_pair.rb, line 44
  def reader
f = open @fifo_path, File::RDWR|File::NONBLOCK
last_position = -1

while l = f.readline
  puts "MOSES: #{l}"
  position = Request::Raw.parse_position(l)
  if position != last_position
    if (last_position % 2 == 0)
      @current_request.processed = true
      @current_request = nil
    else
      @queue_lock.synchronize do
        @current_request = @request_queue.shift
      end
    end
  end
  @current_request.process_line(l) if @current_request
  last_position = position
end
  end