This is from my previous post here:
How to return 1 only if the last letter of a word is a vowel? Return 0 otherwise
Here is the code I am using:
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:
if len(word) == 1:
print(word[0], 1, *((1, 1) if word[0] in 'aeiou' else (0, 0))) # * unpacks startVowel 1 endVowel 1 if word[0] is a vowel
else:
print(word[0], 1, 1 if word[0] in 'aeiou' else 0, 0)
print(*(f'{letter} 1 0 0' for letter in word[1: -1]), sep='\n')
print(word[-1], 1, 0, 1 if word[-1] in 'aeiou' else 0)
I want this to only print if a character is an alphabet, so an example output I would like is this for a text file containing the string "It's a beautiful life":
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
I am currently seeing this:
i 1 1 0
' 1 0 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
I am wondering how to get rid of special characters in the output. I have tried a couple things including adding
for letter in word:
if pattern.match(letter):
in the for letter in word"
block, but it is not returning the output I want.