class Interscript::Detector

Constants

CACHE

Attributes

cache[RW]
compiler[RW]
distance_computer[RW]
each[RW]

TODO: use transliterate_each

load_path[RW]
map_pattern[RW]
multiple[RW]

Returns a summary of all detected transliterations

Public Class Methods

new() click to toggle source
# File lib/interscript/detector.rb, line 17
def initialize
  @compiler = Interscript::Interpreter
  @distance_computer = DistanceComputer::Levenshtein
  @map_pattern = "*"

  @each = false

  @load_path = false
  @cache = CACHE
end

Public Instance Methods

call(source, destination) click to toggle source
# File lib/interscript/detector.rb, line 34
def call(source, destination)
  maps = Interscript.maps(select: @map_pattern, load_path: @load_path)
  maps = Interscript.exclude_maps(maps, compiler: self.class)
  maps = Interscript.exclude_maps(maps, compiler: @compiler)

  summary = maps.map do |map|
    try_dest = Interscript.transliterate(map, source, compiler: @compiler)

    [map, try_dest]
  end.map do |map, try_dest|
    dist = @distance_computer.(try_dest, destination)
    
    [map, dist]
  end.sort_by(&:last).to_h

  if @multiple
    summary.to_h
  else
    summary.first.first
  end
end
set_from_kwargs(**kwargs) click to toggle source
# File lib/interscript/detector.rb, line 28
def set_from_kwargs(**kwargs)
  kwargs.each do |k,v|
    self.public_send(:"#{k}=", v)
  end
end