Class: PROIEL::Commands::Visualize

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

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

    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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/proiel/cli/commands/visualize.rb', line 17

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

  if layout != 'classic' and layout != 'linearized' and layout != 'packed'
    STDERR.puts "Invalid layout"
    exit 1
  end

  if objects != 'sentences' and objects != 'divs' and objects != 'sources'
    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
      raise
    end
  end
end

.save_graph(template, format, graph) ⇒ Object



65
66
67
# File 'lib/proiel/cli/commands/visualize.rb', line 65

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



69
70
71
72
73
74
75
76
# File 'lib/proiel/cli/commands/visualize.rb', line 69

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