Class: Interscript::Detector

Inherits:
Object
  • Object
show all
Defined in:
lib/interscript/detector.rb

Defined Under Namespace

Modules: DistanceComputer

Constant Summary collapse

CACHE =
{}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeDetector

Returns a new instance of Detector.



17
18
19
20
21
22
23
24
25
26
# 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

Instance Attribute Details

#cacheObject

Returns the value of attribute cache.



12
13
14
# File 'lib/interscript/detector.rb', line 12

def cache
  @cache
end

#compilerObject

Returns the value of attribute compiler.



4
5
6
# File 'lib/interscript/detector.rb', line 4

def compiler
  @compiler
end

#distance_computerObject

Returns the value of attribute distance_computer.



5
6
7
# File 'lib/interscript/detector.rb', line 5

def distance_computer
  @distance_computer
end

#eachObject

TODO: use transliterate_each



9
10
11
# File 'lib/interscript/detector.rb', line 9

def each
  @each
end

#load_pathObject

Returns the value of attribute load_path.



11
12
13
# File 'lib/interscript/detector.rb', line 11

def load_path
  @load_path
end

#map_patternObject

Returns the value of attribute map_pattern.



6
7
8
# File 'lib/interscript/detector.rb', line 6

def map_pattern
  @map_pattern
end

#multipleObject

Returns a summary of all detected transliterations



15
16
17
# File 'lib/interscript/detector.rb', line 15

def multiple
  @multiple
end

Instance Method Details

#call(source, destination) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# 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) ⇒ Object



28
29
30
31
32
# File 'lib/interscript/detector.rb', line 28

def set_from_kwargs(**kwargs)
  kwargs.each do |k,v|
    self.public_send(:"#{k}=", v)
  end
end