Skip to content

Commit 70f1e4f

Browse files
author
webdev778
committed
Implement detector (#728)
1 parent 8020b3c commit 70f1e4f

File tree

3 files changed

+74
-1
lines changed

3 files changed

+74
-1
lines changed

ruby/interscript.gemspec

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,6 @@ Gem::Specification.new do |spec|
2727
spec.require_paths = ["lib"]
2828

2929
spec.add_dependency "thor"
30-
spec.add_dependency "interscript-maps"
30+
spec.add_dependency "interscript-maps", "~> #{Interscript::VERSION.split('.')[0,2].join(".")}.0"
31+
spec.add_dependency "text"
3132
end

ruby/lib/interscript.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,16 @@ def transliterate_file(system_code, input_file, output_file, maps={})
5353
output_file
5454
end
5555

56+
# Detects the transliteration that gives the most close approximation
57+
# of transliterating source into destination.
58+
#
59+
# Set multiple: true to get a full report.
60+
def detect(source, destination, **kwargs)
61+
detector = Detector.new
62+
detector.set_from_kwargs(**kwargs)
63+
detector.(source, destination)
64+
end
65+
5666
def map_gems
5767
@map_gems ||= Gem.find_latest_files('interscript-maps.yaml').map do |i|
5868
[i, YAML.load_file(i)]
@@ -109,3 +119,5 @@ def maps(basename: true, load_path: false, select: "*", libraries: false)
109119

110120
require 'interscript/dsl'
111121
require 'interscript/node'
122+
123+
require 'interscript/detector'

ruby/lib/interscript/detector.rb

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
require "text"
2+
3+
class Interscript::Detector
4+
attr_accessor :compiler
5+
attr_accessor :distance_computer
6+
attr_accessor :map_pattern
7+
8+
# TODO: use transliterate_each
9+
attr_accessor :each
10+
11+
attr_accessor :load_path
12+
attr_accessor :cache
13+
14+
# Returns a summary of all detected transliterations
15+
attr_accessor :multiple
16+
17+
def initialize
18+
@compiler = Interscript::Interpreter
19+
@distance_computer = DistanceComputer::Levenshtein
20+
@map_pattern = "*"
21+
22+
@each = false
23+
24+
@load_path = false
25+
@cache = CACHE
26+
end
27+
28+
def set_from_kwargs(**kwargs)
29+
kwargs.each do |k,v|
30+
self.public_send(:"#{k}=", v)
31+
end
32+
end
33+
34+
def call(source, destination)
35+
maps = Interscript.maps(select: @map_pattern, load_path: @load_path)
36+
37+
summary = maps.map do |map|
38+
try_dest = Interscript.transliterate(map, source, compiler: @compiler)
39+
40+
[map, try_dest]
41+
end.map do |map, try_dest|
42+
dist = @distance_computer.(try_dest, destination)
43+
44+
[map, dist]
45+
end.sort_by(&:last).to_h
46+
47+
if @multiple
48+
summary.to_h
49+
else
50+
summary.first.first
51+
end
52+
end
53+
54+
CACHE = {}
55+
56+
# A DistanceComputer needs to respond to #call(source, destination)
57+
module DistanceComputer
58+
Levenshtein = Text::Levenshtein.method(:distance)
59+
end
60+
end

0 commit comments

Comments
 (0)