Class: AjaxCat::MosesPair

Inherits:
Object
  • Object
show all
Defined in:
lib/ajax-cat/moses_pair.rb

Instance Method Summary collapse

Constructor Details

#initialize(name, moses_path, moses_ini_path) ⇒ MosesPair

Returns a new instance of MosesPair.



4
5
6
7
8
9
10
11
12
13
14
15
16
# 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

Instance Method Details

#process_request(request) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# 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) ⇒ Object



18
19
20
21
# File 'lib/ajax-cat/moses_pair.rb', line 18

def process_string(str)
	@pipe.write("#{str}\nxxxxnonsensestringxxx\n")
	@pipe.flush
end

#readerObject



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# 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