Module: Interscript::Fs

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

Instance Method Summary collapse

Instance Method Details

#aliases(refresh: false) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/interscript/fs.rb', line 62

def aliases (refresh: false)
  file = root_path.join("./aliases.json").to_s
  if !refresh && File.exist?(file)
    JSON.load(File.read(file))
  elsif !refresh && @aliases
    @aliases
  else
    @aliases = {}
    Dir[root_path.join('./maps/*.yaml').to_s].each do |yaml_file|
      org_name = File.basename(yaml_file, ".yaml")
      map = YAML.load_file(yaml_file)
      (map["alias"] || {}).each do |k,v|
        @aliases[v["code"]] = org_name
      end
    end

    # Try to save it to a file, but not force it.
    File.write("aliases.json", JSON.dump(@aliases)) rescue nil

    @aliases
  end
end

#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