Module: Treat::Entities::Entity::Magical

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

Instance Method Summary collapse

Instance Method Details

#first_but_warn(array, type) ⇒ Object

Return the first element in the array, warning if not the only one in the array. Used for magic methods: e.g., the magic method “word” if called on a sentence with many words, Treat will return the first word, but warn the user.



85
86
87
88
89
90
91
# File 'lib/treat/entities/entity/magical.rb', line 85

def first_but_warn(array, type)
  if array.size > 1
    warn "Warning: requested one #{type}, but" +
    " there are many #{type}s in this entity."
  end
  array[0]
end

#magic(sym, *args) ⇒ Object

Parse “magic methods”, which allow the following syntaxes to be used (where ‘word’ can be replaced by any entity type, e.g. token, zone, etc.):

  • each_word : iterate over each children of type word.

  • words: return an array of children words.

  • word: return the first word in the entity.

  • word_count: return the number of words in the entity.

  • words_with_*(value) (where * is an arbitrary feature): return the words that have the given feature set to value.

Also provides magical methods for types of words (each_noun, nouns, noun_count, nouns_with_*(value) noun_with_*(value), etc.) For this to be used, the words in the text must have been tokenized and categorized in the first place.



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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/treat/entities/entity/magical.rb', line 18

def magic(sym, *args)

  # Cache this for performance.
  @@entities_regexp ||= "(#{Treat.core.entities.list.join('|')})"
  @@cats_regexp ||= "(#{Treat.linguistics.categories.join('|')})"

  method = sym.to_s =~ /entities/ ?
  sym.to_s.gsub('entities', 'entitys') :
  method = sym.to_s

  if method =~ /^#{@@entities_regexp}s$/
    entities_with_type($1.intern)
  elsif method =~ /^#{@@entities_regexp}$/
    first_but_warn(entities_with_type($1.intern), $1)
  elsif method =~ /^first_#{@@entities_regexp}$/
    e = entities_with_type($1.intern)
    e ? e[0] : nil
  elsif method =~ /^parent_#{@@entities_regexp}$/
    ancestor_with_type($1.intern)
  elsif method =~ /^each_#{@@entities_regexp}$/
    each_entity($1.intern) { |e| yield e }
  elsif method =~ /^#{@@entities_regexp}_count$/
    entities_with_type($1.intern).size
  elsif method =~ /^#{@@entities_regexp}s_with_([a-z]+)$/
    entities_with_feature($2.intern, args[0], $1.intern)
  elsif method =~ /^#{@@entities_regexp}_with_([a-z]*)$/
    first_but_warn(entities_with_feature(
    $2.intern, args[0], $1.intern), $1)
  elsif method =~ /^each_#{@@entities_regexp}_with_([a-z]*)$/
    entities_with_feature($2.intern, args[0], 
    $1.intern).each { |e| yield e }
  elsif method =~ /^each_with_([a-z]*)$/
    entities_with_feature($2.intern, 
    args[0], $1.intern).each { |e| yield e }
  elsif method =~ /^each_#{@@cats_regexp}$/
    entities_with_category($1).each { |e| yield e }
  elsif method =~ /^#{@@cats_regexp}s$/
    entities_with_category($1)
  elsif method =~ /^#{@@cats_regexp}$/
   first_but_warn(entities_with_category($1), $1)
  elsif method =~ /^first_#{@@cats_regexp}$/
   e = entities_with_category($1)
   e ? e[0] : nil
  elsif method =~ /^#{@@cats_regexp}_count$/
    entities_with_category($1).size
  elsif method =~ /^(.*)_count$/
    num_children_with_feature($1.intern)
  elsif method =~ /^#{@@cats_regexp}s_with_([a-z]*)$/
    entities_with_feature($2.intern, args[0], $1)
  elsif method =~ /^#{@@cats_regexp}_with_([a-z]*)$/
    first_but_warn(entities_with_feature(
    $2.intern, args[0], $1.intern), $1)
  elsif method =~ /^([a-z]*)_of_(.*)$/
    f = send($2.intern)
    f ? f.send($1.intern) : nil
  elsif method =~ /^frequency_in_#{@@entities_regexp}$/
    frequency_in($1.intern)
  else
    return :no_magic # :-(
  end
  
end