mattt / broccoli

NLP question asking and answering module

This URL has Read+Write access

broccoli / answer
100755 70 lines (54 sloc) 1.562 kb
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#!/usr/bin/env ruby -w
 
=begin rdoc
Question answering script.
Authors:
Zach Paine (znp)
Mattt Thompson (mattt)
Owen Yamauchi (ody)
Emilie McConville (emcconvi)
=end
 
# So we don't need a long nasty line on every require
$: << File.join(File.dirname(__FILE__), '/lib')
$:.concat(Dir.glob(File.join(File.dirname(__FILE__), '/vendor/*/lib')))
 
require 'easy_answer'
require 'getoptlong'
require 'logger'
 
logdir = File.join(File.dirname(__FILE__), '/log')
Dir.mkdir(logdir) unless File.directory?(logdir)
$logger = Logger.new(File.join(File.dirname(__FILE__), '/log/' +
  Time.now.strftime("ask_%m%d%Y%H%M") + ".log"))
 
usage = "#{$0} [--verbose] article_file questions_file"
 
$VERBOSE = false
opts = GetoptLong.new(['--verbose', '-v', GetoptLong::NO_ARGUMENT])
opts.each {|opt, arg| $VERBOSE = (opt == '--verbose')}
 
if $VERBOSE
  $logger = Logger.new(STDERR)
  $logger = Logger.new(STDOUT)
end
 
if ARGV.length != 2
  puts usage
  exit 0
end
 
$logger.debug "Question Answerer Started"
(article, questions_file) = ARGV
 
if !File.exists?(article) or !File.exists?(questions_file)
  puts usage
  exit 0
end
 
$logger.debug "Loading article text"
article_text = ''
File.open(article) do |file|
  article_text = file.read
end
 
File.open(questions_file) do |file|
  file.each_line do |line|
    $logger.debug "QUESTION: " + line.chomp
    answer = EasyAnswer.answer(line, article_text)
    if answer
      $logger.debug "ANSWER: " + answer unless $VERBOSE
      puts answer
    else
      puts "Couldn't answer this question!"
    end
  end
end