Sha256: 145c5cc14274c0b6d60411edcce63c8aea964c14dadaba3efc6f5e9cfceb367f

Contents?: true

Size: 1.92 KB

Versions: 3

Compression:

Stored size: 1.92 KB

Contents

module BlingFire
  class Model
    def initialize(path = nil, prefix: nil)
      @handle = nil
      if path
        raise Error, "Model not found" unless File.exist?(path)
        @handle = FFI.LoadModel(path)
        @handle.free = FFI["FreeModel"]

        BlingFire.change_settings_dummy_prefix(@handle, prefix) unless prefix.nil?
      else
        raise Error, "prefix option requires path" unless prefix.nil?
      end
    end

    def text_to_words(text)
      if @handle
        BlingFire.text_to_words_with_model(@handle, text)
      else
        BlingFire.text_to_words(text)
      end
    end

    def text_to_words_with_offsets(text)
      if @handle
        BlingFire.text_to_words_with_offsets_with_model(@handle, text)
      else
        BlingFire.text_to_words_with_offsets(text)
      end
    end

    def text_to_sentences(text)
      if @handle
        BlingFire.text_to_sentences_with_model(@handle, text)
      else
        BlingFire.text_to_sentences(text)
      end
    end

    def text_to_sentences_with_offsets(text)
      if @handle
        BlingFire.text_to_sentences_with_offsets_with_model(@handle, text)
      else
        BlingFire.text_to_sentences_with_offsets(text)
      end
    end

    def text_to_ids(text, max_len = nil, unk_id = 0)
      if @handle
        BlingFire.text_to_ids(@handle, text, max_len, unk_id)
      else
        raise "Not implemented"
      end
    end

    def text_to_ids_with_offsets(text, max_len = nil, unk_id = 0)
      if @handle
        BlingFire.text_to_ids_with_offsets(@handle, text, max_len, unk_id)
      else
        raise "Not implemented"
      end
    end

    def ids_to_text(ids, skip_special_tokens: true, output_buffer_size: nil)
      if @handle
        BlingFire.ids_to_text(@handle, ids, skip_special_tokens: skip_special_tokens, output_buffer_size: output_buffer_size)
      else
        raise "Not implemented"
      end
    end

    def to_ptr
      @handle
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
blingfire-0.2.2 lib/blingfire/model.rb
blingfire-0.2.1 lib/blingfire/model.rb
blingfire-0.2.0 lib/blingfire/model.rb