Sha256: a41a99ebbb419add03e66c91616cc6f8847d2d307fbcf97917ffce452cced2fa

Contents?: true

Size: 1.21 KB

Versions: 5

Compression:

Stored size: 1.21 KB

Contents

module FastText
  class Model
    def initialize(**options)
      @options = options
    end

    def dimension
      m.dimension
    end

    def quantized?
      m.quantized?
    end

    def save_model(path)
      m.save_model(path)
    end

    def words(include_freq: false)
      words, freqs = m.words
      if include_freq
        words.zip(freqs).to_h
      else
        words
      end
    end

    def word_vector(word)
      m.word_vector(word)
    end

    def sentence_vector(text)
      m.sentence_vector(prep_text(text))
    end

    def word_id(word)
      m.word_id(word)
    end

    def subword_id(subword)
      m.subword_id(subword)
    end

    def subwords(word)
      m.subwords(word)
    end

    private

    # text must end in newline for prediction to match Python and CLI
    def prep_text(text)
      text = "#{text}\n" unless text.end_with?("\n")
      text
    end

    def m
      @m || (raise Error, "Not fit")
    end

    def build_args(default_options)
      a = Ext::Args.new
      opts = @options.dup
      default_options.each do |k, v|
        a.send("#{k}=", opts.delete(k) || v)
      end
      raise ArgumentError, "Unknown argument: #{opts.keys.first}" if opts.any?
      a
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
fasttext-0.4.0 lib/fasttext/model.rb
fasttext-0.3.0 lib/fasttext/model.rb
fasttext-0.2.4 lib/fasttext/model.rb
fasttext-0.2.3 lib/fasttext/model.rb
fasttext-0.2.2 lib/fasttext/model.rb