Class: PhraseKit::Scorer

Inherits:
Object
  • Object
show all
Defined in:
lib/phrasekit/scorer.rb

Defined Under Namespace

Classes: Error

Class Method Summary collapse

Class Method Details

.score(domain_path:, background_path:, output_path:, method: :ratio, min_salience: 2.0, min_domain_count: 10, assign_phrase_ids: true, starting_phrase_id: 1000, config_path: nil) ⇒ Object



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
# File 'lib/phrasekit/scorer.rb', line 8

def score(
  domain_path:,
  background_path:,
  output_path:,
  method: :ratio,
  min_salience: 2.0,
  min_domain_count: 10,
  assign_phrase_ids: true,
  starting_phrase_id: 1000,
  config_path: nil
)
  binary_path = find_binary

  # Create temporary config if not provided
  if config_path.nil?
    require "tempfile"
    require "json"

    config_file = Tempfile.new(["score_config", ".json"])
    config_file.write(JSON.generate({
      method: method.to_s,
      min_salience: min_salience,
      min_domain_count: min_domain_count,
      assign_phrase_ids: assign_phrase_ids,
      starting_phrase_id: starting_phrase_id
    }))
    config_file.flush
    config_path = config_file.path
  end

  # Run scoring
  cmd = [
    binary_path,
    domain_path.to_s,
    background_path.to_s,
    config_path.to_s,
    output_path.to_s
  ]
  output = `#{cmd.shelljoin} 2>&1`

  unless $?.success?
    config_file.close! if config_file
    raise Error, "Scoring failed: #{output}"
  end

  config_file.close! if config_file

  # Parse stats from output
  parse_stats(output)
end