Class: PROIEL::Commands::Dictionary

Inherits:
PROIEL::Command show all
Defined in:
lib/proiel/cli/commands/dictionary.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
# File 'lib/proiel/cli/commands/dictionary.rb', line 5

def init_with_program(prog)
  prog.command(:dictionary) do |c|
    c.syntax 'dictionary [options] filename(s)'
    c.description 'Build a dictionary'

    c.option 'glosses', '--merge-glosses glosses.tsv', 'Merge glosses from an external file'
    c.option 'gloss-languages', '--merge-gloss-languages eng,rus', 'Merge glosses from selected languages'

    c.action { |args, options| process(args, options) }
  end
end

.process(args, options) ⇒ Object



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

def process(args, options)
  tb = PROIEL::Treebank.new
  dict = PROIEL::DictionaryBuilder.new

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

    tb.load_from_xml(filename)
  end

  if options['glosses']
    languages = (options['gloss-languages'] || 'eng').split(',').map(&:to_sym)
    if File.exist?(options['glosses'])
      dict.add_external_glosses!(options['glosses'], languages)
    else
      STDERR.puts "#{options['glosses']} not found"
      exit 1
    end
  end

  tb.sources.each do |source|
    dict.add_source!(source)
  end

  dict.to_xml(STDOUT)
end