Class: Sequitur::Digram

Inherits:
Object
  • Object
show all
Defined in:
lib/sequitur/digram.rb

Overview

In linguistics, a digram is a sequence of two letters. In Sequitur, a digram is a sequence of two consecutive symbols that appear in a production rule. Each symbol in a digram can be a terminal or not.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(symbol1, symbol2, aProduction) ⇒ Digram

Constructor. A digram represents a sequence of two symbols (that appears in a rhs of a production). Terminal symbols must respond to the :hash message.

Parameters:

  • symbol1 (String, Symbol)

    First element of the digram

  • symbol2 (String, Symbol)

    Second element of the digram

  • aProduction (Sequitur::Production)

    Production in which the RHS the sequence symbol1 symbol2 appears.



29
30
31
32
33
# File 'lib/sequitur/digram.rb', line 29

def initialize(symbol1, symbol2, aProduction)
  @symbols = [symbol1, symbol2]
  @key = "#{symbol1.hash.to_s(16)}:#{symbol2.hash.to_s(16)}"
  @production = aProduction
end

Instance Attribute Details

#keyString (readonly)

Returns An unique hash key of the digram.

Returns:

  • (String)

    An unique hash key of the digram



16
17
18
# File 'lib/sequitur/digram.rb', line 16

def key
  @key
end

#productionSequitur::Production (readonly)

Returns The production in which the digram occurs.

Returns:



19
20
21
# File 'lib/sequitur/digram.rb', line 19

def production
  @production
end

#symbolsArray<String, Symbol> (readonly)

The sequence of two consecutive grammar symbols.

Returns:

  • (Array<String, Symbol>)

    The two symbols should respond to the :hash message.



13
14
15
# File 'lib/sequitur/digram.rb', line 13

def symbols
  @symbols
end

Instance Method Details

#==(other) ⇒ TrueClass, FalseClass

Equality testing. true iff keys of both digrams are equal, false otherwise

Parameters:

Returns:

  • (TrueClass, FalseClass)


39
40
41
# File 'lib/sequitur/digram.rb', line 39

def ==(other)
  key == other.key
end

#repeating?TrueClass, FalseClass

Does the digram consists of twice the same symbols?

Returns:

  • (TrueClass, FalseClass)

    true when symbols.first == symbols.last



45
46
47
# File 'lib/sequitur/digram.rb', line 45

def repeating?
  symbols[0] == symbols[1]
end