Class: Ai::Nlp::Hasher

Inherits:
Object
  • Object
show all
Defined in:
lib/ai/nlp/n_gram/hasher.rb

Overview

Class managing an n-gram hash

Instance Method Summary collapse

Constructor Details

#initialize(input) ⇒ Hasher

Initialisation

Parameters:

  • string

    input The string to treat



18
19
20
21
22
# File 'lib/ai/nlp/n_gram/hasher.rb', line 18

def initialize(input)
  @input = input
  @hash = {}
  clean
end

Instance Method Details

#calculateObject

Calculates n-gram frequencies for the dataset

Returns:

  • Frequencies of ngram or sorted array



27
28
29
30
31
32
33
# File 'lib/ai/nlp/n_gram/hasher.rb', line 27

def calculate
  @input.split(/[\d\s\[\]]/).each do |word|
    calculate_word_gram("_#{word}_")
  end
  drop_unwanted_keys
  @hash.sort { |one, other| other[1] <=> one[1] }
end