Class: PROIEL::Commands::Visualize

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

Constant Summary collapse

LAYOUTS =
%w(classic linearized packed modern)

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/visualize.rb', line 5

def init_with_program(prog)
  prog.command(:visualize) do |c|
    c.syntax 'visualize [OPTION(S)] FILENAME(S)'
    c.description 'Visualize treebank graphs'
    c.option 'objects', '--objects sentences|divs|sources|SENTENCE-ID', 'Objects to visualize (default: sentences)'
    c.option 'format', '--format png|svg|dot', 'Output format (default: svg)'
    c.option 'layout', '--layout classic|linearized|packed|modern', 'Graph layout (default: classic)'

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

.process(args, options) ⇒ Object



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

def process(args, options)
  objects = options['objects'] || 'sentences'
  format = options['format'] || 'svg'
  layout = options['layout'] || 'classic'

  unless LAYOUTS.include?(layout)
    STDERR.puts 'Invalid layout'
    exit 1
  end

  if objects != 'sentences' and objects != 'divs' and objects != 'sources' and objects.to_i.to_s != objects
    STDERR.puts 'Invalid object type'
    exit 1
  end

  if format != 'png' and format != 'svg' and format != 'dot'
    STDERR.puts 'Invalid format'
    exit 1
  end

  tb = PROIEL::Treebank.new

  if args.empty?
    STDERR.puts 'Reading from standard input...'.green if options['verbose']
    tb.load_from_xml(STDIN)
  else
    args.each do |filename|
      STDERR.puts "Reading #{filename}...".green if options['verbose']

      tb.load_from_xml(filename)
    end
  end

  tb.sources.each do |source|
    case objects
    when 'sources'
      puts 'This can take a very, very long time... Be patient!'
      save_graph layout, format, source
    when 'divs'
      save_graphs source.divs, layout, format, source.id, source.divs.count
    when 'sentences'
      save_graphs source.sentences, layout, format, source.id, source.sentences.count
    else
      object = tb.find_sentence(objects.to_i)
      save_graph(layout, format, object) if object
    end
  end
end

.save_graph(template, format, graph) ⇒ Object



68
69
70
# File 'lib/proiel/cli/commands/visualize.rb', line 68

def save_graph(template, format, graph)
  PROIEL::Visualization::Graphviz.generate_to_file(template, graph, format, "#{graph.id}.#{format}")
end

.save_graphs(enumerator, template, format, title, n) ⇒ Object



72
73
74
75
76
77
78
79
# File 'lib/proiel/cli/commands/visualize.rb', line 72

def save_graphs(enumerator, template, format, title, n)
  pbar = ProgressBar.create progress_mark: 'X', remainder_mark: ' ', title: title, total: n

  enumerator.each_with_index do |object, i|
    save_graph template, format, object
    pbar.increment
  end
end