Sha256: 41e26221423589d48291a5ddc8cf9692280a3c56a5df65b55329d98b98f8f444

Contents?: true

Size: 1.57 KB

Versions: 3

Compression:

Stored size: 1.57 KB

Contents

# File: digram.rb

module Sequitur # Module for classes implementing the Sequitur algorithm

# 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.
class Digram
  # The sequence of two consecutive grammar symbols.
  #   The two symbols should respond to the :hash message.
  attr_reader(:symbols)

  # An unique hash key of the digram
  attr_reader(:key)

  # The production in which the digram occurs
  attr_reader(:production)

  # 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.
  # @param symbol1 [StringOrSymbol] First element of the digram
  # @param symbol2 [StringOrSymbol] Second element of the digram
  # @param aProduction [Production] Production in which the RHS
  #   the sequence symbol1 symbol2 appears.
  def initialize(symbol1, symbol2, aProduction)
    @symbols = [symbol1, symbol2]
    @key = symbol1.hash.to_s(16) + ':' + symbol2.hash.to_s(16)
    @production = aProduction
  end

  # Equality testing.
  #   true iff keys of both digrams are equal, false otherwise
  # @param other [Digram] another to compare with
  # @return [true/false]
  def ==(other)
    return key == other.key
  end

  # Does the digram consists of twice the same symbols?
  # @return [true/false] true when symbols.first == symbols.last
  def repeating?()
    return symbols[0] == symbols[1]
  end

end # class

end # module

# End of file

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
sequitur-0.1.13 lib/sequitur/digram.rb
sequitur-0.1.12 lib/sequitur/digram.rb
sequitur-0.1.11 lib/sequitur/digram.rb