Module: Fonemas

Defined in:
lib/fonemas.rb,
lib/fonemas/version.rb

Constant Summary collapse

VERSION =
"0.2.4"

Class Method Summary collapse

Class Method Details

.anormalize(t) ⇒ Object



460
461
462
463
464
465
466
467
468
469
470
471
# File 'lib/fonemas.rb', line 460

def self.anormalize(t)
  #puts "pre normalize: #{t}"
  if t.class.name == 'Array'
    for i in t
      anormalize(i)
    end
  else
    #puts "found #{t}"
    @output << t
  end

end

.clean(text) ⇒ Object



12
13
14
15
16
17
18
# File 'lib/fonemas.rb', line 12

def self.clean(text)
  s = text.gsub(/,/,' ')
  s = s.gsub(/\s+/,' ')
  s = s.chomp.strip
  s = downcase(s)
  return s
end

.downcase(text) ⇒ Object



20
21
22
# File 'lib/fonemas.rb', line 20

def self.downcase(text)
  UnicodeUtils.downcase(text)
end

.entreVocales(word, i) ⇒ Object



111
112
113
114
115
116
117
# File 'lib/fonemas.rb', line 111

def self.entreVocales(word,i)
  if i == 0 or word.size - 1 == i
    return false
  else
    return (isVocal(word,i-1) and isVocal(word,i+1))
  end
end

.entreVocalyConsonante(word, i) ⇒ Object



119
120
121
122
# File 'lib/fonemas.rb', line 119

def self.entreVocalyConsonante(word,i)
  return ((isVocal(word,i-1) and !isVocal(word,i+1)) or (isVocal(word,i+1) and !isVocal(word,i-1)))

end

.fonemaLetra(letra) ⇒ Object



160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/fonemas.rb', line 160

def self.fonemaLetra(letra)
  case letra
    when 'a' then ['aa']
    when 'b' then ['b ee']
    when 'c' then ['s ee']
    when 'd' then ['d ee']
    when 'e' then ['ee']
    when 'f' then ['ee f ee']
    when 'g' then ['g ee']
    when 'h' then ['aa ch e']
    when 'i' then ['ii']
    when 'j' then ['j oo t a']
    when 'k' then ['k aa']
    when 'l' then ['ee l e']
    when 'm' then ['ee m e']
    when 'n' then ['ee n e']
    when 'ñ' then ['ee nh e']
    when 'o' then ['oo']
    when 'p' then ['p ee']
    when 'q' then ['c uu']
    when 'r' then ['ee rr ee','ee r ee']
    when 's' then ['ee s e']
    when 't' then ['t ee']
    when 'u' then ['uu']
    when 'v' then ['b ee','uu b e']
    when 'w' then ['d o b l e b ee','d o b l e uu v e']
    when 'x' then ['ee k i s']
    when 'y' then ['ll ee']
    when 'z' then ['z ee t a']
  end
end

.fonemas(word) ⇒ Object



193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
# File 'lib/fonemas.rb', line 193

def self.fonemas(word)
  if word.size == 1
    return fonemaLetra(word)
  end
  if word.include?('_')
    output = []
    for a in word.split('_')
      if a.size > 0
        output << Fonemas.fonemas(a)
      end
    end
    return [output.join(" ")]
  end
  word = separar(word)
  fonema = []
  for i in 0..(word.length-1)
    letra = word[i]
    case letra
      when 'á' then
        fonema << 'aa'
      when 'é' then
        fonema << 'ee'
      when 'í' then
        fonema << 'ii'
      when 'ó' then
        fonema << 'oo'
      when 'ú' then
        fonema << 'uu'
      when 'a' then
        if isTonica(word,i)
          fonema << 'aa'
        else
          fonema << 'a'
        end
      when 'b','v' then
        if word.size() == 1
          return Fonemas.fonemas('be')
        elsif isVocal(word,i-1) and (word[i+1] == 'b' or word[i+1] == 'v')
          fonema << ['bb','']
        elsif i == 0 and isVocal(word,i+1)
          if word[i+1] == 'u' and isDiptongo(word,i+1,i+2)
            fonema << ['bb','b','g']
          else
            fonema << ['bb','b']
          end
        elsif word[i+1] == 'u' and isDiptongo(word,i+1,i+2)
          if entreVocales(word,i)
            fonema << ['b','g','']
          else
            fonema << ['bb','g']
          end
        elsif isFricativa(word,i-1)
          fonema << 'b'
        elsif isFinal(word,i)
          fonema << 'b'
        elsif entreVocales(word,i)
          fonema << ['b','']
        else
          fonema << 'bb'
        end
      when 'c' then
        if word.size() == 1
           fonema << 's'
           fonema << 'ee'
        elsif word[i+1] == 'e' or word[i+1] == 'i'
          fonema << 's'
        else
          fonema << 'k'
        end
      when 'ch' then
        if entreVocales(word,i)
          fonema << ['ch','sh','tch','j']
        else
          fonema << ['ch','sh','tch']
        end
      when 'd' then
        if word.size == 1
          fonema << 'd'
          fonema << 'e'
        elsif i == 0 and isVocal(word,i+1)
          fonema << ['dd','d']
        elsif entreVocales(word,i) or i == word.size-1
          fonema << ['d','']
        elsif entreVocalyConsonante(word,i)
          fonema << ['dd','d']
        else
          fonema << 'd'
        end
      when 'e' then
        if isTonica(word,i)
          fonema << 'ee'
        else
          fonema << 'e'
        end
      when 'f' then
        fonema << 'f'
      when 'g' then
        if word[i+1] == 'u' and i == 0
          #nada

        elsif word[i+1] == 'e' or word[i+1] == 'i'
          fonema << 'j'
        else
          if !entreVocales(word,i) and word[i-1] != 'n'
            fonema << 'gg'
          else
            fonema << 'g'
          end
        end
      when 'h' then
        if word.size == 1
          return Fonemas.fonemas('hache')
        elsif word[i+1] == 'u' and isDiptongo(word,i+1,i+2)
          fonema << ['','g']
        end
        #nada
      when 'i' then
        if isTonica(word,i)
          fonema << 'ii'
        else
          fonema << 'i'
        end
      when 'j' then
        fonema << 'j'
      when 'k' then
        fonema << 'k'
      when 'l' then
        fonema << 'l'
      when 'll' then
        fonema << ['ll','lli','i']
      when 'm' then
        fonema << 'm'
      when 'n' then
        fonema << 'n'
      when 'ñ'  then
        fonema << 'nh'
      when 'o' then
        if isTonica(word,i)
          fonema << 'oo'
        else
          fonema << 'o'
        end
      when 'p' then
        fonema << 'p'
      when 'q' then
        fonema << 'k'
      when 'r' then
        if i == 0
          fonema << 'rr'
        else
          fonema << 'r'
        end
      when 'rr' then
        fonema << 'rr'
      when 's' then
        if word[i-1] == 'r' or word[i-1] == 'd' or i == word.size-1
          fonema << ['s','','h']
        elsif entreVocalyConsonante(word,i)
          fonema << ['s','h']
        elsif word[i-1] == 'b' and word[i+1] == 't'
          fonema << ['s','h']
        elsif word[i-1] == 'b'
          fonema << ['s','']
        else
          fonema << 's'
        end
      when 't' then
        fonema << 't'
      when 'ü' then
        fonema << 'u'
      when 'u' then
        if word[i-1] == 'g' and i == 1 and isTonica(word,i+1)
          fonema << ['gu']
        elsif word[i-1] == 'q' or word[i-1] == 'g'
          #nada

        elsif isTonica(word,i)
            fonema << 'uu'
        else
            fonema << 'u'
        end
      when 'w' then
        if i == 0
          fonema << ['b','bb']
        elsif word[i-1] == 'o'
          fonema << 'u'
        elsif word[i+1] == 'i'
          fonema << 'u'
        else #if entreVocales(word,i)
          fonema << 'gu'
#          else
#            fonema << 'Gu'
        end
      when 'x' then
        fonema << ['ks','k','h']
      when 'y' then
        if i == word.size - 1
          fonema << 'i'
        else
          fonema << ['ll','lli','i']
        end
      when 'z' then
        if i == word.size - 1
          fonema << ['s','h','']
        else
          fonema << 's'
        end

      else
        fonema << letra
    end



  end
  #puts "pre: #{fonema}"
  t =  normalize(generateFonemas(fonema))
  #puts "out: #{t}"

  #self.checkFonemas(t)

  return t
end

.generateFonemas(fonema, i = 0, current = []) ⇒ Object

def self.checkFonemas(p)

#un ultimo chequeo de seguridad
for pronunciacion in p
  for fonema in pronunciacion.split(" ")
    raise "fonema invalido" unless lista_de_fonemas.include? fonema
  end
end

end



427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
# File 'lib/fonemas.rb', line 427

def self.generateFonemas(fonema,i=0,current=[])
    if i == fonema.length
      return current.join(' ')
    end

    c = fonema[i]
    if c.class.name == 'Array'
      output = []
      for j in c
        if j == ''
          output << generateFonemas(fonema,i+1,current)
        else
          output << generateFonemas(fonema,i+1,current + [j])
        end
      end
      return output
    else
      if c == ''
        return generateFonemas(fonema,i+1,current)
      else
        return generateFonemas(fonema,i+1,current + [c])
      end
    end

end

.isDiptongo(word, first, second) ⇒ Object



129
130
131
132
133
134
135
136
# File 'lib/fonemas.rb', line 129

def self.isDiptongo(word,first,second)
  f = word[first]
  s = word[second]
  abiertas = %w(a e o)
  cerradas = %w(i u)
  return ((abiertas.include? f and cerradas.include? s) or (abiertas.include? s and cerradas.include? f) or (cerradas.include? f and cerradas.include? s))

end

.isFinal(word, i) ⇒ Object



102
103
104
# File 'lib/fonemas.rb', line 102

def self.isFinal(word,i)
  return word.size == i-1
end

.isFricativa(word, i) ⇒ Object



106
107
108
109
# File 'lib/fonemas.rb', line 106

def self.isFricativa(word,i)
  fricativas = %w(f s c z j ll y g b w b v w s m b x d)
  return fricativas.include? word[i]
end

.isTonica(word, i) ⇒ Object



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
# File 'lib/fonemas.rb', line 36

def self.isTonica(word,i)
  #falta considerar las palabras que poseen acento pero no tilde
  return true if word.size == 1
  tildes = %w(á é í ó ú)
  w = word.join
  if tildes.include? word[i]
    return true
  else
    es = Text::Hyphen.new(:language => "es", :left => 0, :right => 1)
    p = es.hyphenate(w)
    #puts es.visualize(w)
    hh = es.visualize(w).split("-")
    #puts hh.size
    if w =~ /[áéíóú]/
      #acento ya existe en otra silaba
      return false
    else
      #puts es.visualize(w)
      if hh.size == 1
        if lastVocal(w,w.size-1) == word[i]
          return true
        else
          return false
        end
        #monosilabos
      elsif hh.size == 2
          #agudas, se acentuan en n,s o vocal
          #puts "#{word[i]} #{i}<#{p[0]} - #{lastVocal(w,p[0])}"
          if w =~ /[nsaeiou]$/
            #termina en n s y vocal y no tiene tilde
            #por lo tanto es grave
          #  puts "#{lastVocal(w,p[0])} == #{word[i]} #{word[i].class.name}"
            if i < p[0] and lastVocal(w,p[0]) == word[i]
              return true
            else
              return false
            end
          else
            if i < p[0]
              return false
            else
              return lastVocal(w,w.size) == word[i]
            end
          end
      elsif hh.size >= 3
        #puts hh.join("-")
        if i > p[p.size-1]
          if w =~ /[nsaeiou]$/
            return false
          else
            return true
          end
        elsif i > p[p.size-2] and i <= p[p.size-1] and w =~ /[nsaeiou]$/
          return true
        else
          return false
        end
      end
    end



    return false
  end
end

.isVocal(word, i) ⇒ Object



124
125
126
127
# File 'lib/fonemas.rb', line 124

def self.isVocal(word,i)
  vocales = %w(a e i o u á é í ó ú)
  return vocales.include? word[i]
end

.lastVocal(word, from) ⇒ Object



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

def self.lastVocal(word,from)
  #puts "last vocal for #{word} from #{from}"
  for i in 1..from
   # puts i
    #puts word[from-i]
    if word[from-i] =~ /[aeiou]/
      return word[from-i]
    end
  end
  return false
end

.lista_de_fonemasObject



473
474
475
476
477
478
# File 'lib/fonemas.rb', line 473

def self.lista_de_fonemas
  phonelist = ['SIL']
  phonelist += %w{a e i o u aa ee ii oo uu}
  phonelist += %w{bb b d e f g h i j k l m n o p q rr r s t u w ks k h gu ch tch sh dd gg ll lli nh}
  phonelist.uniq
end

.normalize(t) ⇒ Object



454
455
456
457
458
# File 'lib/fonemas.rb', line 454

def self.normalize(t)
  @output = []
  anormalize(t)
  return @output
end

.separar(word) ⇒ Object



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/fonemas.rb', line 138

def self.separar(word)
  word = downcase(word)
  output = []
  i = 0
  while(i < word.length)
    if word[i] == 'c' and word[i+1] == 'h'
      output << "ch"
      i+=1
    elsif word[i] == 'l' and word[i+1] == 'l'
      output << 'll'
      i+=1
    elsif word[i] == 'r' and word[i+1] == 'r'
      output << 'rr'
      i+=1
    else
      output << word[i]
    end
    i +=1
  end
  return output
end

.versionObject



8
9
10
# File 'lib/fonemas.rb', line 8

def self.version
  VERSION
end