Module: Interscript
- Defined in:
- lib/interscript.rb,
lib/interscript/command.rb,
lib/interscript/version.rb
Defined Under Namespace
Modules: DSL, Utils
Classes: Command, Compiler, Detector, Interpreter, MapNotFoundError, Node, Stdlib, Visualize
Constant Summary
collapse
- VERSION =
"2.4.2"
Class Method Summary
collapse
-
.detect(source, destination, **kwargs) ⇒ Object
Detects the transliteration that gives the most close approximation of transliterating source into destination.
-
.exclude_maps(maps, compiler:, platform: true) ⇒ Object
Removes the excluded maps for a given compiler and RUBY_PLATFORM.
-
.load(system_code, maps = {}, compiler: Interscript::Interpreter) ⇒ Object
-
.load_path ⇒ Object
-
.locate(map_name) ⇒ Object
-
.map_aliases ⇒ Object
-
.map_gems ⇒ Object
-
.map_locations ⇒ Object
-
.maps(basename: true, load_path: false, select: "*", libraries: false) ⇒ Object
List all possible maps to use.
-
.parse(map_name) ⇒ Object
-
.rababa_configs ⇒ Object
-
.rababa_provision(model_name, model_uri) ⇒ Object
This code is borrowed from Secryst and should end up in Rababa, but for now, let’s keep it here.
-
.secryst_index_locations ⇒ Object
-
.transliterate(system_code, string, maps = {}, compiler: Interscript::Interpreter) ⇒ Object
Transliterates the string.
-
.transliterate_each(system_code, string, maps = {}, &block) ⇒ Object
Gives each possible value of the transliteration.
-
.transliterate_file(system_code, input_file, output_file, maps = {}) ⇒ Object
Class Method Details
.detect(source, destination, **kwargs) ⇒ Object
Detects the transliteration that gives the most close approximation of transliterating source into destination.
Set multiple: true to get a full report.
60
61
62
63
64
|
# File 'lib/interscript.rb', line 60
def detect(source, destination, **kwargs)
detector = Detector.new
detector.set_from_kwargs(**kwargs)
detector.(source, destination)
end
|
.exclude_maps(maps, compiler:, platform: true) ⇒ Object
Removes the excluded maps for a given compiler and RUBY_PLATFORM. To be used by tests and builders. It uses the ‘skip` directive in interscript-maps.yaml
166
167
168
169
170
171
172
173
174
175
176
177
|
# File 'lib/interscript.rb', line 166
def exclude_maps(maps, compiler:, platform: true)
map_gems.each do |i,v|
[compiler.name, (Gem::Platform.local.os if platform)].compact.each do |name|
skips = v.dig('skip', name) || []
skips.each do |skip|
skip_re = /#{Regexp.escape(skip).gsub("\\*", ".*?")}/
maps = maps.grep_v(skip_re)
end
end
end
maps
end
|
.load(system_code, maps = {}, compiler: Interscript::Interpreter) ⇒ Object
29
30
31
|
# File 'lib/interscript.rb', line 29
def load(system_code, maps={}, compiler: Interscript::Interpreter)
maps[[system_code, compiler.name]] ||= compiler.(system_code)
end
|
.locate(map_name) ⇒ Object
12
13
14
15
16
17
18
19
20
21
22
23
|
# File 'lib/interscript.rb', line 12
def locate map_name
map_name = map_aliases[map_name] if map_aliases.include? map_name
load_path.each do |i|
["iml", "imp"].each do |ext|
f = File.expand_path("#{map_name}.#{ext}", i)
return f if File.exist?(f)
end
end
raise MapNotFoundError, "Couldn't locate #{map_name}"
end
|
.map_aliases ⇒ Object
139
140
141
142
143
144
145
146
147
148
149
150
151
|
# File 'lib/interscript.rb', line 139
def map_aliases
return @map_aliases if @map_aliases
@map_aliases = {}
map_gems.each do |i,v|
(v["aliases"] || {}).each do |code, value|
value.each do |al, map|
@map_aliases[al] = map["alias_to"]
end
end
end
@map_aliases
end
|
.map_gems ⇒ Object
66
67
68
69
70
|
# File 'lib/interscript.rb', line 66
def map_gems
@map_gems ||= Gem.find_latest_files('interscript-maps.yaml').map do |i|
[i, YAML.load_file(i)]
end.to_h
end
|
.map_locations ⇒ Object
72
73
74
75
76
77
78
79
80
81
|
# File 'lib/interscript.rb', line 72
def map_locations
@map_locations ||= map_gems.map do |i,v|
paths = v["paths"].dup
paths += v["staging"] if ENV["INTERSCRIPT_STAGING"] && v["staging"]
paths.map do |j|
File.expand_path(j, File.dirname(i))
end
end.flatten
end
|
.maps(basename: true, load_path: false, select: "*", libraries: false) ⇒ Object
List all possible maps to use
154
155
156
157
158
159
160
161
|
# File 'lib/interscript.rb', line 154
def maps(basename: true, load_path: false, select: "*", libraries: false)
paths = load_path ? Interscript.load_path : Interscript.map_locations
ext = libraries ? "iml" : "imp"
imps = paths.map { |i| Dir["#{i}/#{select}.#{ext}"] }.flatten
basename ? imps.map { |j| File.basename(j, ".#{ext}") } : imps
end
|
.parse(map_name) ⇒ Object
25
26
27
|
# File 'lib/interscript.rb', line 25
def parse(map_name)
Interscript::DSL.parse(map_name)
end
|
.rababa_configs ⇒ Object
89
90
91
92
93
94
95
|
# File 'lib/interscript.rb', line 89
def rababa_configs
@rababa_configs ||= map_gems.map do |i,v|
v["rababa-configs"]
end.compact.inject({}) do |a,b|
a.merge(b)
end
end
|
.rababa_provision(model_name, model_uri) ⇒ Object
This code is borrowed from Secryst and should end up in Rababa, but for now, let’s keep it here.
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
|
# File 'lib/interscript.rb', line 99
def rababa_provision(model_name, model_uri)
require 'fileutils'
require 'open-uri'
possible_paths = [
"/var/lib/rababa",
"/usr/local/share/rababa",
"/usr/share/rababa",
File.join(Dir.home, ".local/share/rababa")
]
write_path = nil
([ENV["RABABA_DATA"]] + possible_paths).compact.each do |path|
FileUtils.mkdir_p(path)
write_path = path unless write_path
rescue
end
raise StandardError, "Can't find a writable path for Rababa. Consider setting a RABABA_DATA environment variable" unless write_path
model_path = "#{write_path}/model-#{model_name}.onnx"
if File.exist?(model_path) && File.mtime(model_path) + 3600 >= Time.now
return model_path
else
data = URI.open(model_uri).read
File.write(model_path, data)
return model_path
end
end
|
.secryst_index_locations ⇒ Object
83
84
85
86
87
|
# File 'lib/interscript.rb', line 83
def secryst_index_locations
@secryst_index_locations ||= map_gems.map do |i,v|
v["secryst-models"]
end.compact.flatten
end
|
.transliterate(system_code, string, maps = {}, compiler: Interscript::Interpreter) ⇒ Object
Transliterates the string.
34
35
36
37
|
# File 'lib/interscript.rb', line 34
def transliterate(system_code, string, maps={}, compiler: Interscript::Interpreter)
load(system_code, maps, compiler: compiler).(string)
end
|
.transliterate_each(system_code, string, maps = {}, &block) ⇒ Object
Gives each possible value of the transliteration.
40
41
42
|
# File 'lib/interscript.rb', line 40
def transliterate_each(system_code, string, maps={}, &block)
load(system_code, maps).(string, each: true, &block)
end
|
.transliterate_file(system_code, input_file, output_file, maps = {}) ⇒ Object
44
45
46
47
48
49
50
51
52
53
54
|
# File 'lib/interscript.rb', line 44
def transliterate_file(system_code, input_file, output_file, maps={})
input = File.read(input_file)
output = transliterate(system_code, input, maps)
File.open(output_file, 'w') do |f|
f.puts(output)
end
puts "Output written to: #{output_file}"
output_file
end
|