-
Notifications
You must be signed in to change notification settings - Fork 0
/
Javascript Spanish Accent
364 lines (325 loc) · 9.71 KB
/
Javascript Spanish Accent
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
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
<HTML>
<HEAD>
<TITLE>Placing the Spanish Accent</TITLE>
<SCRIPT LANGUAGE="JAVASCRIPT">
<!-- hide this from old browsers
var TRUE = 1;
var FALSE=-1;
function ClearForm(form){
form.InSeed.value ="";
}
function Accent(form) {
var word = "";
word= form.InSeed.value.toLowerCase();
if (invalidword(word)==TRUE)
alert("Sorry.. bad input");
else {
if(word.length== 1) alert("Monosunds do not have accents.");
else {
if(word.length== 2) {
if (word=="mi") mimsg();
if (word=="si") simsg();
if (word=="el") elmsg();
if (word!="mi" && word != "si" && word != "el")
alert("There should be no accent");
}
else {
if(word.length== 3)
alert("Monovowels have different rules (example: ma's.");
else process(word);
}
}
}
}
function Help()
{
alert(
"-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=\n"+
"These are the accent rule: Words are rated with + or - on two criteria:\n"+
"1) What is the last sound of the word:\n"+
" If a word ends with an N, S or a vowel the word gets a '+'\n"+
" else the word gets a minus '-.'\n"+
"2) Is the emphasis on the penultimate syllable (next to last):\n"+
" If yes the the word gets a '+'; else a '-.'\n"+
"If the word yields a + - or - + combination on these two criteria\n"+
"then an accent is placed on the vowel of the emphasis syllable.\n"+
"Ex: fran-ces (emphasis on <ces>) and fran-ce-sa (emphasis on <ce>\n"+
"will yield an accent on france's but not on francesa.\n\n"+
"-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=\n"+
"All interrogative prepositions get an accent when used in a question!\n"+
"Ex: ?Que' dice?\n"+
"An accent is also required if the emphasis is on the WEAK vowel of a diphthong\n"+
"Ex: Estanteri'a\n\n");
}
/*determines bad input*/
function invalidword(word)
{
var badword=FALSE;
if (word.length<1) badword=TRUE;
else for (i=0;i<=word.length-1;i++)
if ("abcdefghijklmnopqrstuvwxyz".indexOf(word.charAt(i))==FALSE)
badword=TRUE;
return(badword);
}
/* message in case the word is mi*/
function mimsg ()
{
alert("MI does not take an accent when used as a possesive pronoun but\n"+
"it has one when used as a reflexive pronoun.\n"+
"Ex: Mi perro es feo.\nMe afeito a mi' mismo.\n");
}
/* message in case the word is si*/
function simsg ()
{
alert("SI has an accent when used as 'yes' or a reflexive pronoun\n"+
"Ex: !Si', me duelen los dientes!\nSe afeita a si' mismo.\n");
}
/* message in case the word is el*/
function elmsg ()
{
alert("EL has an accent when used as a pronoun and not as an article\n"+
"Ex: E'l me dio un puno.\n!Deme el dinero!");
}
//taking care of real words
function process (word)
{
var vowel = new Array();
var strong = new Array();
var boundary = new Array();
var syllables = new Array();
var EndPlus =0;
var CountPlus=0;
var LastSyl=0;
var StressSyl=0;
var StressLet=0;
initialize(word,strong,boundary,vowel,syllables);
set_quality(word,strong,vowel);
set_sylls(word,boundary,vowel,strong);
split_consonants(word,boundary,vowel);
weld_ch(word,boundary);
weld_liquids(word,boundary,vowel);
if (vowel[word.length-1]==FALSE) boundary[word.length-1]=FALSE;
divide(boundary,word,syllables);
LastSyl=eval_sylls(syllables,word);
StressSyl=check_syl(LastSyl);
StressLet= set_stress(word,StressSyl,vowel,boundary);
EndPlus=FALSE;CountPlus=FALSE;
if (vowel[word.length-1]==TRUE || vowel[word.length-2]==TRUE)
if (vowel[word.length-1]==TRUE || is_N_S(word.charAt(word.length-1))==TRUE) EndPlus=TRUE;
if (LastSyl-StressSyl==1) CountPlus=TRUE;
if (EndPlus!=CountPlus ||
(strong[StressLet-1]==TRUE && strong[StressLet]==FALSE) ||
(strong[word.length-2]==FALSE && vowel[word.length-2]==TRUE && strong[word.length-1]==TRUE))
display_accent(word,StressLet);
else alert("No accent!");
infomsg(word,EndPlus,CountPlus);
}
/* is this a vowel */
function is_vowel(character)
{
if (character=="a" || character=="e" || character=="i"
|| character=="o" || character=="u")
return(TRUE);
else
return(FALSE);
}
/* is this a string vowel */
function is_strong(character)
{
if (character=="a" || character=="e" || character=="o")
return(TRUE);
else
return(FALSE);
}
/* is this a liquid */
function is_liquid(character)
{
if (character=="l" || character=="r")
return(TRUE);
else
return(FALSE);
}
/* is it an n or an s */
function is_N_S(character)
{
if (character=="n" || character=="s")
return(TRUE);
else
return(FALSE);
}
function initialize(word,strong,boundary,vowel,syllables)
{
var i=0;
for (i=0;i<=word.length-1;i++)
{
boundary[i]=FALSE;
vowel[i]=FALSE;
strong[i]=FALSE;
}
for (i=0;i<=50;i++)
{
syllables[i]="000"
}
boundary[word.length]=TRUE;
}
function set_quality(word,strong,vowel)
{
var i=0;
for (i=0;i<=word.length-1;i++)
if (is_vowel(word.charAt(i))==TRUE) {
vowel[i]=TRUE;
if (is_strong(word.charAt(i))==TRUE) strong[i]=TRUE;
}
}
/* parse syllables. */
function set_sylls(word,boundary,vowel,strong)
{
var i=1;
for (i=1;i<=word.length-1;i++) {
if ((vowel[i]==FALSE && vowel[i-1]==TRUE) ||
(word.charAt(i-1)=="n" && word.charAt(i)=="t") ||
(strong[i-1]==TRUE && strong[i]==FALSE) ||
(strong[i-1]==TRUE && strong[i]==TRUE) ||
(strong[i]==TRUE && strong[i-1]==FALSE && vowel[i-1]==TRUE))
boundary[i]=TRUE;
if (i>1) {
if (vowel[i-2]==TRUE && vowel[i-1]==FALSE
&& vowel[i]==FALSE && word.charAt(i)==word.charAt(i-1)) {
boundary[i-1]=TRUE;
boundary[i]=FALSE;
}
}
}
}
/* split syllables between certain consonants */
function split_consonants(word,boundary,vowel)
{
var i=2;
for (i=2;i<=word.length-1;i++)
if (vowel[i-2]==TRUE && vowel[i-1]==FALSE && vowel[i]==FALSE) {
boundary[i-1]=FALSE;
boundary[i]=TRUE;
}
}
/* c and h belong in the same syllable */
function weld_ch(word,boundary)
{
var i=2;
for (i=2;i<=word.length-1;i++)
if (word.charAt(i-1)=="c" && boundary[i-1]==FALSE
&& word.charAt(i)=="h" && boundary[i]==TRUE) {
boundary[i-1]=TRUE;
boundary[i]=FALSE;
}
}
/* liquids belong in the same syllable */
function weld_liquids(word,boundary,vowel)
{
var i=2;
for (i=2;i<=word.length-1;i++)
if (vowel[i-2]==TRUE && vowel[i-1]==FALSE &&
vowel[i]==FALSE && is_liquid(word.charAt(i))==TRUE) {
boundary[i-1]=TRUE;
boundary[i]=FALSE;
}
}
//split into syllables
function divide(boundary,word,syllables)
{
var temp="";
var i=0; var j=1;
syllables[i]=word.charAt(0);
while (j<=word.length-1) {
temp=word.charAt(j);
if (boundary[j]==FALSE)
syllables[i]=syllables[i]+temp;
else {
syllables[i+1]=temp;
i++;
}
j++;
}
}
function eval_sylls(syllables,word)
{
var i=0; var n=0; var s="";
while (syllables[i] != "000") i++;
n=i-1;
for (i=0;i<=n;i++) s=s+" "+syllables[i]+" <--"+(i+1)+"\n";
alert ("these are the syllables and their respective numbers:\n"+s+
"which one is emphasized when you pronounce "+word+"?");
return(n);
}
function check_syl(LastSyl)
{
var StressSyl=0;
StressSyl=prompt("Enter the number of the syllable with the emphasis",LastSyl);
StressSyl--;
while (StressSyl<0 || StressSyl>LastSyl) {
alert("Invalid syllable number\n");
StressSyl=prompt("Enter the number of the syllable with the emphasis",LastSyl);
StressSyl--;
}
return(StressSyl);
}
function set_stress(word,StressSyl,vowel,boundary)
{
var i=0; var count=0; var temp=0;
for (i=0;i<=word.length-1;i++) {
if (boundary[i]==TRUE) count++;
if (count==StressSyl && vowel[i]==TRUE) temp=i;
}
return(temp);
}
function display_accent(word,StressLet)
{
var i=0; var s="";
for (i=0;i<=word.length-1;i++){
s=s+word.charAt(i);
if (i==StressLet) s=s+"'"; else s=s+" ";
}
alert("The word "+word+" has a written accent\n"+s);
}
function infomsg(word,EndPlus,CountPlus)
{
var s="";
s="The final letter of "+word+" is <"+ word.charAt(word.length-1)+"> thus the <Word End> is";
if (EndPlus==TRUE) s=s+"+.\n";else s=s+"-.\n";
s=s+"The sign for emphasis placement is ";
if (CountPlus==TRUE) s=s+"'+ (penultimate syllable).\n";
else s=s+"- (not the penultimate syllable).\n"+
"A written accent is required when the signs are different.\n";
s=s+"An accent is also required when there is a combo of a strong + weak vowels.\n";
alert(s);
}
// -- done hiding from old browsers -->
</SCRIPT>
</HEAD>
<BODY>
<CENTER>
<H1>Determining Accent Placement in Spanish</H1>
<HR size=10>
</center>
<b>
Enter the words in upper or lower case WITHOUT accents or punctuation.<br>
The program will parse and evaluate the word based on the accent rules. Click <i>Help</i> for theory<br>
</b>
<FORM>
<table width=100% border=5 CELLPADDING=5>
<TR>
<TD><DIV ALIGN=CENTER>Enter the word and click the <b>Accent</b> button</div></td>
</tr>
<TR>
<TD><INPUT TYPE=TEXT NAME=InSeed SIZE=60 ></TD>
</tr>
</TABLE>
<p>
<INPUT TYPE="reset" VALUE="Help" onClick="Help()">
<INPUT TYPE="button" VALUE="Accent?" onClick="Accent(this.form)">
<INPUT TYPE="reset" VALUE="Clear" onClick="ClearForm(this.form)">
</FORM>
</CENTER>
<p>
<hr size=10>
</BODY>
</HTML>