This is a follow up to this question I posted earlier:
I am having trouble returning 1 only if the last letter of a word is a vowel.
What I have so far is:
import sys
import re
pattern = re.compile("^[a-z]+$") # matches purely alphabetic words
starting_vowels = re.compile("(^[aeiouAEIOU])") # matches starting vowels
ending_vowels = re.compile("[aeiouAEIOU]$") # matches ending vowels
starting_vowel_match = 0
ending_vowel_match = 0
for line in sys.stdin:
line = line.strip() # removes leading and trailing whitespace
words = line.lower().split() # splits the line into words and converts to lowercase
for word in words:
starting_vowel_match = 1 if starting_vowels.match(word[0]) else 0
# ternary operator, word[0] is the first letter of the word
ending_vowel_match = 1 if ending_vowels.match(word[-1]) else 0
for letter in word:
if pattern.match(letter):
print("%s 1" % letter, starting_vowel_match, ending_vowel_match)
starting_vowel_match = 0
ending_vowel_match = 0
The output I am getting is when I run this with a text file with the string "its a beautiful life" is:
i 1 1 0
t 1 0 0
s 1 0 0
a 1 1 1
b 1 0 0
e 1 0 0
a 1 0 0
u 1 0 0
t 1 0 0
i 1 0 0
f 1 0 0
u 1 0 0
l 1 0 0
l 1 0 1
i 1 0 0
f 1 0 0
e 1 0 0
It seems as though the last l character is returning a 1 in the fourth column because the last letter in the string life is e. What I would like is for only the last e to output 1 in the fourth column, since it is the last letter of a word.
The output I want is:
i 1 1 0
t 1 0 0
s 1 0 0
a 1 1 1
b 1 0 0
e 1 0 0
a 1 0 0
u 1 0 0
t 1 0 0
i 1 0 0
f 1 0 0
u 1 0 0
l 1 0 0
l 1 0 0
i 1 0 0
f 1 0 0
e 1 0 1