Class: Omnizip::Algorithms::PPMd7::Context

Inherits:
Object
  • Object
show all
Includes:
Constants
Defined in:
lib/omnizip/algorithms/ppmd7/context.rb

Overview

Represents a context node in the PPMd7 model

A context in PPMd is a sequence of symbols that have appeared previously. Each context tracks the symbols that have followed it and their frequencies. This forms a tree structure where each node represents a different context.

The context uses symbol states to track:

  • Which symbols have appeared after this context
  • How frequently each symbol has appeared
  • Escape probabilities for unknown symbols

Constant Summary

Constants included from Constants

Omnizip::Algorithms::PPMd7::Constants::ALPHABET_SIZE, Omnizip::Algorithms::PPMd7::Constants::BIN_SCALE, Omnizip::Algorithms::PPMd7::Constants::BOT_VALUE, Omnizip::Algorithms::PPMd7::Constants::DEFAULT_MEM_SIZE, Omnizip::Algorithms::PPMd7::Constants::DEFAULT_ORDER, Omnizip::Algorithms::PPMd7::Constants::INIT_ESCAPE_FREQ, Omnizip::Algorithms::PPMd7::Constants::INTERVAL, Omnizip::Algorithms::PPMd7::Constants::INT_BITS, Omnizip::Algorithms::PPMd7::Constants::MAX_FREQ, Omnizip::Algorithms::PPMd7::Constants::MAX_MEM_SIZE, Omnizip::Algorithms::PPMd7::Constants::MAX_ORDER, Omnizip::Algorithms::PPMd7::Constants::MAX_STATES, Omnizip::Algorithms::PPMd7::Constants::MIN_MEM_SIZE, Omnizip::Algorithms::PPMd7::Constants::MIN_ORDER, Omnizip::Algorithms::PPMd7::Constants::PERIOD_BITS, Omnizip::Algorithms::PPMd7::Constants::PROB_TOTAL, Omnizip::Algorithms::PPMd7::Constants::SEE_CONTEXTS, Omnizip::Algorithms::PPMd7::Constants::SUFFIX_CONTEXTS, Omnizip::Algorithms::PPMd7::Constants::TOP_VALUE, Omnizip::Algorithms::PPMd7::Constants::UNIT_ALLOC_SIZE, Omnizip::Algorithms::PPMd7::Constants::UNIT_SIZE

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(order, suffix = nil) ⇒ Context

Initialize a new context

Parameters:

  • order (Integer)

    The order of this context (depth in tree)

  • suffix (Context, nil) (defaults to: nil)

    Parent context (shorter context)



47
48
49
50
51
52
53
# File 'lib/omnizip/algorithms/ppmd7/context.rb', line 47

def initialize(order, suffix = nil)
  @order = order
  @suffix = suffix
  @states = {} # symbol => SymbolState
  @sum_freq = 0
  @escape_freq = INIT_ESCAPE_FREQ
end

Instance Attribute Details

#escape_freqObject

Returns the value of attribute escape_freq.



41
42
43
# File 'lib/omnizip/algorithms/ppmd7/context.rb', line 41

def escape_freq
  @escape_freq
end

#orderObject (readonly)

Returns the value of attribute order.



40
41
42
# File 'lib/omnizip/algorithms/ppmd7/context.rb', line 40

def order
  @order
end

#statesObject (readonly)

Returns the value of attribute states.



40
41
42
# File 'lib/omnizip/algorithms/ppmd7/context.rb', line 40

def states
  @states
end

#suffixObject (readonly)

Returns the value of attribute suffix.



40
41
42
# File 'lib/omnizip/algorithms/ppmd7/context.rb', line 40

def suffix
  @suffix
end

#sum_freqObject (readonly)

Returns the value of attribute sum_freq.



40
41
42
# File 'lib/omnizip/algorithms/ppmd7/context.rb', line 40

def sum_freq
  @sum_freq
end

Instance Method Details

#add_symbol(symbol, freq = 1) ⇒ SymbolState

Add a new symbol to this context

This is called when a symbol appears for the first time after this context.

Parameters:

  • symbol (Integer)

    The symbol to add (0-255)

  • freq (Integer) (defaults to: 1)

    Initial frequency (default: 1)

Returns:

Raises:

  • (ArgumentError)


71
72
73
74
75
76
77
78
# File 'lib/omnizip/algorithms/ppmd7/context.rb', line 71

def add_symbol(symbol, freq = 1)
  raise ArgumentError, "Symbol already exists" if @states[symbol]

  state = SymbolState.new(symbol, freq)
  @states[symbol] = state
  @sum_freq += freq
  state
end

#find_symbol(symbol) ⇒ SymbolState?

Find a symbol state in this context

Parameters:

  • symbol (Integer)

    The symbol to find (0-255)

Returns:

  • (SymbolState, nil)

    The state if found, nil otherwise



59
60
61
# File 'lib/omnizip/algorithms/ppmd7/context.rb', line 59

def find_symbol(symbol)
  @states[symbol]
end

#needs_escape?Boolean

Check if context needs escape (has unseen symbols)

Returns:

  • (Boolean)

    True if escape symbol is needed



107
108
109
# File 'lib/omnizip/algorithms/ppmd7/context.rb', line 107

def needs_escape?
  @states.size < ALPHABET_SIZE
end

#num_symbolsInteger

Get the number of distinct symbols in this context

Returns:

  • (Integer)

    Number of symbols tracked



128
129
130
# File 'lib/omnizip/algorithms/ppmd7/context.rb', line 128

def num_symbols
  @states.size
end

#root?Boolean

Check if this is a root context

Returns:

  • (Boolean)

    True if this has no suffix (order 0)



121
122
123
# File 'lib/omnizip/algorithms/ppmd7/context.rb', line 121

def root?
  @suffix.nil?
end

#symbols_by_frequencyArray<Integer>

Get all symbols in order of frequency

Returns:

  • (Array<Integer>)

    Symbols sorted by frequency (desc)



114
115
116
# File 'lib/omnizip/algorithms/ppmd7/context.rb', line 114

def symbols_by_frequency
  @states.values.sort_by { |s| -s.freq }.map(&:symbol)
end

#total_freqInteger

Get total frequency including escape

Returns:

  • (Integer)

    Total frequency for probability calculation



100
101
102
# File 'lib/omnizip/algorithms/ppmd7/context.rb', line 100

def total_freq
  @sum_freq + @escape_freq
end

#update_symbol(symbol, increment = 1) ⇒ void

This method returns an undefined value.

Update symbol frequency after encoding/decoding

Increases the frequency count for the symbol and updates the total frequency sum for this context.

Parameters:

  • symbol (Integer)

    The symbol to update

  • increment (Integer) (defaults to: 1)

    Amount to increase frequency



88
89
90
91
92
93
94
95
# File 'lib/omnizip/algorithms/ppmd7/context.rb', line 88

def update_symbol(symbol, increment = 1)
  state = @states[symbol]
  return unless state

  state.freq += increment
  @sum_freq += increment
  rescale_frequencies if @sum_freq > MAX_FREQ
end