Class: Raingrams::Ngram

Inherits:
Array
  • Object
show all
Defined in:
lib/raingrams/ngram.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(objects) ⇒ Ngram

Creates a new Ngram object with the specified objects.



9
10
11
# File 'lib/raingrams/ngram.rb', line 9

def initialize(objects)
  super(objects.map { |obj| obj.to_gram })
end

Class Method Details

.[](*objects) ⇒ Object

Creates a new Ngram object from the specified objects.



16
17
18
# File 'lib/raingrams/ngram.rb', line 16

def self.[](*objects)
  self.new(objects)
end

Instance Method Details

#+(grams) ⇒ Object

Creates a new Ngram object by appending the specified grams to the ngram.



24
25
26
27
28
29
30
31
32
# File 'lib/raingrams/ngram.rb', line 24

def +(grams)
  if grams.kind_of?(Array)
    return self.class.new(super(grams.map { |gram|
      gram.to_gram
    }))
  else
    return self.class.new(super([grams.to_gram]))
  end
end

#<<(gram) ⇒ Object



34
35
36
# File 'lib/raingrams/ngram.rb', line 34

def <<(gram)
  super(gram.to_gram)
end

#ends_with?(obj) ⇒ Boolean

Returns:

  • (Boolean)


65
66
67
# File 'lib/raingrams/ngram.rb', line 65

def ends_with?(obj)
  self.last == obj.to_gram
end

#flattenObject



81
82
83
# File 'lib/raingrams/ngram.rb', line 81

def flatten
  self.dup
end

#flatten!Object



85
86
87
# File 'lib/raingrams/ngram.rb', line 85

def flatten!
  self
end

#include?(obj) ⇒ Boolean

Returns:

  • (Boolean)


69
70
71
# File 'lib/raingrams/ngram.rb', line 69

def include?(obj)
  super(obj.to_gram)
end

#includes_all?(*grams) ⇒ Boolean

Returns:

  • (Boolean)


77
78
79
# File 'lib/raingrams/ngram.rb', line 77

def includes_all?(*grams)
  grams.all? { |gram| include?(gram) }
end

#includes_any?(*grams) ⇒ Boolean

Returns:

  • (Boolean)


73
74
75
# File 'lib/raingrams/ngram.rb', line 73

def includes_any?(*grams)
  grams.any? { |gram| include?(gram) }
end

#inspectObject



93
94
95
# File 'lib/raingrams/ngram.rb', line 93

def inspect
  'Ngram[' + self.map { |gram| gram.inspect }.join(', ') + ']'
end

#postfixObject



53
54
55
# File 'lib/raingrams/ngram.rb', line 53

def postfix
  self[1..-1]
end

#postfixed_by?(ngram) ⇒ Boolean

Returns:

  • (Boolean)


57
58
59
# File 'lib/raingrams/ngram.rb', line 57

def postfixed_by?(ngram)
  postfix == ngram
end

#prefixObject

Returns the prefix of the ngram.



41
42
43
# File 'lib/raingrams/ngram.rb', line 41

def prefix
  self[0...length-1]
end

#prefixed_by?(smaller_ngram) ⇒ Boolean

Returns true if the ngram is prefixed by the specified smaller_ngram.

Returns:

  • (Boolean)


49
50
51
# File 'lib/raingrams/ngram.rb', line 49

def prefixed_by?(smaller_ngram)
  prefix == smaller_ngram
end

#starts_with?(obj) ⇒ Boolean

Returns:

  • (Boolean)


61
62
63
# File 'lib/raingrams/ngram.rb', line 61

def starts_with?(obj)
  self.first == obj.to_gram
end

#to_sObject



89
90
91
# File 'lib/raingrams/ngram.rb', line 89

def to_s
  join(', ')
end