-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtranscriber.rb
130 lines (107 loc) · 2.9 KB
/
transcriber.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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
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
# encoding: UTF-8
require 'mini_magick'
require 'tmpdir'
require 'securerandom'
include MiniMagick
module Transcriber
GLYPH_DIR = 'glyphs'
# Maps a phoneme to its appropriate glyph
CONSONANT_MAP = {
'm'=>'m',
'p'=>'m',
'b'=>'b',
'g'=>'g',
'd'=>'d',
'ɖ'=>'ɖ',
'l'=>'l',
't'=>'l',
'ɣ'=>'k',
'k'=>'k',
'ʈ'=>'ʈ',
'ɭ'=>'ʈ'
}
# String containing all vowels
VOWELS = 'aioue'
# String containing all consonants
CONSONANTS = (CONSONANT_MAP.keys + CONSONANT_MAP.values).uniq.join
def morphemes str
##
# Pulls morphemes (based on the CVC rule) from the given string
re = Regexp.new "([#{CONSONANTS}])?([#{VOWELS}]+)([#{CONSONANTS}])?"
str.scan re
end
def make_char_glyph morpheme, canvas = nil
##
# Creates a glyph for the given morpheme.
# If a canvas is specified, increases the width of the canvas as needed
# and paints the image onto it
# Returns an instance of Image
consonant = CONSONANT_MAP[morpheme[0]]
vowels = morpheme[1].split('')
consonant2 = CONSONANT_MAP[morpheme[2]]
dir = GLYPH_DIR + File::SEPARATOR
v1img = Image.open(dir + vowels[0] + '.png', 'png')
v2img = nil
c2img = nil
if(consonant == nil)
# assume this is a phoneme of the form VC
image = v1img
else
image = Image.open(dir + consonant + '.png', 'png')
image = image.composite(v1img) # append vowel
end
if(vowels[1] != nil)
v2img = Image.open(dir + vowels[1] + '.png')
# cut me in two
w = v2img['width']
h = v2img['height']
v2img.crop("#{w}x#{h}+#{w/2}+0")
# append second vowel
image = image.composite(v2img) do |c|
c.gravity 'NorthEast'
end
end
if consonant2 != nil
c2img = Image.open(dir + consonant2 + '.png')
c2img.flip
image = image.composite(c2img)
end
if canvas != nil
path = Dir.tmpdir + File::SEPARATOR + SecureRandom.hex + 'glyph.png'
# TODO: Could not easily use +append function; will need to research more
`convert #{canvas.path} #{image.path} +append #{path}`
image = Image.open(path)
end
return image
end
def valid_word word
word.each_char do |c|
if CONSONANT_MAP[c] == nil && VOWELS.index(c) == nil
puts "Found invalid phoneme: #{c}"
return false
end
end
true
end
def transcribe word, output
##
# Given a word, written in the IPA, writes to the specified output file
# return: boolean - whether or not it was successful
if not valid_word(word)
return false
end
glyph = nil
morphemes(word).each{|morpheme|
glyph = make_char_glyph(morpheme, glyph)
}
if glyph == nil
return false
end
glyph.write output
true
end
module_function :transcribe
module_function :morphemes
module_function :make_char_glyph
module_function :valid_word
end