Module: TextMetrics

Defined in:
lib/text_metrics/version.rb,
lib/text_metrics.rb,
lib/text_metrics/railtie.rb,
lib/text_metrics/levenshtein.rb,
lib/text_metrics/configuration.rb,
lib/text_metrics/processors/base.rb,
lib/text_metrics/processors/french.rb,
lib/text_metrics/processors/american_english.rb,
lib/generators/text_metrics/install_generator.rb

Overview

:nodoc:

Defined Under Namespace

Modules: Generators, Levenshtein, Processors Classes: Configuration, Error, Railtie

Constant Summary collapse

DEFAULT_LANGUAGE =
:en_us
PROCESSORS =
{
  en_us: Processors::AmericanEnglish,
  fr: Processors::French
}.freeze
LANGUAGE_ALIASES =

Coerce to a known language symbol, or raise a helpful error. Handles nil, strings and symbols without leaking a NoMethodError.

{en: :en_us}.freeze
VERSION =
"1.1.1"

Class Method Summary collapse

Class Method Details

.configurationObject



16
17
18
# File 'lib/text_metrics.rb', line 16

def configuration
  @configuration ||= Configuration.new
end

.configure {|configuration| ... } ⇒ Object

Yields:



20
21
22
# File 'lib/text_metrics.rb', line 20

def configure
  yield configuration
end

.distance(text, other) ⇒ Object

Raw Levenshtein edit distance between two texts.



44
45
46
# File 'lib/text_metrics.rb', line 44

def self.distance(text, other)
  Levenshtein.distance(text, other)
end

.new(text, language: DEFAULT_LANGUAGE) ⇒ Object

Build an analyzer for text in the given language (:en_us or :fr). Returns the language-specific processor, which exposes every metric and #to_h.



38
39
40
41
# File 'lib/text_metrics.rb', line 38

def self.new(text, language: DEFAULT_LANGUAGE)
  language = resolve_language(language)
  PROCESSORS.fetch(language).new(text, language: language)
end

.similarity(text, other) ⇒ Object

Levenshtein similarity between two texts, as a 0–100 score (100.0 == identical).



49
50
51
# File 'lib/text_metrics.rb', line 49

def self.similarity(text, other)
  Levenshtein.similarity(text, other)
end