Module: Treat::Entities::Entity::Registrable

Included in:
Treat::Entities::Entity
Defined in:
lib/treat/entities/entity/registrable.rb

Overview

Registers the entities ocurring in the subtree of a node as children are added. Also registers text occurrences for word groups and tokens (n grams).

Instance Method Summary collapse

Instance Method Details

#register(entity) ⇒ Object

Registers a token or phrase in the registry. The registry keeps track of children by id, by entity type, and also keeps the position of the entity in its parent entity.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/treat/entities/entity/registrable.rb', line 10

def register(entity)
  unless @registry 
    @count, @registry = 0, 
    {id: {}, value: {}, position:{}, type: {}} 
  end
  if entity.is_a?(Treat::Entities::Token) ||
    entity.is_a?(Treat::Entities::Group)
    val = entity.to_s.downcase
    @registry[:value][val] ||= 0
    @registry[:value][val] += 1
  end
  @registry[:id][entity.id] = true
  @registry[:type][entity.type] ||= 0
  @registry[:type][entity.type] += 1
  @registry[:position][entity.id] = @count
  @count += 1
  @parent.register(entity) if has_parent?
end

#registry(type = nil) ⇒ Object

Backtrack up the tree to find a token registry, by default the one in the root node of the tree.



31
32
33
34
# File 'lib/treat/entities/entity/registrable.rb', line 31

def registry(type = nil)
  (has_parent? && type != self.type) ?
  @parent.registry(type) : @registry
end