-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenglish2piglatin.py
228 lines (158 loc) · 4.82 KB
/
english2piglatin.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
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
#!/usr/bin/python
"""
English to Piglatin translator
It takes a word or a whole sentence from stdin,
lower cases it and transforms it then
based upon the first character of
the given string.
"""
"""
detectWords()
Takes a sentence and splits it by searching
for whitespaces.
input arguments:
string - Sentence or word to examine
return values:
words - array of single word strings
"""
def detectWords(string):
offset = 0
words = []
for idx in range (0, len(string)):
if string[idx] == " ":
words.append(string[offset:idx])
offset = idx + 1
if idx == len(string) - 1:
words.append(string[offset:idx + 1])
#print "Found %d word(s)!" % len(words)
#print words
return words
"""
stripSpecialChars()
Examines a given string for
special characters, extracts them
and stores them into an extra array.
input arguments:
string - array of word strings
return values:
specialChars - Tuple of essential values [word index,
character index, word length, special character]
strippedWords - Array of words which are stripped from
special characters
"""
def stripSpecialChars(words):
specialChars = []
strippedWords = []
for idx in range (0, len(words)):
word = words[idx]
appendAsIs = True
for idx2 in range (0, len(word)):
if word[idx2] == "!" or word[idx2] == "?" or word[idx2] == "." or word[idx2] == ",":
t = idx, idx2, len(word), word[idx2] # [pos in sentence, pos in word, len of word, special char]
specialChars.append(t)
temp = word[ 0 : idx2 ] + word[ idx2 + 1 : len(word) ]
strippedWords.append(temp)
appendAsIs = False
if appendAsIs:
strippedWords.append(word)
return specialChars, strippedWords
"""
en2pl()
This function takes a word string
and tranforms it into it's piglatin
version. First the word is checked
for correctness, means that it must not
be empty or contain any special characters.
Then, depending on the starting character,
the word gets transformed.
a) If the first char is a vowel, then only
the pyg extension is appended to the word
example: "ape" --> "ape-ay"
b) If the first char is a consonant, the first
char is extracted from the string, reappended
at the end in addition to the pyg extension
example: "tiger" --> "iger-t-ay"
input arguments:
string - Word to examine
return values:
new_word - tranformed piglatin word
"""
def en2pl(string):
#pig extension
pyg = 'ay'
#print "Current string is: '" + string + "'"
#check if it's a valid string. Means not empty and only ASCII chars
if len(string) > 0 and string.isalpha():
#lower case the inout
word = string.lower()
#print "lower: " + word
#get first char
first = word[0]
#print "First: " + first
#check for vowels
if first == 'a' or first == 'o' or first == 'e' or first == 'i' or first == 'u':
#just append pig extension in this case
new_word = string + pyg
#return new word
return new_word
else:
#Take the rest of the word append first char and the
#the pig extension
new_word = string[1:len(string)] + first + pyg
#print ne word
return new_word
else:
#Give warning in this case
return '>>Empty or invalid string<<'
"""
doConversion()
This function takes all cleaned words and all
extracted special chars and applies the piglatin
conversion, rematches the special chars to the
converted words, and puts the whole sentence
back together.
input arguments:
specialChars - List of tuple which contains essential information
about each special char extracted
strippedWords - List of all bare stripped strings which resemble
to the sentence in the end
"""
def doConversion(specialChars, strippedWords):
pl_string = ""
#loop through all words
for idx in range (0, len(strippedWords)):
next = en2pl(strippedWords[idx])
next_sp = ""
#loop through all special chars
for idx2 in range (0, len(specialChars)):
#if current word has a special char
if idx == specialChars[idx2][0]:
#print specialChars[idx2]
#if special char is at end of word
if specialChars[idx2][1] == specialChars[idx2][2] - 1:
next_sp = next + specialChars[idx2][3]
#if special char is in the middle of the word
else:
next_sp = next[0:specialChars[idx2][1]] + specialChars[idx2][3] + next[specialChars[idx2][1]:specialChars[idx2][2] + 1]
if next_sp == "":
next_sp = next
pl_string += next_sp + " "
return pl_string
"""
translate()
Wrapper function for easy usage
of the translator.
input arguments:
org_string - Word/sentence to examine
return values:
translated piglatin sentence or word
"""
def translate(org_string):
words = detectWords(org_string)
raw = stripSpecialChars(words)
return doConversion(raw[0], raw[1])
"""
main block
"""
original = raw_input('Enter a word or sentence: ')
print "In pig latin this would be: " + translate(original)