Sha256: 16324a45c15a3a4a38a3e91fbea1cc7320c3f122ff26c087eb48270f2ee5aa19

Contents?: true

Size: 1.62 KB

Versions: 3

Compression:

Stored size: 1.62 KB

Contents

module BlingFire
  class Model
    def initialize(path = nil)
      @handle = nil
      if path
        raise Error, "Model not found" unless File.exist?(path)
        @handle = FFI.LoadModel(path)
        ObjectSpace.define_finalizer(self, self.class.finalize(@handle))
      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 to_ptr
      @handle
    end

    def self.finalize(pointer)
      # must use proc instead of stabby lambda
      proc { FFI.FreeModel(pointer) }
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
blingfire-0.1.5 lib/blingfire/model.rb
blingfire-0.1.4 lib/blingfire/model.rb
blingfire-0.1.3 lib/blingfire/model.rb