Class: Wylie::Converter

Inherits:
Object
  • Object
show all
Defined in:
lib/wylie/converter.rb

Instance Method Summary collapse

Constructor Details

#initialize(debug = false) ⇒ Converter

Returns a new instance of Converter.



3
4
5
6
7
8
9
10
11
12
# File 'lib/wylie/converter.rb', line 3

def initialize(debug = false)
  combined_characters = CONSONANTS.merge(VOWELS).merge(SANSKRIT_VOWELS).merge(FINAL).merge(OTHER)

  @sorted_characters = combined_characters.sort_by do |key, value|
    key.size
  end.reverse.to_h

  @vowels = VOWELS.merge(SANSKRIT_VOWELS)
  @debug = debug
end

Instance Method Details

#is_foot_letter?(char) ⇒ Boolean

Returns:

  • (Boolean)


95
96
97
# File 'lib/wylie/converter.rb', line 95

def is_foot_letter?(char)
  ["y", "r", "l"].include?(char)
end

#is_head_letter?(char) ⇒ Boolean

Returns:

  • (Boolean)


91
92
93
# File 'lib/wylie/converter.rb', line 91

def is_head_letter?(char)
  ["s", "r", "l"].include?(char)
end

#is_new_stack?Boolean

Returns:

  • (Boolean)


79
80
81
# File 'lib/wylie/converter.rb', line 79

def is_new_stack?
  @new_stack
end

#is_preceeded_by_plus?Boolean

Returns:

  • (Boolean)


83
84
85
# File 'lib/wylie/converter.rb', line 83

def is_preceeded_by_plus?
  @preceeded_by_plus
end

#is_prefix?(char) ⇒ Boolean

Returns:

  • (Boolean)


99
100
101
# File 'lib/wylie/converter.rb', line 99

def is_prefix?(char)
  ["g", "d", "b", "m", "'"].include?(char)
end

#is_terminating_character?(char) ⇒ Boolean

Returns:

  • (Boolean)


103
104
105
# File 'lib/wylie/converter.rb', line 103

def is_terminating_character?(char)
  ["g", "/"].include?(char)
end

#is_vowel?(char) ⇒ Boolean

Returns:

  • (Boolean)


87
88
89
# File 'lib/wylie/converter.rb', line 87

def is_vowel?(char)
  @vowels.has_key?(char)
end

#parse_syllable(syl) ⇒ Object



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
# File 'lib/wylie/converter.rb', line 107

def parse_syllable(syl)
  tib = syl
  x = true
  tib_string = []
  @y = false

  while x == true

    @sorted_characters.each do |key, value|
      @y = true
      if tib.start_with?(key)
        tib_string << key
        tib.slice!(0..key.size-1)
        @y = false
      end
      break if @y == false
    end

    # CHECKS FOR A + SYMBOL
    if tib.match(/^[\+]/)
      tib_string << "+"
      tib.sub!(/\+/, "")
      @y = false
    end

    x = false if @y == true
  end
  tib_string
end

#show_unicodeObject



185
186
187
188
189
190
191
192
# File 'lib/wylie/converter.rb', line 185

def show_unicode
  pp CONSONANTS
  pp VOWELS
  pp SANSKRIT_VOWELS
  pp COMPLEX_VOWELS
  pp FINAL
  pp OTHER
end

#starts_with?(prefix) ⇒ Boolean

Returns:

  • (Boolean)


137
138
139
140
# File 'lib/wylie/converter.rb', line 137

def starts_with?(prefix)
  prefix = prefix.to_s
  self[0, prefix.length] == prefix
end

#tibetan(wylie) ⇒ Object



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

def tibetan(wylie)
  wylie_syllables = wylie.split(" ")
  tibetan_text = wylie_syllables.map do |syl|
    tibetan_syllable(syl)
  end
  is_terminating_character?(wylie[-1]) ? ending = "" : ending = ""
  tibetan_text.join(@sorted_characters[" "][0].join) + ending
end

#tibetan_roles(tokens) ⇒ Object



142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/wylie/converter.rb', line 142

def tibetan_roles(tokens)
  tib_roles = []

  tokens.each_with_index do |token, index|

    next_token = tokens[index+1]
    next_next_token = tokens[index+2]
    previous_token = tokens[index-1]
    previous_role = tib_roles[index-1]

    if index == 0
      if is_vowel?(token)
        tib_roles << "vowel"
      elsif is_vowel?(next_token)
        tib_roles << "root"
      elsif is_foot_letter?(next_token) && is_vowel?(next_next_token)
        tib_roles << "root"
      elsif is_prefix?(token)
        tib_roles << "pre"
      elsif is_head_letter?(token)
        tib_roles << "head"
      else
        tib_roles << "root"
      end
    else
      if is_vowel?(token)
        tib_roles << "vowel"
      elsif is_vowel?(previous_token) || previous_role == "suf"
        tib_roles << "suf"
      elsif previous_role.include?("root") && is_foot_letter?(token)
        tib_roles << "foot"
      elsif previous_role == "head"
        tib_roles << "root_sub"
      elsif previous_role == "pre" && is_vowel?(next_token)
        tib_roles << "root"
      else
        tib_roles << "head"
      end
    end
  end
  tib_roles
end

#tibetan_syllable(syl) ⇒ Object



23
24
25
26
# File 'lib/wylie/converter.rb', line 23

def tibetan_syllable(syl)
  wylie_tokens = parse_syllable(syl)
  transcribe_wylie(wylie_tokens)
end

#transcribe_wylie(tokens) ⇒ Object



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
# File 'lib/wylie/converter.rb', line 28

def transcribe_wylie(tokens)

  @new_stack = true
  @preceeded_by_plus = false

  tib_roles = tibetan_roles(tokens)
  tib_tokens = []

  pp tokens if @debug
  pp tib_roles if @debug

  tokens.each_with_index do |token, index|

    if token == "+"
      @preceeded_by_plus = true
      next
    end

    subjoined_characters = ["root_sub", "foot"]

    if VOWELS.include?(token)
      if index == 0
        tib_tokens << @sorted_characters["a"][0].join
      end

      if token == "a"
        next
      end
      @new_stack = true
    elsif subjoined_characters.include?(tib_roles[index])
      @new_stack = false
    elsif @preceeded_by_plus
      @new_stack = false
    else
      @new_stack = true
    end

    @preceeded_by_plus = false

    if is_new_stack?
      tib_tokens << @sorted_characters[token][0].join
    else
      tib_tokens << @sorted_characters[token][1].join
    end

  end

  tib_tokens.flatten.join

end