Class: Interscript::Detector
- Inherits:
-
Object
- Object
- Interscript::Detector
- Defined in:
- lib/interscript/detector.rb
Defined Under Namespace
Modules: DistanceComputer
Constant Summary collapse
- CACHE =
{}
Instance Attribute Summary collapse
-
#cache ⇒ Object
Returns the value of attribute cache.
-
#compiler ⇒ Object
Returns the value of attribute compiler.
-
#distance_computer ⇒ Object
Returns the value of attribute distance_computer.
-
#each ⇒ Object
TODO: use transliterate_each.
-
#load_path ⇒ Object
Returns the value of attribute load_path.
-
#map_pattern ⇒ Object
Returns the value of attribute map_pattern.
-
#multiple ⇒ Object
Returns a summary of all detected transliterations.
Instance Method Summary collapse
- #call(source, destination) ⇒ Object
-
#initialize ⇒ Detector
constructor
A new instance of Detector.
- #set_from_kwargs(**kwargs) ⇒ Object
Constructor Details
#initialize ⇒ Detector
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
#cache ⇒ Object
Returns the value of attribute cache.
12 13 14 |
# File 'lib/interscript/detector.rb', line 12 def cache @cache end |
#compiler ⇒ Object
Returns the value of attribute compiler.
4 5 6 |
# File 'lib/interscript/detector.rb', line 4 def compiler @compiler end |
#distance_computer ⇒ Object
Returns the value of attribute distance_computer.
5 6 7 |
# File 'lib/interscript/detector.rb', line 5 def distance_computer @distance_computer end |
#each ⇒ Object
TODO: use transliterate_each
9 10 11 |
# File 'lib/interscript/detector.rb', line 9 def each @each end |
#load_path ⇒ Object
Returns the value of attribute load_path.
11 12 13 |
# File 'lib/interscript/detector.rb', line 11 def load_path @load_path end |
#map_pattern ⇒ Object
Returns the value of attribute map_pattern.
6 7 8 |
# File 'lib/interscript/detector.rb', line 6 def map_pattern @map_pattern end |
#multiple ⇒ Object
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 |