Class: Omnizip::Algorithms::PPMd7::Model

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

Overview

Core PPMd7 prediction model

The model maintains a tree of contexts, where each context represents a sequence of symbols that have appeared in the input. The model predicts the next symbol based on the current context's statistics.

The model uses Prediction by Partial Matching (PPM), which tries progressively shorter contexts until it finds one that has seen the current symbol before.

Constant Summary

Constants included from Constants

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(max_order = DEFAULT_ORDER, mem_size = DEFAULT_MEM_SIZE) ⇒ Model

Initialize the PPMd7 model

Parameters:

  • max_order (Integer) (defaults to: DEFAULT_ORDER)

    Maximum context order (2-16)

  • mem_size (Integer) (defaults to: DEFAULT_MEM_SIZE)

    Memory size for context allocation



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/omnizip/algorithms/ppmd7/model.rb', line 45

def initialize(max_order = DEFAULT_ORDER, mem_size = DEFAULT_MEM_SIZE)
  validate_parameters(max_order, mem_size)

  @max_order = max_order
  @mem_size = mem_size

  # Initialize context tree with root (order -1)
  @root_context = Context.new(-1, nil)
  @current_context = @root_context

  # Context history for maintaining context chain
  @context_history = []

  # Initialize root context with uniform distribution
  initialize_root_context
end

Instance Attribute Details

#current_contextObject (readonly)

Returns the value of attribute current_context.



39
40
41
# File 'lib/omnizip/algorithms/ppmd7/model.rb', line 39

def current_context
  @current_context
end

#max_orderObject (readonly)

Returns the value of attribute max_order.



39
40
41
# File 'lib/omnizip/algorithms/ppmd7/model.rb', line 39

def max_order
  @max_order
end

#mem_sizeObject (readonly)

Returns the value of attribute mem_size.



39
40
41
# File 'lib/omnizip/algorithms/ppmd7/model.rb', line 39

def mem_size
  @mem_size
end

#root_contextObject (readonly)

Returns the value of attribute root_context.



39
40
41
# File 'lib/omnizip/algorithms/ppmd7/model.rb', line 39

def root_context
  @root_context
end

Instance Method Details

#get_symbol_probability(symbol = nil) ⇒ Hash

Get probability information for encoding/decoding a symbol

Returns information needed by range coder:

  • cumulative frequency up to symbol
  • symbol frequency
  • total frequency
  • whether this is an escape

Parameters:

  • symbol (Integer, nil) (defaults to: nil)

    Symbol to encode (nil for decode)

Returns:

  • (Hash)

    Probability information



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/omnizip/algorithms/ppmd7/model.rb', line 72

def get_symbol_probability(symbol = nil)
  context = find_context_with_symbol(symbol)

  if context && (state = context.find_symbol(symbol))
    # Symbol found in context
    cum_freq = cumulative_frequency(context, symbol)
    {
      context: context,
      cumulative_freq: cum_freq,
      freq: state.freq,
      total_freq: context.total_freq,
      escape: false,
    }
  else
    # Use escape symbol
    cum_freq = escape_cumulative_frequency(context || @root_context)
    {
      context: context || @root_context,
      cumulative_freq: cum_freq,
      freq: (context || @root_context).escape_freq,
      total_freq: (context || @root_context).total_freq,
      escape: true,
    }
  end
end

#resetvoid

This method returns an undefined value.

Reset model to initial state



120
121
122
123
124
125
# File 'lib/omnizip/algorithms/ppmd7/model.rb', line 120

def reset
  @root_context = Context.new(-1, nil)
  @current_context = @root_context
  @context_history.clear
  initialize_root_context
end

#update(symbol) ⇒ void

This method returns an undefined value.

Update model after encoding/decoding a symbol

This updates context statistics and creates new contexts as needed.

Parameters:

  • symbol (Integer)

    The symbol that was encoded/decoded



105
106
107
108
109
110
111
112
113
114
115
# File 'lib/omnizip/algorithms/ppmd7/model.rb', line 105

def update(symbol)
  # Update current context or create new symbol
  if @current_context.find_symbol(symbol)
    @current_context.update_symbol(symbol)
  else
    @current_context.add_symbol(symbol)
  end

  # Move to next context
  update_current_context(symbol)
end