Class: PROIEL::Commands::Build

Inherits:
PROIEL::Command show all
Defined in:
lib/proiel/cli/commands/build.rb

Class Method Summary collapse

Methods inherited from PROIEL::Command

inherited, subclasses

Class Method Details

.init_with_program(prog) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/proiel/cli/commands/build.rb', line 5

def init_with_program(prog)
  prog.command(:build) do |c|
    c.syntax 'build resource'
    c.description 'Build a derived resource'

    c.command(:dictionary) do |f|
      f.syntax 'output_filename [filename(s)]'
      f.description 'Build a dictionary from treebank data'
      f.action { |args, options| process_dictionary(args, options) }
    end

    c.command(:dictionaries) do |f|
      f.syntax '[filename(s)]'
      f.description 'Build multiple dictionaries (one per language) from treebank data'
      f.action { |args, options| process_dictionaries(args, options) }
    end

    c.action do |_, _|
      STDERR.puts 'Missing or invalid format. Use --help for more information.'
      exit 1
    end
  end
end

.process_dictionaries(args, options) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/proiel/cli/commands/build.rb', line 60

def process_dictionaries(args, options)
  dicts = {}

  if args.empty?
    STDERR.puts 'Reading from standard input...'.green if options['verbose']

    tb = PROIEL::Treebank.new
    tb.load_from_xml(STDIN)
  else
    tb = PROIEL::Treebank.new

    args.each do |filename|
      STDERR.puts "Reading #{filename}...".green if options['verbose']
      tb.load_from_xml(filename)
    end
  end

  tb.sources.each do |source|
    dicts[source.language] ||= PROIEL::Dictionary::Builder.new
    dicts[source.language].add_source!(source)
  end

  dicts.each do |language, dict|
    File.open("#{language}.xml", 'w') do |f|
      dict.to_xml(f)
    end
  end 
end

.process_dictionary(args, options) ⇒ Object



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
# File 'lib/proiel/cli/commands/build.rb', line 29

def process_dictionary(args, options)
  if args.empty?
    STDERR.puts 'Missing output filename. Use --help for more information.'
    exit 1
  end

  output_filename, *input_filenames = args

  tb = PROIEL::Treebank.new
  dict = PROIEL::Dictionary::Builder.new

  if input_filenames.empty?
    STDERR.puts 'Reading from standard input...'.green if options['verbose']

    tb.load_from_xml(STDIN)
    tb.sources.each { |source| dict.add_source!(source) }
  else
    input_filenames.each do |filename|
      STDERR.puts "Reading #{filename}...".green if options['verbose']

      tb.load_from_xml(filename)
    end
  end

  tb.sources.each { |source| dict.add_source!(source) }

  File.open(output_filename, 'w') do |f|
    dict.to_xml(f)
  end
end