Module: Interscript

Defined in:
lib/interscript.rb,
lib/interscript/command.rb,
lib/interscript/mapping.rb,
lib/interscript/version.rb

Overview

Transliteration

Defined Under Namespace

Classes: Command, InvalidSystemError, Mapping

Constant Summary collapse

VERSION =
"0.1.3"

Class Method Summary collapse

Class Method Details

.external_process(process_name, string) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
# File 'lib/interscript.rb', line 34

def external_process(process_name, string)
  import_python_modules
  case process_name
    when 'sequitur.pythainlp_lexicon'
      return g2pwrapper.transliterate('pythainlp_lexicon', string)
    when 'sequitur.wiktionary_phonemic'
      return g2pwrapper.transliterate('wiktionary_phonemic', string)
  else
    puts "Invalid Process"
  end
end

.import_python_modulesObject



24
25
26
27
28
29
30
31
32
# File 'lib/interscript.rb', line 24

def import_python_modules
  begin
    pyimport :g2pwrapper
  rescue
    pyimport :sys
    sys.path.append(root_path.to_s+"/lib/")
    pyimport :g2pwrapper
  end
end

.root_pathObject



10
11
12
# File 'lib/interscript.rb', line 10

def root_path
  @root_path ||= Pathname.new(File.dirname(__dir__))
end

.transliterate(system_code, string, maps = {}) ⇒ Object



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
135
136
137
138
# File 'lib/interscript.rb', line 46

def transliterate(system_code, string, maps={})
  if (!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&.sort_by { |k, _v| k.size }&.reverse&.to_h
  # dictmap = mapping.dictionary&.sort_by { |k, _v| k.size }&.reverse&.to_h
  charmap = mapping.characters_hash
  dictmap = mapping.dictionary_hash
  trie = mapping.dictionary_trie

  # Segmentation
  string = external_process(mapping.segmentation, string) if mapping.segmentation

  # Transliteration/Transcription
  string = external_process(mapping.transcription, string) if mapping.transcription

  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[pos..pos+m-1] = 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|
    output.gsub!(/#{r['pattern']}/, r['result'])
  end

  charmap.each do |k, v|
    while (match = output&.match(/#{k}/))
      pos = match.offset(0).first
      result = !downcase && up_case_around?(output, pos) ? v.upcase : v
      result = result[0] if result.is_a?(Array) # if more than one, choose the first one
      output[pos, match[0].size] = add_separator(separator, pos, result)
    end
  end

  mapping.postrules.each do |r|
    output.gsub!(/#{r['pattern']}/, r['result'])
  end

  if output
    output.sub!(/^(.)/,  &:upcase) if title_case
    if word_separator != ''
      output.gsub!(/#{word_separator}#{separator}/,word_separator)
      output.gsub!(/#{word_separator}(.)/, &:upcase) if title_case
    end
  end

  output ? output.unicode_normalize : output
end

.transliterate_file(system_code, input_file, output_file, maps) ⇒ Object



14
15
16
17
18
19
20
21
22
# File 'lib/interscript.rb', line 14

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}"
end