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, ExternalUtilError, Interpreter, MapLogicError, MapNotFoundError, Node, Stdlib, SystemConversionError, Visualize

Constant Summary collapse

VERSION =
"2.4.5"

Class Method Summary collapse

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.



67
68
69
70
71
# File 'lib/interscript.rb', line 67

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



173
174
175
176
177
178
179
180
181
182
183
184
# File 'lib/interscript.rb', line 173

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



36
37
38
# File 'lib/interscript.rb', line 36

def load(system_code, maps={}, compiler: Interscript::Interpreter)
  maps[[system_code, compiler.name]] ||= compiler.(system_code)
end

.load_pathObject



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

def load_path
  @load_path ||= ['.', *Interscript.map_locations]
end

.locate(map_name) ⇒ Object

Raises:



19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/interscript.rb', line 19

def locate map_name
  map_name = map_aliases[map_name] if map_aliases.include? map_name

  load_path.each do |i|
    # iml is an extension for a library, imp for a map
    ["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_aliasesObject



146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/interscript.rb', line 146

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_gemsObject



73
74
75
76
77
# File 'lib/interscript.rb', line 73

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_locationsObject



79
80
81
82
83
84
85
86
87
88
# File 'lib/interscript.rb', line 79

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



161
162
163
164
165
166
167
168
# File 'lib/interscript.rb', line 161

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



32
33
34
# File 'lib/interscript.rb', line 32

def parse(map_name)
  Interscript::DSL.parse(map_name)
end

.rababa_configsObject



96
97
98
99
100
101
102
# File 'lib/interscript.rb', line 96

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.

Raises:



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
138
139
140
141
142
143
144
# File 'lib/interscript.rb', line 106

def rababa_provision(model_name, model_uri)
  require 'fileutils'
  require 'open-uri'

  # We provision the environment in the following way:
  # First, we try the RABABA_DATA environment variable. If that's available,
  # we use it to store the Rababa data we need. Otherwise, we try the following
  # paths:

  possible_paths = [
    "/var/lib/rababa",
    "/usr/local/share/rababa",
    "/usr/share/rababa",
    File.join(Dir.home, ".local/share/rababa")
  ]

  # We find the first writable path

  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 ExternalUtilError, "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"

  # Redownload every hour
  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_locationsObject



90
91
92
93
94
# File 'lib/interscript.rb', line 90

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.



41
42
43
44
# File 'lib/interscript.rb', line 41

def transliterate(system_code, string, maps={}, compiler: Interscript::Interpreter)
  # The current best implementation is Interpreter
  load(system_code, maps, compiler: compiler).(string)
end

.transliterate_each(system_code, string, maps = {}, &block) ⇒ Object

Gives each possible value of the transliteration.



47
48
49
# File 'lib/interscript.rb', line 47

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



51
52
53
54
55
56
57
58
59
60
61
# File 'lib/interscript.rb', line 51

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