Class: JSGF::Alternation

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/jsgf/alternation.rb

Instance Attribute Summary collapse

Enumerable collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Alternation

Returns a new instance of Alternation.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/jsgf/alternation.rb', line 9

def initialize(*args)
    @elements = args.map do |a|
  case a
      when String
    case a
        when /\<(.*)\>/, /:(.*)/ then {name:$1, weight:1.0, tags:[]}
        else
      {atom:a, weight:1.0, tags:[]}
    end
      when Symbol then {name:a.to_s, weight:1.0, tags:[]}
      else
    a
  end
    end

    @optional = false
    @tags = []
end

Instance Attribute Details

#elementsObject (readonly)

Returns the value of attribute elements.



5
6
7
# File 'lib/jsgf/alternation.rb', line 5

def elements
  @elements
end

#optionalObject

Returns the value of attribute optional.



6
7
8
# File 'lib/jsgf/alternation.rb', line 6

def optional
  @optional
end

#tagsObject (readonly)

Returns the value of attribute tags.



7
8
9
# File 'lib/jsgf/alternation.rb', line 7

def tags
  @tags
end

Instance Method Details

#each(*args, &block) ⇒ Object



29
30
31
# File 'lib/jsgf/alternation.rb', line 29

def each(*args, &block)
    elements.each(*args, &block)
end

#push(*args) ⇒ Object



34
35
36
# File 'lib/jsgf/alternation.rb', line 34

def push(*args)
    @elements.push *args
end

#sizeObject



38
39
40
# File 'lib/jsgf/alternation.rb', line 38

def size
    @elements.size
end

#weightsObject



42
43
44
# File 'lib/jsgf/alternation.rb', line 42

def weights
    @elements.map {|a| a[:weight]}
end