Class: Ai::Nlp::Languages

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

Overview

Class to handle multiple languages

Instance Method Summary collapse

Constructor Details

#initializeLanguages

Initialisation



14
15
16
# File 'lib/ai/nlp/languages.rb', line 14

def initialize
  @n_gram = NGram.new
end

Instance Method Details

#allObject

Returns the currently known languages

Returns:

  • An array of Language



21
22
23
# File 'lib/ai/nlp/languages.rb', line 21

def all
  @languages = Language.all
end

#create_one(name, input) ⇒ Object

Create a new language.

Parameters:

  • string

    name The language name.

  • string

    input The initial data set.

Returns:

  • La langue créée.



40
41
42
43
# File 'lib/ai/nlp/languages.rb', line 40

def create_one(name, input)
  language = Language.new(name: name)
  language.update(map: @n_gram.calculate(input).to_h)
end

#guess(input) ⇒ Object

Offers among the available languages the closest one to the datasets

Parameters:

  • string

    input The data set.



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

def guess(input)
  all
  return [] if @languages.empty?
  hash = @languages.map { |language| [language, score(input, language)] }.to_h
  sort(hash)
end