Module: Interscript
- Defined in:
- lib/interscript.rb,
lib/interscript/fs.rb,
lib/interscript/opal.rb,
lib/interscript/command.rb,
lib/interscript/mapping.rb,
lib/interscript/version.rb,
lib/interscript/opal/entrypoint.rb
Overview
Transliteration
Defined Under Namespace
Modules: Fs, Opal Classes: Command, ExternalProcessNotRecognizedError, ExternalProcessUnavailableError, InvalidSystemError, Mapping
Constant Summary collapse
- VERSION =
"0.1.9"
Class Method Summary collapse
- .map_resolve(map) ⇒ Object
- .on_load(&block) ⇒ Object
-
.on_load_maps(arg, &block) ⇒ Object
on_load + load_maps.
- .transliterate(system_code, string, maps = {}) ⇒ Object
Methods included from Opal
aliases, external_processing, load_map_json, load_maps, map_exist?, map_loaded?, mkregexp, sub_replace
Methods included from Fs
aliases, external_process, external_processing, import_python_modules, root_path, sub_replace, transliterate_file
Class Method Details
.map_resolve(map) ⇒ Object
136 137 138 139 140 |
# File 'lib/interscript.rb', line 136 def map_resolve(map) map = aliases[map] if aliases.key? map raise ArgumentError, "Map #{map} doesn't exist" unless map_exist? map map end |
.on_load(&block) ⇒ Object
6 7 8 |
# File 'lib/interscript/opal/entrypoint.rb', line 6 def self.on_load(&block) WebAssembly.wait_for("onigmo/onigmo-wasm", &block) end |
.on_load_maps(arg, &block) ⇒ Object
on_load + load_maps
11 12 13 14 15 |
# File 'lib/interscript/opal/entrypoint.rb', line 11 def self.on_load_maps(arg, &block) self.on_load.JS.then do self.load_maps(arg, &block) end end |
.transliterate(system_code, string, maps = {}) ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 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 |
# File 'lib/interscript.rb', line 22 def transliterate(system_code, string, maps={}) system_code = map_resolve(system_code) unless maps.has_key? system_code maps[system_code] = Interscript::Mapping.for(system_code) end # mapping = Interscript::Mapping.for(system_code) mapping = maps[system_code] # First, apply chained transliteration as specified in the list `chain` chain = mapping.chain.dup while chain.length > 0 string = transliterate(chain.shift, string, maps) end # Then, apply the rest of the map separator = mapping.character_separator || "" word_separator = mapping.word_separator || "" title_case = mapping.title_case downcase = mapping.downcase charmap = mapping.characters_hash dictmap = mapping.dictionary_hash trie = mapping.dictionary_trie string = external_processing(mapping, string) pos = 0 while pos < string.to_s.size m = 0 wordmatch = "" # Using Trie, find the longest matching substring while (pos + m < string.to_s.size) && (trie.partial_word?string[pos..pos+m]) wordmatch = string[pos..pos+m] if trie.word?string[pos..pos+m] m += 1 end m = wordmatch.length if m > 0 repl = dictmap[string[pos..pos+m-1]] string = sub_replace(string, pos, m, repl) pos += repl.length else pos += 1 end end output = string.clone offsets = Array.new string.to_s.size, 1 # mapping.rules.each do |r| # string.to_s.scan(/#{r['pattern']}/) do |matches| # match = Regexp.last_match # pos = match.offset(0).first # result = r['result'].clone # matches.each.with_index { |v, i| result.sub!(/\\#{i + 1}/, v) } if matches.is_a? Array # result.upcase! if up_case_around?(string, pos) # output[offsets[0...pos].sum, match[0].size] = result # offsets[pos] += result.size - match[0].size # end # end mapping.rules.each do |r| next unless output re = mkregexp(r["pattern"]) output = output.gsub(re, r["result"]) end charmap.each do |k, v| re = mkregexp(k) while (match = output&.match(re)) pos = match.offset(0).first result = !downcase && up_case_around?(output, pos) ? v.upcase : v # if more than one, choose the first one result = result[0] if result.is_a?(Array) output = sub_replace( output, pos, match[0].size, add_separator(separator, pos, result) ) end end mapping.postrules.each do |r| next unless output re = mkregexp(r["pattern"]) output = if r["result"] == "upcase" output.gsub(re, &:upcase) else output.gsub(re, r["result"]) end end return unless output re = mkregexp('^(.)') output = output.gsub(re, &:upcase) if title_case if word_separator != '' re = mkregexp("#{word_separator}#{separator}") output = output.gsub(re, word_separator) if title_case re = mkregexp("#{word_separator}(.)") output = output.gsub(re, &:upcase) end end output.unicode_normalize end |