Class: Informers::SentimentAnalysis

Inherits:
Object
  • Object
show all
Defined in:
lib/informers/sentiment_analysis.rb

Instance Method Summary collapse

Constructor Details

#initialize(model_path) ⇒ SentimentAnalysis

Returns a new instance of SentimentAnalysis.



18
19
20
21
22
# File 'lib/informers/sentiment_analysis.rb', line 18

def initialize(model_path)
  tokenizer_path = File.expand_path("../../vendor/bert_base_tok.bin", __dir__)
  @tokenizer = BlingFire.load_model(tokenizer_path)
  @model = OnnxRuntime::Model.new(model_path)
end

Instance Method Details

#predict(texts) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/informers/sentiment_analysis.rb', line 24

def predict(texts)
  singular = !texts.is_a?(Array)
  texts = [texts] if singular

  # tokenize
  input_ids =
    texts.map do |text|
      tokens = @tokenizer.text_to_ids(text, nil, 100) # unk token
      tokens.unshift(101) # cls token
      tokens << 102 # sep token
      tokens
    end

  max_tokens = input_ids.map(&:size).max
  attention_mask = []
  input_ids.each do |ids|
    zeros = [0] * (max_tokens - ids.size)

    mask = ([1] * ids.size) + zeros
    attention_mask << mask

    ids.concat(zeros)
  end

  # infer
  input = {
    input_ids: input_ids,
    attention_mask: attention_mask
  }
  output = @model.predict(input)

  # transform
  scores =
    output["output_0"].map do |row|
      mapped = row.map { |v| Math.exp(v) }
      sum = mapped.sum
      mapped.map { |v| v / sum }
    end

  labels = ["negative", "positive"]
  scores.map! do |item|
    {label: labels[item.each_with_index.max[1]], score: item.max}
  end

  singular ? scores.first : scores
end