Class: Decompound::Packer
- Inherits:
-
Object
- Object
- Decompound::Packer
- Defined in:
- lib/decompound/packer.rb
Overview
Compiles the trained JSON probability tables into the binary runtime model, see Decompound::Model
Constant Summary collapse
Instance Method Summary collapse
Instance Method Details
#pack(probabilities) ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/decompound/packer.rb', line 11 def pack(probabilities) entries = Hash.new { |hash, key| hash[key] = [Model::MISSING] * 3 } POSITIONS.each do |position, slot| probabilities.fetch(position).each do |ngram, probability| entries[ngram.b][slot] = (probability.clamp(0.0, 1.0) * Model::SCALE).round end end keys = entries.keys.sort! offsets = [] blob = +"" buckets = +"" bucket_count = 0 previous_prefix = nil keys.each_with_index do |key, index| prefix = key.byteslice(0, 3).ljust(3, "\0") if prefix != previous_prefix buckets << prefix << [index].pack("V") bucket_count += 1 previous_prefix = prefix end offsets << blob.bytesize blob << key end offsets << blob.bytesize Model::MAGIC.b + [keys.length, bucket_count].pack("VV") + buckets + offsets.pack("V*") + keys.flat_map { |key| entries.fetch(key) }.pack("C*") + blob end |