-
Notifications
You must be signed in to change notification settings - Fork 0
/
qc_ipa.rb
executable file
·194 lines (180 loc) · 4.71 KB
/
qc_ipa.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
131
132
133
134
135
136
137
138
139
140
141
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
184
185
186
187
188
189
190
191
192
193
194
#!/usr/bin/ruby -KuU
# encoding: utf-8
require 'optparse'
def normalize_qc(ipa)
raw_ipa = ipa.chomp.gsub(/\//, "")
raw_ipa.gsub(/ɛl$/, "al")
.gsub(/ti/, "t͡si")
.gsub(/ty/, "t͡sy")
.gsub(/di/, "d͡zi")
.gsub(/dy/, "d͡zy")
.gsub(/dj/, "d͡zj")
.gsub(/tj/, "t͡sj")
.gsub(/bl$/, "b")
.gsub(/bʁ$/, "b")
.gsub(/dʁ$/, "d")
.gsub(/fl$/, "f")
.gsub(/gm$/, "g")
.gsub(/gʁ$/, "g")
.gsub(/kʁ$/, "k")
.gsub(/([^s])kl$/, "\\1k")
.gsub(/kst$/, "ks")
.gsub(/kt$/, "k")
.gsub(/mn$/, "m")
.gsub(/pt$/, "p")
.gsub(/sk$/, "s")
.gsub(/skl$/, "s")
.gsub(/sm$/, "s")
.gsub(/st$/, "s")
.gsub(/stʁ$/, "s")
.gsub(/tm$/, "t")
.gsub(/tʁ$/, "t")
.gsub(/vʁ$/, "v")
.gsub(/ɛ̃/, "ẽĩ̯")
.gsub(/ɔ̃/, "õũ̯")
.gsub(/œ̃/, "œ̃˞")
.gsub(/ɔ/, "ɑɔ̯")
.gsub(/([^w])a$/, "\\1ɔ")
.gsub(/ɛ/, "a")
.gsub(/ɑ̃$/, "æ̃") # imprecise: only in open final syllables
.gsub(/ɑ̃(.)$/, "ãũ̯\\1") # imprecise: only in closed final syllables
.gsub(/ɑː/, "ɑʊ̯")
.gsub(/ɛː/, "aɪ̯")
# ɛ̃ː => ?
# ɑ̃ː => ?
# .gsub(/skl$/, "s / sk")
end
def affricate(ipa)
while ipa.match(/([abdfgɡhjklmnpʁsʃtvwz])i([abdfgɡhjklmnpʁsʃtvwz])/)
ipa.gsub!(/([abdfgɡhjklmnpʁsʃtvwz])i([abdfgɡhjklmnpʁsʃtvwz])/, "\\1ɪ\\2")
end
while ipa.match(/([abdfgɡhjklmnpʁsʃtvwz])y([abdfgɡhjklmnpʁsʃtvwz])/)
ipa.gsub!(/([abdfgɡhjklmnpʁsʃtvwz])y([abdfgɡhjklmnpʁsʃtvwz])/, "\\1ʏ\\2")
end
while ipa.match(/([abdfgɡhjklmnpʁsʃtvwz])u([abdfgɡhjklmnpʁsʃtvwz])/)
ipa.gsub!(/([abdfgɡhjklmnpʁsʃtvwz])u([abdfgɡhjklmnpʁsʃtvwz])/, "\\1ʊ\\2")
end
ipa
end
def manual_convert(word)
conversions = File.read("conversions.txt")
found = false
conversions.each_line do |line|
headword, ipa = line.chomp.split("\t")
if headword == word
found = true
puts line
end
end
found
end
def process_dict(fr)
fr.each_line do |line|
word,ipa = line.split("\t")
found = manual_convert(word)
if !found
ipa_split = ipa.split(", ")
collector = []
ipa_split.each do |i|
qc = normalize_qc(ipa)
qc = affricate(qc)
collector << qc
end
puts word + "\t/" + collector.join("/, /") + "/"
end
end
end
def check_word(word, dict)
db = File.read(dict)
found = false
db.each_line do |line|
headword, ipa = line.chomp.split("\t")
if headword == word
found = true
puts line
end
end
if found == false
abort(" Entry \"#{word}\" not found in dictionary file.")
end
end
def lookup_word(word, dict)
found = false
vocab = File.read("mots.txt")
vocab.each_line do |line|
headword, ipa = line.chomp.split("\t")
if headword == word
found = true
puts line
end
end
if found == false
found = manual_convert(word)
end
if found == false
db = File.read(dict)
db.each_line do |line|
headword, ipa = line.chomp.split("\t")
if headword == word
found = true
qc = normalize_qc(ipa)
qc = affricate(qc)
puts word + "\t/" + qc + "/"
end
end
end
if found == false
abort(" Entry \"#{word}\" not found.")
end
end
options = {}
OptionParser.new do |opts|
opts.banner = "Usage: qc_ipa.rb [options]"
opts.on('-c', '--check WORD', 'Check if word exists in standard French database (dictionary file must be specified with -d)') { |v| options[:check_word] = v }
opts.on('-d', '--dictionary FILE', 'Specify Standard French IPA dictionary file') { |v| options[:dictionary_file] = v }
opts.on('-i', '--ipa-convert IPA', 'Convert single word IPA') { |v| options[:ipa_convert] = v }
opts.on('-l', '--lookup WORD', 'Lookup / convert a single word (dictionary file must be specified with -d)') { |v| options[:lookup_word] = v }
opts.on('-p', '--process [DICTIONARY]', 'Convert entire IPA dictionary file') do |v|
options[:process_dict] = true
options[:dict] = v || ''
end
end.parse!
if options[:check_word]
word = options[:check_word]
if options[:dictionary_file]
dict = options[:dictionary_file]
check_word(word, dict)
else
abort(" Please specify a dictionary file.")
end
exit
end
if options[:lookup_word]
word = options[:lookup_word]
if options[:dictionary_file]
dict = options[:dictionary_file]
lookup_word(word, dict)
else
abort(" Please specify a dictionary file.")
end
exit
end
if options[:ipa_convert]
ipa = options[:ipa_convert]
qc = normalize_qc(ipa)
qc = affricate(qc)
puts qc
exit
end
if options[:process_dict]
dict = options[:dict]
if dict == ""
if options[:dictionary_file]
dict = options[:dictionary_file]
else
abort(" Please specify a dictionary file.")
end
end
process_dict(File.read(dict))
exit
end