Module: Interscript::Fs

Included in:
Interscript
Defined in:
lib/interscript/fs.rb

Constant Summary collapse

ALPHA_REGEXP =
'[[:alpha:]]'

Instance Method Summary collapse

Instance Method Details

#external_process(process_name, string) ⇒ Object


36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/interscript/fs.rb', line 36

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
    raise ExternalProcessNotRecognizedError.new
  end

rescue
  raise ExternalProcessUnavailableError.new
end

#external_processing(mapping, string) ⇒ Object


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

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

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

  string
end

#import_python_modulesObject


26
27
28
29
30
31
32
33
34
# File 'lib/interscript/fs.rb', line 26

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/fs.rb', line 10

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

#sub_replace(string, pos, size, repl) ⇒ Object


5
6
7
8
# File 'lib/interscript/fs.rb', line 5

def sub_replace(string, pos, size, repl)
  string[pos..pos + size - 1] = repl
  string
end

#transliterate_file(system_code, input_file, output_file, maps = {}) ⇒ Object


14
15
16
17
18
19
20
21
22
23
24
# File 'lib/interscript/fs.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}"
  output_file
end