Class: Glove::Workers::CooccurrenceWorker

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/glove/workers/cooccurrence_worker.rb

Overview

Constructs the co-occurrence matrix for Model

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(caller) ⇒ CooccurrenceWorker

Creates instance of the class

Parameters:



18
19
20
21
22
# File 'lib/glove/workers/cooccurrence_worker.rb', line 18

def initialize(caller)
  @caller = caller
  @token_index = @caller.token_index.dup
  @token_pairs = @caller.token_pairs.dup
end

Instance Attribute Details

#token_indexHash{String=>Integer} (readonly)

Returns Clone of @caller.token_index.

Returns:

  • (Hash{String=>Integer})

    Clone of @caller.token_index



11
12
13
# File 'lib/glove/workers/cooccurrence_worker.rb', line 11

def token_index
  @token_index
end

#token_pairsObject (readonly)

Returns the value of attribute token_pairs.



11
12
13
# File 'lib/glove/workers/cooccurrence_worker.rb', line 11

def token_pairs
  @token_pairs
end

#word_biasesArray<(Glove::TokenPair)> (readonly)

Returns Clone of @caller.token_pairs.

Returns:



11
# File 'lib/glove/workers/cooccurrence_worker.rb', line 11

attr_reader :token_index, :token_pairs

Instance Method Details

#build_cooc_matrix_col(slice) ⇒ Array

Creates a vector column for the cooc_matrix based on given token. Calculates sum for how many times the word exists in the constext of the entire vocabulary

Parameters:

  • slice (Array<(String, Integer)>)

    Token with index

Returns:

  • (Array)

    GSL::Vector#to_a representation of the column



41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/glove/workers/cooccurrence_worker.rb', line 41

def build_cooc_matrix_col(slice)
  token = slice[0]
  vector = GSL::Vector.alloc(token_index.size)

  token_pairs.each do |pair|
    key = token_index[pair.token]
    sum = pair.neighbors.select{ |word| word == token }.size
    vector[key] += sum
  end

  vector.to_a
end

#runGSL::Matrix

Perform the building of the matrix

Returns:

  • (GSL::Matrix)

    The co-occurrence matrix



27
28
29
30
31
32
33
# File 'lib/glove/workers/cooccurrence_worker.rb', line 27

def run
  vectors = Parallel.map(token_index, in_processes: threads) do |slice|
    build_cooc_matrix_col(slice)
  end

  GSL::Matrix.alloc(*vectors)
end