-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathLatin-Syllable-n-Stress-Viewer.html
330 lines (260 loc) · 11.5 KB
/
Latin-Syllable-n-Stress-Viewer.html
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
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
<!DOCTYPE html>
<head>
<title>Latin Syllable & Stress Viewer V0.1</title>
</head>
<body>
<input id="latin-word" onkeyup="syllabify()">
<div id="latin-syllable">-</div>
<script>
/**
* Name: Latin Syllable & Stress Viewer
* Version: V0.1
* Author: Aschneekäppchen ([email protected])
* The original class Syllable, Word and Latin were written by:
* - Matthew Spencer
* - OSJ <[email protected]>,
* with some minor modifications.
* Difference compared to the original classes:
* - Supports macron
* - Supports stress
*/
var Syllable = function (text, word) {
this.text = text;
this.word = word;
this.toString = function () {
return this.text;
}
this.isLastInWord = function () {
return this.word.syllables[this.word.length - 1] == this;
}
};
var Word = function (syllables) {
this.syllables = syllables;
this.toString = function () {
var s = "";
for (var i = 0, end = this.syllables.length; i < end; i++)
s += this.syllables[i].text;
return s;
}
// make sure the syllables know who's who
for (var i = 0, end = this.syllables.length; i < end; i++)
this.syllables[i].word = this;
};
var Latin = function () {
this.name = "Latin";
this.syllableConnector = "-";
// fixme: should we include 'diphthongs' with accented vowels, e.g., áe?
// fixme: ui is only diphthong in the exceptional cases below (according to Wheelock's Latin)
this.diphthongs = ["ae", "au", "oe"];
// some words that are simply exceptions to standard syllabification rules!
var wordExceptions = new Object();
// ui combos pronounced as dipthongs
wordExceptions["huius"] = new Word(["hui", "us"]);
wordExceptions["cuius"] = new Word(["cui", "us"]);
wordExceptions["huic"] = new Word(["huic"]);
wordExceptions["cui"] = new Word(["cui"]);
wordExceptions["hui"] = new Word(["hui"]);
this.vowels = ['a', 'e', 'i', 'o', 'u',
'á', 'é', 'í', 'ó', 'ú',
'ā', 'ē', 'ī', 'ō', 'ū',
'æ', 'œ',
'ǽ', // no accented œ in unicode?
'y']; // y is treated as a vowel; not native to Latin but useful for words borrowed from Greek
this.muteConsonantsAndF = ['b', 'c', 'd', 'g', 'p', 't', 'f'];
this.liquidConsonants = ['l', 'r'];
};
Latin.prototype = {
// c must be lowercase!
isVowel: function (c) {
for (var i = 0, end = this.vowels.length; i < end; i++)
if (this.vowels[i] == c)
return true;
return false;
},
/**
* f is not a mute consonant, but we lump it together for syllabification
* since it is syntactically treated the same way
*
* @param {String} c The character to test; must be lowercase
* @return {boolean} true if c is an f or a mute consonant
*/
isMuteConsonantOrF: function (c) {
for (var i = 0, end = this.muteConsonantsAndF.length; i < end; i++)
if (this.muteConsonantsAndF[i] == c)
return true;
return false;
},
/**
*
* @param {String} c The character to test; must be lowercase
* @return {boolean} true if c is a liquid consonant
*/
isLiquidConsonant: function (c) {
for (var i = 0, end = this.liquidConsonants.length; i < end; i++)
if (this.liquidConsonants[i] == c)
return true;
return false;
},
/**
*
* @param {String} s The string to test; must be lowercase
* @return {boolean} true if s is a diphthong
*/
isDiphthong: function (s) {
for (var i = 0, end = this.diphthongs.length; i < end; i++)
if (this.diphthongs[i] == s)
return true;
return false;
},
/**
* Rules for Latin syllabification (from Collins, "A Primer on Ecclesiastical Latin")
*
* Divisions occur when:
* 1. After open vowels (those not followed by a consonant) (e.g., "pi-us" and "De-us")
* 2. After vowels followed by a single consonant (e.g., "vi-ta" and "ho-ra")
* 3. After the first consonant when two or more consonants follow a vowel
* (e.g., "mis-sa", "minis-ter", and "san-ctus").
*
* Exceptions:
* 1. In compound words the consonants stay together (e.g., "de-scribo").
* 2. A mute consonant (b, c, d, g, p, t) or f followed by a liquid consonant (l, r)
* go with the succeeding vowel: "la-crima", "pa-tris"
*
* In addition to these rules, Wheelock's Latin provides this sound exception:
* - Also counted as single consonants are qu and the aspirates ch, ph,
* th, which should never be separated in syllabification:
* architectus, ar-chi-tec-tus; loquacem, lo-qua-cem.
*
* @param {String} The word to divide into syllables
* @returns {Word} The Word object with parsed syllables
*/
syllabifyWord: function (word) {
var syllables = [];
var haveCompleteSyllable = false;
var previousWasVowel = false;
var workingString = word.toLowerCase();
var startSyllable = 0;
var c, lookahead, haveLookahead;
// a helper function to create syllables
var makeSyllable = function (length) {
if (haveCompleteSyllable) {
syllables.push(new Syllable(word.substr(startSyllable, length)));
startSyllable += length;
}
haveCompleteSyllable = false;
}
c = ' '; // used just for a silly breakpoint in firebug!
for (var i = 0, wordLength = workingString.length; i < wordLength; i++) {
c = workingString[i];
// get our lookahead in case we need them...
lookahead = '*';
haveLookahead = (i + 1) < wordLength;
if (haveLookahead)
lookahead = workingString[i + 1];
var cIsVowel = this.isVowel(c);
// i is a special case for a vowel. when i is at the beginning
// of the word (Iesu) or i is between vowels (alleluia),
// then the i is treated as a consonant (y)
if (c == 'i') {
if (i == 0 && haveLookahead && this.isVowel(lookahead))
cIsVowel = false;
else if (previousWasVowel && haveLookahead && this.isVowel(lookahead)) {
cIsVowel = false;
}
}
if (c == '-') {
// a hyphen forces a syllable break, which effectively resets
// the logic...
haveCompleteSyllable = true;
previousWasVowel = false;
makeSyllable(i - startSyllable);
startSyllable++;
} else if (cIsVowel) {
// once we get a vowel, we have a complete syllable
haveCompleteSyllable = true;
if (previousWasVowel && !this.isDiphthong(workingString[i - 1] + "" + c)) {
makeSyllable(i - startSyllable);
haveCompleteSyllable = true;
}
previousWasVowel = true;
} else if (haveLookahead) {
if ((c == 'q' && lookahead == 'u') ||
(lookahead == 'h' && (c == 'c' || c == 'p' || c == 't'))) {
// handle wheelock's exceptions for qu, ch, ph and th
makeSyllable(i - startSyllable);
i++; // skip over the 'h' or 'u'
} else if (previousWasVowel && this.isVowel(lookahead)) {
// handle division rule 2
makeSyllable(i - startSyllable);
} else if (this.isMuteConsonantOrF(c) && this.isLiquidConsonant(lookahead)) {
// handle exception 2
makeSyllable(i - startSyllable);
} else if (haveCompleteSyllable) {
// handle division rule 3
makeSyllable(i + 1 - startSyllable);
}
previousWasVowel = false;
}
}
// if we have a complete syllable, we can add it as a new one. Otherwise
// we tack the remaining characters onto the last syllable.
if (haveCompleteSyllable)
syllables.push(new Syllable(word.substr(startSyllable)));
else if (startSyllable > 0)
syllables[syllables.length - 1].text += word.substr(startSyllable);
return new Word(syllables);
},
/**
* @param {String} s the string to search
* @param {Number} startIndex The index at which to start searching for a vowel in the string
* @retuns a custom class with three properties: {found: (true/false) startIndex: (start index in s of vowel segment) length ()}
*/
findVowelSegment: function (s, startIndex) {
var i, end, index;
var workingString = s.toLowerCase();
// do we have a diphthongs?
for (i = 0, end = this.diphthongs.length; i < end; i++) {
var d = this.diphthongs[i];
index = workingString.indexOf(d, startIndex);
if (index >= 0)
return {found: true, startIndex: index, length: d.length};
}
// no diphthongs. Let's look for single vowels then...
for (i = 0, end = this.vowels.length; i < end; i++) {
index = workingString.indexOf(this.vowels[i], startIndex);
if (index >= 0)
return {found: true, startIndex: index, length: 1};
}
// no vowels sets found after startIndex!
return {found: false, startIndex: -1, length: -1};
}
};
var latin = new Latin();
function stressify(syllables) {
var length = syllables.length,
longVowels = ['ā', 'ē', 'ī', 'ō', 'ū'];
var isLong = false;
if (length > 1) {
syllables.reverse();
for (var i in longVowels) {
if (syllables[1].text.indexOf(longVowels[i]) !== -1) {
isLong = true;
break;
}
}
if (!latin.isVowel(syllables[1].text.substr(-1)) && !latin.isVowel(syllables[0][0])) isLong = true;
if (!isLong && length > 2) syllables[2].text = '<b>' + syllables[2].text + '</b>';
else if (isLong) syllables[1].text = '<b>' + syllables[1].text + '</b>';
syllables.reverse();
}
return syllables;
}
function syllabify() {
var words = stressify(latin.syllabifyWord(document.getElementById('latin-word').value).syllables);
var syllableDivider = "-";
var s = words.join(syllableDivider);
document.getElementById('latin-syllable').innerHTML = '/' + s + '/';
}
</script>
</body>
</html>