proiel 1.1.1 → 1.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/LICENSE +1 -1
- data/lib/proiel/source.rb +4 -1
- data/lib/proiel/version.rb +1 -1
- data/lib/proiel/visualization/graphviz/classic.dot.erb +23 -0
- data/lib/proiel/visualization/graphviz/linearized.dot.erb +54 -0
- data/lib/proiel/visualization/graphviz/packed.dot.erb +23 -0
- data/lib/proiel/visualization/graphviz.rb +103 -0
- data/lib/proiel/visualization.rb +1 -0
- data/lib/proiel.rb +4 -0
- metadata +15 -11
    
        checksums.yaml
    CHANGED
    
    | @@ -1,7 +1,7 @@ | |
| 1 1 | 
             
            ---
         | 
| 2 2 | 
             
            SHA1:
         | 
| 3 | 
            -
              metadata.gz:  | 
| 4 | 
            -
              data.tar.gz:  | 
| 3 | 
            +
              metadata.gz: 5ead6b41029599129af6a717b0398980876a139c
         | 
| 4 | 
            +
              data.tar.gz: 11a555a98a41029dfb721e9613bbb5be23847cfd
         | 
| 5 5 | 
             
            SHA512:
         | 
| 6 | 
            -
              metadata.gz:  | 
| 7 | 
            -
              data.tar.gz:  | 
| 6 | 
            +
              metadata.gz: 475971ca8443be39f3ef2a634ff8afd50931b248130e439991d473d06fcf0b7695d38d0629856654738ecaa853af1d63ba8675849eef52055b431252d07e205e
         | 
| 7 | 
            +
              data.tar.gz: c33088acdb1e3fb130386204b02e3f54ca6566a05310c3397e8471c88360a742aafb8e5edc515e0d185280625994c606fed987f8cb7518a23c34a5e13686474b
         | 
    
        data/LICENSE
    CHANGED
    
    
    
        data/lib/proiel/source.rb
    CHANGED
    
    | @@ -29,7 +29,10 @@ module PROIEL | |
| 29 29 | 
             
                def initialize(parent, id, export_time, language, metadata, alignment_id, &block)
         | 
| 30 30 | 
             
                  @treebank = parent
         | 
| 31 31 | 
             
                  @id = id.freeze
         | 
| 32 | 
            -
             | 
| 32 | 
            +
             | 
| 33 | 
            +
                  raise ArgumentError, 'string or nil expected' unless export_time.nil? or export_time.is_a?(String)
         | 
| 34 | 
            +
                  @export_time = export_time.nil? ? nil : DateTime.parse(export_time).freeze
         | 
| 35 | 
            +
             | 
| 33 36 | 
             
                  @language = language.freeze
         | 
| 34 37 | 
             
                  @metadata = metadata.freeze
         | 
| 35 38 |  | 
    
        data/lib/proiel/version.rb
    CHANGED
    
    
| @@ -0,0 +1,23 @@ | |
| 1 | 
            +
            digraph G {
         | 
| 2 | 
            +
              charset="UTF-8";
         | 
| 3 | 
            +
              graph [truecolor=true,bgcolor=transparent];
         | 
| 4 | 
            +
              root [label="",shape=circle];
         | 
| 5 | 
            +
             | 
| 6 | 
            +
              <%- @graph.tokens.select { |t| t.empty_token_sort != 'P' }.each do |token| -%>
         | 
| 7 | 
            +
                <%- if token.empty_token_sort == 'C' or token.part_of_speech == 'C-' -%>
         | 
| 8 | 
            +
                  <%= node token.id, token.form || token.empty_token_sort, shape: :diamond -%>
         | 
| 9 | 
            +
                <%- elsif token.is_empty? -%>
         | 
| 10 | 
            +
                  <%= node token.id, token.empty_token_sort,               shape: :circle -%>
         | 
| 11 | 
            +
                <%- else -%>
         | 
| 12 | 
            +
                  <%= node token.id, token.form,                           shape: :box -%>
         | 
| 13 | 
            +
                <%- end -%>
         | 
| 14 | 
            +
             | 
| 15 | 
            +
                <%- if token.relation -%>
         | 
| 16 | 
            +
                  <%= edge (token.head ? token.head.id : :root), token.id, token.relation.to_s.upcase, weight: 1.0, color: :orange -%>
         | 
| 17 | 
            +
                <%- end -%>
         | 
| 18 | 
            +
             | 
| 19 | 
            +
                <%- token.slashes.each do |(relation, target)| -%>
         | 
| 20 | 
            +
                  <%= edge token.id, target, relation.to_s.upcase, weight: 0.0, color: :blue, style: :dashed %>
         | 
| 21 | 
            +
                <%- end -%>
         | 
| 22 | 
            +
              <%- end -%>
         | 
| 23 | 
            +
            }
         | 
| @@ -0,0 +1,54 @@ | |
| 1 | 
            +
            digraph G {
         | 
| 2 | 
            +
              charset="UTF-8"; rankdir=TD; ranksep=.0005; nodesep=.05;
         | 
| 3 | 
            +
              graph [truecolor=true,bgcolor=transparent];
         | 
| 4 | 
            +
             | 
| 5 | 
            +
              node [shape=none]; {
         | 
| 6 | 
            +
                depthR ->
         | 
| 7 | 
            +
                <% (0..@sentence.max_depth).each do |depth| %>
         | 
| 8 | 
            +
                  depth<%= depth %> ->
         | 
| 9 | 
            +
                <% end %>
         | 
| 10 | 
            +
                WORDS [style=invis];
         | 
| 11 | 
            +
              }
         | 
| 12 | 
            +
             | 
| 13 | 
            +
              node [shape=point]; {
         | 
| 14 | 
            +
                rank = same;
         | 
| 15 | 
            +
                depthR [label=""];
         | 
| 16 | 
            +
                root;
         | 
| 17 | 
            +
              }
         | 
| 18 | 
            +
             | 
| 19 | 
            +
              <% @graph.tokens.select { |t| t.empty_token_sort != 'P' }.inject({}) { |k, e| (k[e.depth] ||= []) << e; k }.sort.each do |depth, tokens| %>
         | 
| 20 | 
            +
                node [shape=point]; {
         | 
| 21 | 
            +
                  rank = same;
         | 
| 22 | 
            +
                  <%= node "depth#{depth}" %>
         | 
| 23 | 
            +
                  <% tokens.each do |t| %>
         | 
| 24 | 
            +
                    <%= node t.id %>
         | 
| 25 | 
            +
                  <% end %>
         | 
| 26 | 
            +
                }
         | 
| 27 | 
            +
              <% end %>
         | 
| 28 | 
            +
             | 
| 29 | 
            +
              node [shape=none]; {
         | 
| 30 | 
            +
                rank = same;
         | 
| 31 | 
            +
                WORDS [label=""];
         | 
| 32 | 
            +
                <% @graph.tokens.select { |t| t.empty_token_sort != 'P' }.reject { |t| t.is_empty? }.sort_by { |t| t.token_number }.each do |t| %>
         | 
| 33 | 
            +
                  <%= node "f#{t.id}", t.form %>
         | 
| 34 | 
            +
                <% end %>
         | 
| 35 | 
            +
              }
         | 
| 36 | 
            +
             | 
| 37 | 
            +
              <% @graph.tokens.select { |t| t.empty_token_sort != 'P' }.each do |t| %>
         | 
| 38 | 
            +
                <% if t.relation %>
         | 
| 39 | 
            +
                  <%= edge (t.head ? t.head.id : :root), t.id, t.relation.to_s.upcase, fontcolor: :black, fontsize: 10 %>
         | 
| 40 | 
            +
                <% end %>
         | 
| 41 | 
            +
             | 
| 42 | 
            +
                <% unless t.is_empty? %>
         | 
| 43 | 
            +
                  <%= edge "f#{t.id}", t.id, '', arrowhead: :none, color: :lightgrey %>
         | 
| 44 | 
            +
                <% end %>
         | 
| 45 | 
            +
             | 
| 46 | 
            +
                <% t.slashes.each do |slash_edge| %>
         | 
| 47 | 
            +
                  <%= edge t.id, slash_edge.target_id, slash_edge.relation.to_s.upcase, color: :blue, weight: 0.0, style: :dotted, fontsize: 10 %>
         | 
| 48 | 
            +
                <% end %>
         | 
| 49 | 
            +
              <% end %>
         | 
| 50 | 
            +
             | 
| 51 | 
            +
              <% @graph.tokens.select { |t| t.empty_token_sort.blank? }.sort_by(&:token_number).each_cons(2) do |n1, n2| %>
         | 
| 52 | 
            +
                <%= edge "f#{n1.id}", "f#{n2.id}", '', weight: 10.0, style: :invis %>
         | 
| 53 | 
            +
              <% end %>
         | 
| 54 | 
            +
            }
         | 
| @@ -0,0 +1,23 @@ | |
| 1 | 
            +
            digraph G {
         | 
| 2 | 
            +
              charset="UTF-8";
         | 
| 3 | 
            +
              graph [truecolor=true,bgcolor=transparent];
         | 
| 4 | 
            +
              root [label="",shape=none];
         | 
| 5 | 
            +
             | 
| 6 | 
            +
              <% @graph.tokens.each do |token| %>
         | 
| 7 | 
            +
                <% if token.conjunction? %>
         | 
| 8 | 
            +
                  <%= node token.id, token.form || token.empty_token_sort, shape: :none %>
         | 
| 9 | 
            +
                <% elsif token.is_empty? %>
         | 
| 10 | 
            +
                  <%= node token.id, token.empty_token_sort, shape: :none %>
         | 
| 11 | 
            +
                <% else %>
         | 
| 12 | 
            +
                  <%= node token.id, token.form, shape: :none %>
         | 
| 13 | 
            +
                <% end %>
         | 
| 14 | 
            +
             | 
| 15 | 
            +
                <% if token.relation %>
         | 
| 16 | 
            +
                  <%= edge (token.head ? token.head.id : :root), token.id, token.relation.to_s.upcase, weight: 0.0, color: :purple %>
         | 
| 17 | 
            +
                <% end %>
         | 
| 18 | 
            +
             | 
| 19 | 
            +
                <% token.slashes.each do |slash_edge| %>
         | 
| 20 | 
            +
                  <%= edge token.id, slash_edge.target_id, slash_edge.relation.to_s.upcase, weight: 0.0, color: :purple, style: :dashed %>
         | 
| 21 | 
            +
                <% end %>
         | 
| 22 | 
            +
              <% end %>
         | 
| 23 | 
            +
            }
         | 
| @@ -0,0 +1,103 @@ | |
| 1 | 
            +
            module PROIEL
         | 
| 2 | 
            +
              module Visualization
         | 
| 3 | 
            +
                module Graphviz
         | 
| 4 | 
            +
                  DEFAULT_GRAPHVIZ_BINARY = 'dot'.freeze
         | 
| 5 | 
            +
                  DEFAULT_TEMPLATES = %i(classic linearized packed)
         | 
| 6 | 
            +
                  SUPPORTED_OUTPUT_FORMATS = %i(png svg)
         | 
| 7 | 
            +
             | 
| 8 | 
            +
                  class GraphvizError < Exception
         | 
| 9 | 
            +
                  end
         | 
| 10 | 
            +
             | 
| 11 | 
            +
                  def self.generate_to_file(template, graph, output_format, output_filename, options = {})
         | 
| 12 | 
            +
                    raise ArgumentError, 'string expected' unless output_filename.is_a?(String)
         | 
| 13 | 
            +
             | 
| 14 | 
            +
                    result = PROIEL::Visualization::Graphviz.generate(template, graph, output_format, options)
         | 
| 15 | 
            +
             | 
| 16 | 
            +
                    File.open(output_filename, 'w') do |f|
         | 
| 17 | 
            +
                      f.write(result)
         | 
| 18 | 
            +
                    end
         | 
| 19 | 
            +
                  end
         | 
| 20 | 
            +
             | 
| 21 | 
            +
                  def self.generate(template, graph, output_format, options = {})
         | 
| 22 | 
            +
                    raise ArgumentError, 'string or symbol expected' unless template.is_a?(String) or template.is_a?(Symbol)
         | 
| 23 | 
            +
             | 
| 24 | 
            +
                    dot_code = generate_dot(template, graph)
         | 
| 25 | 
            +
             | 
| 26 | 
            +
                    if output_format.to_sym == :dot
         | 
| 27 | 
            +
                      dot_code
         | 
| 28 | 
            +
                    else
         | 
| 29 | 
            +
                      generate_image(dot_code, output_format, options)
         | 
| 30 | 
            +
                    end
         | 
| 31 | 
            +
                  end
         | 
| 32 | 
            +
             | 
| 33 | 
            +
                  def self.template_filename(template)
         | 
| 34 | 
            +
                    raise ArgumentError, 'string or symbol expected' unless template.is_a?(String) or template.is_a?(Symbol)
         | 
| 35 | 
            +
                    raise ArgumentError, 'invalid template' unless DEFAULT_TEMPLATES.include?(template.to_sym)
         | 
| 36 | 
            +
             | 
| 37 | 
            +
                    File.join(File.dirname(__FILE__), 'graphviz', "#{template}.dot.erb")
         | 
| 38 | 
            +
                  end
         | 
| 39 | 
            +
             | 
| 40 | 
            +
                  def self.generate_image(dot_code, output_format, options = {})
         | 
| 41 | 
            +
                    raise ArgumentError, 'string expected' unless dot_code.is_a?(String)
         | 
| 42 | 
            +
                    raise ArgumentError, 'string or symbol expected' unless output_format.is_a?(String) or output_format.is_a?(Symbol)
         | 
| 43 | 
            +
                    raise ArgumentError, 'invalid output format' unless SUPPORTED_OUTPUT_FORMATS.include?(output_format.to_sym)
         | 
| 44 | 
            +
             | 
| 45 | 
            +
                    graphviz_binary = options[:graphviz_binary] || DEFAULT_GRAPHVIZ_BINARY
         | 
| 46 | 
            +
             | 
| 47 | 
            +
                    result, errors = nil, nil
         | 
| 48 | 
            +
             | 
| 49 | 
            +
                    Open3.popen3("dot -T#{output_format}") do |dot, img, err|
         | 
| 50 | 
            +
                      dot.write dot_code
         | 
| 51 | 
            +
                      dot.close
         | 
| 52 | 
            +
             | 
| 53 | 
            +
                      result, errors = img.read, err.read
         | 
| 54 | 
            +
                    end
         | 
| 55 | 
            +
             | 
| 56 | 
            +
                    raise GraphvizError, "graphviz exited with errors: #{errors}" unless errors.nil? or errors == ''
         | 
| 57 | 
            +
             | 
| 58 | 
            +
                    result
         | 
| 59 | 
            +
                  end
         | 
| 60 | 
            +
             | 
| 61 | 
            +
                  def self.generate_dot(template, graph)
         | 
| 62 | 
            +
                    filename = template_filename(template)
         | 
| 63 | 
            +
             | 
| 64 | 
            +
                    content = File.read(filename)
         | 
| 65 | 
            +
             | 
| 66 | 
            +
                    template = ERB.new(content, nil, '-')
         | 
| 67 | 
            +
                    template.filename = filename
         | 
| 68 | 
            +
             | 
| 69 | 
            +
                    TemplateContext.new(graph).generate(template)
         | 
| 70 | 
            +
                  end
         | 
| 71 | 
            +
             | 
| 72 | 
            +
                  class TemplateContext
         | 
| 73 | 
            +
                    def initialize(graph)
         | 
| 74 | 
            +
                      @graph = graph
         | 
| 75 | 
            +
                    end
         | 
| 76 | 
            +
             | 
| 77 | 
            +
                    def generate(template)
         | 
| 78 | 
            +
                      template.result(binding)
         | 
| 79 | 
            +
                    end
         | 
| 80 | 
            +
             | 
| 81 | 
            +
                    protected
         | 
| 82 | 
            +
             | 
| 83 | 
            +
                    # Creates a node with an identifier and a label.
         | 
| 84 | 
            +
                    def node(identifier, label = '', options = {})
         | 
| 85 | 
            +
                      attrs = join_attributes(options.merge(label: label))
         | 
| 86 | 
            +
             | 
| 87 | 
            +
                      "#{identifier} [#{attrs}];"
         | 
| 88 | 
            +
                    end
         | 
| 89 | 
            +
             | 
| 90 | 
            +
                    # Creates an edge with a label from one identifier to another identifier.
         | 
| 91 | 
            +
                    def edge(identifier1, identifier2, label = '', options = {})
         | 
| 92 | 
            +
                      attrs = join_attributes(options.merge(label: label))
         | 
| 93 | 
            +
             | 
| 94 | 
            +
                      "#{identifier1} -> #{identifier2} [#{attrs}];"
         | 
| 95 | 
            +
                    end
         | 
| 96 | 
            +
             | 
| 97 | 
            +
                    def join_attributes(attrs)
         | 
| 98 | 
            +
                      attrs.map { |a, v| %|#{a}="#{v.to_s.gsub('"', '\\"')}"| }.join(',')
         | 
| 99 | 
            +
                    end
         | 
| 100 | 
            +
                  end
         | 
| 101 | 
            +
                end
         | 
| 102 | 
            +
              end
         | 
| 103 | 
            +
            end
         | 
| @@ -0,0 +1 @@ | |
| 1 | 
            +
            require 'proiel/visualization/graphviz'
         | 
    
        data/lib/proiel.rb
    CHANGED
    
    | @@ -10,6 +10,9 @@ require 'ostruct' | |
| 10 10 | 
             
            require 'sax-machine'
         | 
| 11 11 | 
             
            require 'memoist'
         | 
| 12 12 | 
             
            require 'nokogiri'
         | 
| 13 | 
            +
            require 'singleton'
         | 
| 14 | 
            +
            require 'erb'
         | 
| 15 | 
            +
            require 'open3'
         | 
| 13 16 |  | 
| 14 17 | 
             
            require 'proiel/version'
         | 
| 15 18 | 
             
            require 'proiel/utils'
         | 
| @@ -27,3 +30,4 @@ require 'proiel/source' | |
| 27 30 | 
             
            require 'proiel/div'
         | 
| 28 31 | 
             
            require 'proiel/sentence'
         | 
| 29 32 | 
             
            require 'proiel/token'
         | 
| 33 | 
            +
            require 'proiel/visualization'
         | 
    
        metadata
    CHANGED
    
    | @@ -1,14 +1,14 @@ | |
| 1 1 | 
             
            --- !ruby/object:Gem::Specification
         | 
| 2 2 | 
             
            name: proiel
         | 
| 3 3 | 
             
            version: !ruby/object:Gem::Version
         | 
| 4 | 
            -
              version: 1. | 
| 4 | 
            +
              version: 1.2.0
         | 
| 5 5 | 
             
            platform: ruby
         | 
| 6 6 | 
             
            authors:
         | 
| 7 7 | 
             
            - Marius L. Jøhndal
         | 
| 8 8 | 
             
            autorequire: 
         | 
| 9 9 | 
             
            bindir: bin
         | 
| 10 10 | 
             
            cert_chain: []
         | 
| 11 | 
            -
            date: 2016- | 
| 11 | 
            +
            date: 2016-08-31 00:00:00.000000000 Z
         | 
| 12 12 | 
             
            dependencies:
         | 
| 13 13 | 
             
            - !ruby/object:Gem::Dependency
         | 
| 14 14 | 
             
              name: json
         | 
| @@ -72,28 +72,28 @@ dependencies: | |
| 72 72 | 
             
                requirements:
         | 
| 73 73 | 
             
                - - "~>"
         | 
| 74 74 | 
             
                  - !ruby/object:Gem::Version
         | 
| 75 | 
            -
                    version: '1. | 
| 75 | 
            +
                    version: '1.12'
         | 
| 76 76 | 
             
              type: :development
         | 
| 77 77 | 
             
              prerelease: false
         | 
| 78 78 | 
             
              version_requirements: !ruby/object:Gem::Requirement
         | 
| 79 79 | 
             
                requirements:
         | 
| 80 80 | 
             
                - - "~>"
         | 
| 81 81 | 
             
                  - !ruby/object:Gem::Version
         | 
| 82 | 
            -
                    version: '1. | 
| 82 | 
            +
                    version: '1.12'
         | 
| 83 83 | 
             
            - !ruby/object:Gem::Dependency
         | 
| 84 84 | 
             
              name: rake
         | 
| 85 85 | 
             
              requirement: !ruby/object:Gem::Requirement
         | 
| 86 86 | 
             
                requirements:
         | 
| 87 87 | 
             
                - - "~>"
         | 
| 88 88 | 
             
                  - !ruby/object:Gem::Version
         | 
| 89 | 
            -
                    version: ' | 
| 89 | 
            +
                    version: '11.2'
         | 
| 90 90 | 
             
              type: :development
         | 
| 91 91 | 
             
              prerelease: false
         | 
| 92 92 | 
             
              version_requirements: !ruby/object:Gem::Requirement
         | 
| 93 93 | 
             
                requirements:
         | 
| 94 94 | 
             
                - - "~>"
         | 
| 95 95 | 
             
                  - !ruby/object:Gem::Version
         | 
| 96 | 
            -
                    version: ' | 
| 96 | 
            +
                    version: '11.2'
         | 
| 97 97 | 
             
            - !ruby/object:Gem::Dependency
         | 
| 98 98 | 
             
              name: rspec
         | 
| 99 99 | 
             
              requirement: !ruby/object:Gem::Requirement
         | 
| @@ -128,28 +128,28 @@ dependencies: | |
| 128 128 | 
             
                requirements:
         | 
| 129 129 | 
             
                - - "~>"
         | 
| 130 130 | 
             
                  - !ruby/object:Gem::Version
         | 
| 131 | 
            -
                    version: '0. | 
| 131 | 
            +
                    version: '0.12'
         | 
| 132 132 | 
             
              type: :development
         | 
| 133 133 | 
             
              prerelease: false
         | 
| 134 134 | 
             
              version_requirements: !ruby/object:Gem::Requirement
         | 
| 135 135 | 
             
                requirements:
         | 
| 136 136 | 
             
                - - "~>"
         | 
| 137 137 | 
             
                  - !ruby/object:Gem::Version
         | 
| 138 | 
            -
                    version: '0. | 
| 138 | 
            +
                    version: '0.12'
         | 
| 139 139 | 
             
            - !ruby/object:Gem::Dependency
         | 
| 140 140 | 
             
              name: yard
         | 
| 141 141 | 
             
              requirement: !ruby/object:Gem::Requirement
         | 
| 142 142 | 
             
                requirements:
         | 
| 143 143 | 
             
                - - "~>"
         | 
| 144 144 | 
             
                  - !ruby/object:Gem::Version
         | 
| 145 | 
            -
                    version: 0. | 
| 145 | 
            +
                    version: '0.9'
         | 
| 146 146 | 
             
              type: :development
         | 
| 147 147 | 
             
              prerelease: false
         | 
| 148 148 | 
             
              version_requirements: !ruby/object:Gem::Requirement
         | 
| 149 149 | 
             
                requirements:
         | 
| 150 150 | 
             
                - - "~>"
         | 
| 151 151 | 
             
                  - !ruby/object:Gem::Version
         | 
| 152 | 
            -
                    version: 0. | 
| 152 | 
            +
                    version: '0.9'
         | 
| 153 153 | 
             
            description: This provides a library of functions for reading and manipulating treebanks
         | 
| 154 154 | 
             
              using the PROIEL dependency format.
         | 
| 155 155 | 
             
            email:
         | 
| @@ -184,6 +184,11 @@ files: | |
| 184 184 | 
             
            - lib/proiel/treebank_object.rb
         | 
| 185 185 | 
             
            - lib/proiel/utils.rb
         | 
| 186 186 | 
             
            - lib/proiel/version.rb
         | 
| 187 | 
            +
            - lib/proiel/visualization.rb
         | 
| 188 | 
            +
            - lib/proiel/visualization/graphviz.rb
         | 
| 189 | 
            +
            - lib/proiel/visualization/graphviz/classic.dot.erb
         | 
| 190 | 
            +
            - lib/proiel/visualization/graphviz/linearized.dot.erb
         | 
| 191 | 
            +
            - lib/proiel/visualization/graphviz/packed.dot.erb
         | 
| 187 192 | 
             
            homepage: http://proiel.github.com
         | 
| 188 193 | 
             
            licenses:
         | 
| 189 194 | 
             
            - MIT
         | 
| @@ -209,4 +214,3 @@ signing_key: | |
| 209 214 | 
             
            specification_version: 4
         | 
| 210 215 | 
             
            summary: A library for working with treebanks using the PROIEL dependency format
         | 
| 211 216 | 
             
            test_files: []
         | 
| 212 | 
            -
            has_rdoc: 
         |