Module: Wordmap::Builder
- Defined in:
- lib/wordmap/builder.rb
Class Method Summary collapse
- .build_vectors(hash) ⇒ Object
- .rjust_bytes(string, bytesize, spacer) ⇒ Object
-
.write_data(path, vecs, cells_c, hash, spacer) ⇒ Object
TODO: drop null bytes at the beginning (offset in meta) TODO: drop null bytes at the end.
- .write_vector(path, vector, spacer) ⇒ Object
Class Method Details
.build_vectors(hash) ⇒ Object
7 8 9 10 11 12 |
# File 'lib/wordmap/builder.rb', line 7 def build_vectors(hash) vectors = hash.first[0].is_a?(Array) ? hash.keys.transpose : [hash.keys] vectors.map!(&:uniq) vectors.map!(&:sort) vectors end |
.rjust_bytes(string, bytesize, spacer) ⇒ Object
44 45 46 47 |
# File 'lib/wordmap/builder.rb', line 44 def rjust_bytes(string, bytesize, spacer) difference = bytesize - string.bytesize (spacer * difference) + string end |
.write_data(path, vecs, cells_c, hash, spacer) ⇒ Object
TODO: drop null bytes at the beginning (offset in meta) TODO: drop null bytes at the end
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/wordmap/builder.rb', line 16 def write_data(path, vecs, cells_c, hash, spacer) File.open("#{path}/data", 'wb') do |file| cell_w = hash.values.max_by(&:bytesize).bytesize file.write("#{cell_w},#{cells_c}#{spacer}") key_iterator = vecs.size == 1 ? vecs[0].each : vecs[0].product(*vecs[1..-1]).to_enum key_iterator.with_index do |key, cell_i| value = hash[key].to_s yield(key, value, cell_i) unless value.empty? file.write(rjust_bytes(value, cell_w, spacer)) end end end |
.write_vector(path, vector, spacer) ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/wordmap/builder.rb', line 32 def write_vector(path, vector, spacer) cell_w = vector.max_by(&:bytesize).bytesize File.open(path, 'wb') do |file| file.write("#{cell_w},#{vector.size}#{spacer}") vector.each do |key| file.write(rjust_bytes(key.to_s, cell_w, spacer)) end end end |