-
Notifications
You must be signed in to change notification settings - Fork 5
/
findstress.py
executable file
·112 lines (112 loc) · 4.49 KB
/
findstress.py
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
def find_stress_index(sequence): # Find stress syllable in word starting from "start"
if sequence == u'' or len(sequence) == 0:
return -1
print(sequence)
consonants = [
"r", "g", "y", "G",
"b", "z", "f", "v",
"t", "s", "q", "p",
"$", "k", "<",
"j", "S", "l",
"H", "D", "m",
"x", "T", "n",
"d", "Z", "h",
"*", "E", "w", "^"]
geminatedConsonants = [
"<<", "rr", "gg", "vv",
"bb", "zz", "ff", "GG",
"tt", "ss", "qq", "pp",
"$$", "kk", "yy",
"jj", "SS", "ll",
"HH", "DD", "mm",
"xx", "TT", "nn",
"dd", "ZZ", "hh",
"**", "EE", "ww", "^^"]
longVowels = ["aa", "AA",
"uu0", "uu1",
"ii0", "ii1",
"UU0", "UU1",
"II0", "II1"]
shortVowels = ["a", "A",
"u0", "u1",
"i0", "i1",
"U0", "U1",
"I0", "I1"]
syllableString = ""
i = 0
while i < len(sequence):
if (sequence[i] in geminatedConsonants):
syllableString += "C"
elif (sequence[i] in consonants):
syllableString += "c"
elif (sequence[i] in longVowels):
syllableString += "V"
elif (sequence[i] in shortVowels):
syllableString += "v"
else:
print('Unacceptable char when finding stress syllable: ' + sequence[i] + ' ' + syllableString + '\n')
file = open("errors", "a")
file.write(sequence[i])
file.write("\n")
file.close()
return 0
i += 1
if syllableString[0] in ['v', 'V']:
return -1
# Stress falls on the last syllable if it is super heavy
if syllableString.endswith("cVc") and syllableString.endswith("CVc"):
return i - 2 # 3
if (syllableString.endswith("cvvc") or syllableString.endswith("cvcc") or syllableString.endswith(
"cVcc") or syllableString.endswith("Cvvc") or syllableString.endswith("Cvcc") or syllableString.endswith(
"CVcc")):
return i - 3 # 4
if syllableString.endswith("cvvcc") and syllableString.endswith("Cvvcc"):
return i - 4 # 5
# Stress is at the beginning if it is a monosyllabic word
if syllableString == "cvv" or syllableString == "cvc":
return i - 2 # 3
if syllableString == "cV":
return i - 1 # 2
# Remove last syllable if first two rules miss
if syllableString.endswith("cvv") or syllableString.endswith("cvc"):
syllableString = syllableString[0:-3]
i = i - 3
elif syllableString.endswith("Cvv") or syllableString.endswith("Cvc"):
syllableString = syllableString[0:-3]
syllableString += 'c'
i = i - 2
elif syllableString.endswith("cV") or syllableString.endswith("cv"):
syllableString = syllableString[0:-2]
i = i - 2
elif syllableString.endswith("CV") or syllableString.endswith("Cv"):
syllableString = syllableString[0:-2]
syllableString += 'c'
i = i - 1
# Stress is at penultimate syllable if disyllabic word
if syllableString == "cvv" or syllableString == "cvc":
return i - 2 # 3
if syllableString == "cV" or syllableString == "cv":
return i - 1 # 2
# Stress is at penultimate syllable if it is heavy
if (syllableString.endswith("cvv") or syllableString.endswith("cvc") or syllableString.endswith(
"Cvv") or syllableString.endswith("Cvc") or syllableString.endswith("cVc") or syllableString.endswith(
"cVC") or syllableString.endswith("CVc")):
return i - 2 # 3
if syllableString.endswith("cV") or syllableString.endswith("CV"):
return i - 1 # 2
if syllableString.endswith("cv"):
i = i - 2
syllableString = syllableString[0:-2]
elif syllableString.endswith("Cv"):
i = i - 1
syllableString = syllableString[0:-2]
syllableString += 'c'
# Stress is at antepenultimate syllable otherwise
if (syllableString.endswith("cvv") or syllableString.endswith("cvc") or syllableString.endswith(
"Cvv") or syllableString.endswith("Cvc") or syllableString.endswith("cVc") or syllableString.endswith(
"cVC") or syllableString.endswith("CVc")):
return i - 2 # 3
if (syllableString.endswith("cV") or syllableString.endswith("cv") or syllableString.endswith(
"CV") or syllableString.endswith("Cv")):
return i - 1 # 2
return i + 1