summaryrefslogtreecommitdiff
path: root/lib/nlp_ruby/misc.rb
blob: 80d932c59410daf925233760512edc5a44895212 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
class Array
  def max_index
    self.index(self.max)
  end

  def is_subset_of? other
    self.each { |i|
      if other.include? i
       return false
      end
    }
    return true
  end

  def sum
    self.inject(:+)
  end

  def mean
    self.sum.to_f/self.size
  end
end

def spawn_with_timeout cmd, t=4, debug=false
  require 'timeout'
  STDERR.write cmd+"\n" if debug
  pipe_in, pipe_out = IO.pipe
  pid = Process.spawn(cmd, :out => pipe_out)
  begin
    Timeout.timeout(t) { Process.wait pid }
  rescue Timeout::Error
    return ""
    # accept the zombies
    #Process.kill('TERM', pid)
  end
  pipe_out.close
  return pipe_in.read
end