Sha256: 36ddf7f0206677506d9d00e6daea59a30e3c23730e4a5c6cbfba046e9e20b487

Contents?: true

Size: 1.71 KB

Versions: 28

Compression:

Stored size: 1.71 KB

Contents

require 'rbbt/entity'

module AssociationItem
  extend Entity

  annotation :knowledge_base
  annotation :database
  annotation :reverse

  property :part => :array2single do
    self.clean_annotations.collect{|p| p.partition("~") }
  end

  property :target => :array2single do
    self.part.collect{|p| p[2]}
  end

  property :source => :array2single do
    self.clean_annotations.collect{|p| p[/[^~]+/] }
  end

  property :target_entity => :array2single do
    type = reverse ? knowledge_base.source(database) : knowledge_base.target(database)
    knowledge_base.annotate self.target, type, database if self.target.any?
  end

  property :source_entity => :array2single do
    type = reverse ? knowledge_base.target(database) : knowledge_base.source(database)
    knowledge_base.annotate self.source, type if self.target.any?
  end

  property :value => :array2single do
    value = knowledge_base.get_index(database).chunked_values_at self
    value.collect{|v| NamedArray.setup(v, knowledge_base.get_index(database).fields)}
  end

  property :info => :array2single do
    fields = knowledge_base.index_fields(database)
    return [{}] * self.length if fields.nil? or fields.empty?

    value.collect{|v|
      Hash[*fields.zip(v).flatten]
    }
  end

  def self.incidence(pairs)
    matrix = {}
    targets = []
    sources = []
    matches = {}

    pairs.each do |p|
      s, sep, t = p.partition "~"
      sources << s
      targets << t
      matches[s] ||= Hash.new{false}
      matches[s][t] = true
    end

    sources.uniq!
    targets = targets.uniq.sort

    matches.each do |s,hash|
      matrix[s] = hash.values_at(*targets)
    end

    defined?(TSV)? TSV.setup(matrix, :fields => targets, :type => :list) : matrix
  end
end

Version data entries

28 entries across 28 versions & 1 rubygems

Version Path
rbbt-util-5.5.40 lib/rbbt/association/item.rb
rbbt-util-5.5.39 lib/rbbt/association/item.rb
rbbt-util-5.5.38 lib/rbbt/association/item.rb
rbbt-util-5.5.37 lib/rbbt/association/item.rb
rbbt-util-5.5.36 lib/rbbt/association/item.rb
rbbt-util-5.5.35 lib/rbbt/association/item.rb
rbbt-util-5.5.34 lib/rbbt/association/item.rb
rbbt-util-5.5.33 lib/rbbt/association/item.rb
rbbt-util-5.5.32 lib/rbbt/association/item.rb
rbbt-util-5.5.31 lib/rbbt/association/item.rb
rbbt-util-5.5.30 lib/rbbt/association/item.rb
rbbt-util-5.5.29 lib/rbbt/association/item.rb
rbbt-util-5.5.28 lib/rbbt/association/item.rb
rbbt-util-5.5.27 lib/rbbt/association/item.rb
rbbt-util-5.5.26 lib/rbbt/association/item.rb
rbbt-util-5.5.25 lib/rbbt/association/item.rb
rbbt-util-5.5.24 lib/rbbt/association/item.rb
rbbt-util-5.5.23 lib/rbbt/association/item.rb
rbbt-util-5.5.22 lib/rbbt/association/item.rb
rbbt-util-5.5.21 lib/rbbt/association/item.rb