Class: Treat::Entities::Entity

Inherits:
Birch::Tree
  • Object
show all
Extended by:
Buildable, Comparable, Debuggable, Delegatable
Includes:
Applicable, Checkable, Countable, Exportable, Iterable, Magical, Registrable, Stringable
Defined in:
lib/treat/entities/entity.rb

Overview

The Entity class extends a basic tree structure (written in C for optimal speed) and represents any form of textual entityin a processing task (this could be a collection of documents, a single document, a single paragraph, etc.)

Classes that extend Entity provide the concrete behavior corresponding to the relevant entity type. See entities.rb for a full list and description of the different entity types in the document model.

Direct Known Subclasses

Collection, Document, Group, Section, Token, Zone

Defined Under Namespace

Modules: Applicable, Buildable, Checkable, Comparable, Countable, Debuggable, Delegatable, Exportable, Iterable, Magical, Registrable, Stringable

Constant Summary

Constants included from Buildable

Buildable::AcceptedFormats, Buildable::EmailRegexp, Buildable::Enclitics, Buildable::NumberRegexp, Buildable::PunctRegexp, Buildable::Reserved, Buildable::UriRegexp, Buildable::WordRegexp

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Delegatable

add_presets, add_workers, call_worker, find_worker, find_worker_for_language, worker_not_found

Methods included from Debuggable

print_debug

Methods included from Buildable

anything_from_string, build, check_encoding, create_collection, from_array, from_db, from_file, from_folder, from_numeric, from_raw_file, from_serialized_file, from_string, from_url, group_from_string, is_serialized_file?, token_from_string, zone_from_string

Methods included from Comparable

compare_with

Methods included from Exportable

#export

Methods included from Iterable

#ancestor_with_feature, #ancestor_with_type, #ancestors_with_type, #each_ancestor, #each_entity, #entities_with_category, #entities_with_feature, #entities_with_types, #num_children_with_feature

Methods included from Checkable

#check_has, #check_hasnt_children

Methods included from Stringable

#implode, #inspect, #print_tree, #short_value, #to_a, #to_s, #to_string

Methods included from Magical

#first_but_warn, #magic

Methods included from Countable

#count, #frequency_in, #frequency_of, #position, #position_from_end

Methods included from Applicable

#apply, #do_task, #get_group

Methods included from Registrable

#register, #registry

Constructor Details

#initialize(value = '', id = nil) ⇒ Entity

Initialize the entity with its value and (optionally) a unique identifier. By default, the object_id will be used as id.



72
73
74
75
76
# File 'lib/treat/entities/entity.rb', line 72

def initialize(value = '', id = nil)
  id ||= object_id; super(value, id)
  @type = :entity if self == Entity
  @type ||= self.class.mn.ucc.intern
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(sym, *args, &block) ⇒ Object

Catch missing methods to support method-like access to features (e.g. entity.category instead of entity.features) and to support magic methods (see #magic).

If the feature or magic method does not exist, or can’t be parsed, raises an exception.

Also catches the “empty” method call (e.g. Word(‘hello’) or Word ‘hello’) as syntactic sugar for the #self.build method.



115
116
117
118
119
120
121
122
# File 'lib/treat/entities/entity.rb', line 115

def method_missing(sym, *args, &block)
  return self.build(*args) if sym == nil
  return @features[sym] if @features.has_key?(sym)
  result = magic(sym, *args, &block)
  return result unless result == :no_magic
  begin; super(sym, *args, &block)
  rescue NoMethodError; invalid_call(sym); end
end

Instance Attribute Details

#typeObject

A symbol representing the lowercase version of the class name. This is the only attribute that the Entity class adds to the Birch::Tree class.



22
23
24
# File 'lib/treat/entities/entity.rb', line 22

def type
  @type
end

Instance Method Details

#<<(entities, clear_parent = true) ⇒ Object

Add an entity to the current entity. Registers the entity in the root node token registry if the entity is a leaf. Unsets the parent node’s value; in order to keep the tree clean, only the leaf values are stored.

Takes in a single entity or an array of entities. Returns the first child supplied. If a string is



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/treat/entities/entity.rb', line 88

def <<(entities, clear_parent = true)
  entities = (entities.is_a?(::String) ||
  entities.is_a?(::Numeric)) ? 
  entities.to_entity : entities
  entities = entities.is_a?(::Array) ?
  entities : [entities]
  # Register each entity in this node.
  entities.each { |e| register(e) }
  # Pass to the <<() method in Birch.
  super(entities)
  # Unset the parent value if necessary.
  @parent.value = '' if has_parent?
  # Return the first child.
  return entities[0]
end

#invalid_call(sym) ⇒ Object

Raises a Treat::Exception saying that the method called was invalid, and that the requested method does not exist. Also provides suggestions for misspellings.

Raises:



128
129
130
131
132
133
134
135
# File 'lib/treat/entities/entity.rb', line 128

def invalid_call(sym)
  msg = Treat::Workers.lookup(sym) ?
  "Method #{sym} can't be called on a #{type}." :
  "Method #{sym} is not defined by Treat." +
  Treat::Helpers::Help.did_you_mean?(
  Treat::Workers.methods, sym)
  raise Treat::Exception, msg
end