This function receives a string as input and should return the number of syllables in the string.
This function has following conditions:
1. Number of syllables is equal to the number of vowels
2. Two or more consecutive vowels count only as one.
3. One or more vowels at the end of the word are not counted.
This is what I've so far but clearly I'm still missing a lot. I'm not sure how to continue here, so I hope you guys can help.
def syllables(word):
vowels = ['a','e','i','o','u','y']
# Delete ending vowel of the word since they don't count in number of syllables
# I've no idea how to remove all ending vowels though
word = word[:-1]
# List with vowels that appear in the word
vowelsList = [x for x in vocals if x in word]
N = []
for i in word:
if i in vowels:
N += i
N = len(N)
return N
print(syllables("bureau"))
# Should print "1" but prints "3" instead
I suggest you the following simple code:
def syllables(word):
vowels = ['a', 'e', 'i', 'o', 'u', 'y']
N = 0
previousLetterIsAVowel = False
# Perform a loop on each letter of the word
for i in word.lower():
if i in vowels:
# Here it is a vowel
# Indicate for the next letter that it is preceded by a vowel
# (Don't count it now as a syllab, because it could belong to a group a vowels ending the word)
previousLetterIsAVowel = True
else:
# Here: it is not a vowel
if previousLetterIsAVowel:
# Here it is preceded by a vowel, so it ends a group a vowels, which is considered as a syllab
N += 1
# Indicate for the next letter that it is not preceded by a vowel
previousLetterIsAVowel = False
return N
print(syllables("bureau")) # it prints 1
print(syllables("papier")) # it prints 2
print(syllables("ordinateur")) # it prints 4
print(syllables("India")) # it prints 1
I also provide a one-line style solution using regex, easily readable too if you know a little bit about regex. It simply counts the number of groups of consecutive vowels that are followed by a consonant:
import re
def syllables(word):
return len(re.findall('[aeiouy]+[bcdfghjklmnpqrstvwxz]', word.lower()))
To check the last vowel you can try something like this (I wouldn't iterate as you're going to loose whole syllables): -> EX: Italian word "Aia" (threshing floor)
if word[-1] in vocals:
word=word[:-1]
-- sorry but I didn't manage to put 'code' into comments so a posted an answer
I would go for:
def syllables(word):
def isVowel(c):
return c.lower() in ['a','e','i','o','u','y']
# Delete ending vowel of the word since they don't count in number of syllables
while word and isVowel(word[-1]):
word = word[:-1]
lastWasVowel = False
counter = 0
for c in word:
isV = isVowel(c)
# skip multiple vowels after another
if lastWasVowel and isV:
continue
if isV:
# found one
counter += 1
lastWasVowel = True
else:
# reset vowel memory
lastWasVowel = False
return counter
Stolen from LaurentH:
print(syllables("bureau")) # prints 1
print(syllables("papier")) # prints 2
print(syllables("ordinateur")) # prints 4
print(syllables("I")) # prints 0
I think we have return 1 if there is previousLetterIsAVowel and N returns 0. Example word Bee.
In Addition to Laurent H. answer
if N == 0 and previousLetterIsAVowel:
return 1
else:
return N
Related
I tried doing it like this:
# the system waits for the user's string input
word = input()
# defining vowels
vowels = "aeiou"
#// BEGIN_TODO [count_substrings] counting all substrings in a string
count_c: int = 0
count_v: int = 0
total_substrings = (len(word)*(len(word)+1)) // 2
for letter in word:
if letter in vowels:
for i in range(total_substrings):
for j in range(i, total_substrings):
count_v += 1
if letter not in vowels:
for i in range(total_substrings):
for j in range(i, total_substrings):
count_c += 1
print('number of substrings starting with Vowels: {} - number of substrings starting with Consonants: {}'.format(count_v, count_c))
#// END_TODO [count_substrings]
but the code outputs strange numbers. Help would be appreciated on how to approach this problem.
Looks like you're trying to do the same thing twice: You use a formula to calculate the number of substrings, but then you also try to count them with loops. One of those is unnecessary. Or you could combine a little calculation and a little counting like this:
VOWELS = "aeiou"
word = 'test'
n = len(word)
count_v: int = 0
count_c: int = 0
for i, letter in enumerate(word):
# number of substrings starting at i, not counting the empty string:
n_substrings = n - i
if letter in VOWELS:
count_v += n_substrings
else:
count_c += n_substrings
print(f"number of substrings starting with vowels: {count_v}")
print(f"number of substrings starting with consonants: {count_c}")
number of substrings starting with vowels: 3
number of substrings starting with consonants: 7
Note, however, that this approach will overcount if there are identical substrings that can be started at different points in the string. For example, if word = 'eel', the substring 'e' would be counted twice. If you don't want that, it gets more complicated. You could extract all the substrings and collect them in two sets (one for those starting with a vowel, and one for the rest), to remove the duplicates.
Hey guys so I'm writing a code for text message abbreviations and these are the following criteria:
Spaces are maintained, and each word is encoded individually. A word is a consecutive string of alphabetic characters.
If the word is composed only of vowels, it is written exactly as in the original message.
If the word has at least one consonant, write only the consonants that do not have another consonant immediately before them. Do not write any vowels.
The letters considered vowels in these rules are 'a', 'e', 'i', 'o' and 'u'. All other letters are considered consonants.
I have written a code and checked it but it is failing for the condition where if the word is composed only of vowels, it is written exactly as in the original message. My code currently is taking out all of the vowels. Like for this example, "aeiou bcdfghjklmnpqrstvwxyz" the code should return "aeiou b"
I tried using another helper function to determine when a word is all vowels but it isn't working. Any suggestions on how to implement something that could make this work? Thanks!
def Vowel(x):
vowels = "a" "e" "i" "o" "u"
value = 0
for ch in x:
if x == vowels:
value = value + 1
return value
def isVowel(phrase):
vowel = "a" "e" "i" "o" "u"
value = 0
for ch in phrase:
if ch in vowel:
value = value + 1
return value
def noVowel(ch):
vowel = "a" "e" "i" "o" "u"
value = 0
for i in ch:
if ch not in vowel:
value = value + 1
return value
def transform(word):
before = 'a'
answer = ""
for ch in word:
if Vowel(ch):
answer += ch
if noVowel(ch) and isVowel(before):
answer += ch
before = ch
return answer
def getMessage(original):
trans = ""
for word in original.split():
trans = trans + " " + transform(word)
trans = trans.strip()
return trans
if __name__ == '__main__':
print(getMessage("aeiou b"))
Your three Vowel-functions are all using a disfunctional for-loop and are highly redundant. Also Vowel() would always return 0, as x may never be a tuple of five vowels.
You would need only one function that just returns True if the character is a vowel and False if it is not. Then, you can use this function in the if-blocks in transform:
def Vowel(x):
vowels = ["a","e","i","o","u"]
return x.lower() in vowels ## True if x is a vowel (upper or lower case)
def transform(word):
before = 'a'
answer = ""
for ch in word:
if Vowel(ch):
answer += ch
if not Vowel(ch) and Vowel(before):
answer += ch
before = ch
return answer
You have not identified what to do with words that have both consonants and vowels...if you provide a few more example inputs and outputs, I might decide to change my code, but this is what I have come up with so far.
I have used regex:
(\b[aeiou]*\b) : finds vowel only words
([bcdfghjklmnpqrstvwxyz]*) : looks for groups of consonants.
The for-loop : helps to return the vowel-only words, or the first letter of the consonant groups.
import re
strings_to_test = ["aeiou bcdfghjklmnpqrstvwxyz ae hjgfd a",
"gg jjjjopppddjk eaf"]
pattern = re.compile(r"(\b[aeiou]*\b)|([bcdfghjklmnpqrstvwxyz]*)")
for line in strings_to_test:
results = []
match_list = pattern.findall(line)
for m in match_list:
if m[0]:
# word of vowels found
results.append(m[0])
elif m[1]:
#consonants found
results.append(m[1][0])
print(results)
OUTPUT:
['aeiou', 'b', 'ae', 'h', 'a']
['g', 'j', 'p', 'f']
I'm learning to work with strings and my project is to calculate the number of syllables in a sentence.
I'm given some code that calculates the syllables by adding to the variable, syllables += 1, for each vowel. But the end product is wrong because consecutive vowels should only count as a single +1. For example the word "certain" should return two syllables, not three. There is more to it than this but it's not relevant to my question.
Here is how I solved the problem, with an explanation below:
syllables = 0
vowels = "aeiouAEIOU"
vowelContainer = "" # Part of my added code for solution. Create some container to hold
# a representation of consecutive vowels.
for word in text.split():
for vowel in vowels:
syllables += word.count(vowel)
for ending in ['es', 'ed', 'e']:
if word.endswith(ending):
syllables -= 1
if word.endswith('le'):
syllables += 1
for vowel in word: # My added code for solution: rest of lines that follow
if vowel == vowel in vowels: # I fill in the container with Ts and Fs to create a string
vowelContainer += "T" # that I can then check for consecutive vowels.
else:
vowelContainer += "F"
count2ConsecutiveVowels = vowelContainer.count("TT") # I count how many times there are two
count3ConsecutiveVowels = vowelContainer.count("TTT") # or three vowels in the word.
syllables = syllables - count2ConsecutiveVowels - count3ConsecutiveVowels # I subtract the count
vowelContainer = "" # from total syllables
# At the end of the loop I empty the container for the next word.
I managed to come up with a solution, but it is messy. Is there a simpler way to do this?
I think this code should work:
vowels = "aeiouAEIOU"
text = "certain"
syllables_count = 0
if len(text) == 1:
if text[0] in vowels:
syllables_count += 1
else:
for i in range(len(text) - 1):
if text[i] in vowels and text[i+1] not in vowels:
syllables_count += 1
elif text[i] in vowels and i+1 == len(text) - 1:
syllables_count += 1
print(syllables_count)
In order to solve the problem of consecutive vowels you mentioned above, and get the correct number of syllables, I suggest using a regular expression as in the following example:
import re # put in the beginning to import regular expression module
word='certaaaaiin' # just for example
a=re.findall(r'[aeiouAEIOU](?![aeiouAEIOU])',word)
syllables =len(a)
print(syllables)
# output is 2
In this method, any number of consecutive vowels is considered as one in counting
in the regular expression [aeiouAEIOU](?![aeiouAEIOU]), it selects any vowel in the list, in case of that vowel does not have another one after.
vowels = "aeiou"
count = 0
with open("/home/doofinschmurts/PycharmProjects/Practice_001/text.txt", "r") as text:
text = text.read()
for character in range(len(text) - 1):
if text[character] in vowels and text[(character + 1)] not in vowels and text[character - 1] not in vowels:
count += 1
elif text[character] in vowels and text[character + 1] in vowels and text[character - 1] not in vowels:
count += 1
elif text[character] in vowels and text[character + 1] in vowels and text[character + 2] in vowels and text[character - 1] not in vowels:
count += 1
print(count)
I'm new to coding but this script works and the main "jist" is in the long if statements. This wont be the best answer; however, if any veterans wouldn't mind critiquing me also.
Every time you encounter a vowel, you need to increment the syllables counter by 1. Otherwise, you can ignore it.
Try this and see if this meets your needs.
text = input('Enter your word(s) :')
syllables = 0
vowels = "aeiou" #no need to check for upper and lower
for word in text.split():
vowel_found = False #initialize vowel_found to False and check if the world has vowels
for char in word: #iterate thru each char in the word
if not vowel_found and char.lower() in vowels: #for every new vowel found, increment syllables by 1
syllables +=1
vowel_found = True
else:
vowel_found = False #reset to increment counter again
print (syllables)
Here's the output I got:
Enter your word(s) :syllables
2
Enter your word(s) :certain
2
Enter your word(s) :book
1
Enter your word(s) :case
2
Enter your word(s) :Heart
1
Let me know if this code fails. I would like to improve this.
Define a function lineStats() that takes one parameter:
1. paragraph, a string of words and white spaces
The function returns a list containing the number of vowels in each line.
for example,
t="Apple\npear and kiwi"
print(lineStats(t))
[2,5]
This is what I have. I've gotten the output to be 7 but not to be able to make it 2,5. I tried to make a counter for each line but that didn't work, any suggestions?
def lineStats(paragraph):
vowels = "AEIOUaeiou"
for line in paragraph:
for word in line:
for letter in word:
if letter in vowels:
counter +=1
else:
continue
return counter
t = "Apple\npear and kiwi"
print(lineStats(t))
Here's an adaption of your current code
def lineStats(paragraph):
vowels = "AEIOUaeiou"
counter = []
current_line_count = 0
newline = "\n"
for letter in paragraph:
if letter in vowels:
current_line_count += 1
elif letter == newline:
counter.append(current_line_count)
current_line_count = 0
counter.append(current_line_count)
return counter
t="Apple\npear and kiwi"
def lineStats(p):
#find vowels in each line and sum the occurences using map
return map(sum, [[1 for e in f if e in "AEIOUaeiou"] for f in p.split('\n')])
lineStats(t)
Out[601]: [2, 5]
Try this
Create this function
def temp(x):
return sum(v for k, v in Counter(x).items() if k.lower() in 'aeiuo')
Now
from collections import Counter
print [temp(x) for x in lines.split('\n')]
Changing as little of your code as necessary, other answers offer improvements.
def lineStats(paragraph):
counter = []
vowels = "AEIOUaeiou"
lines = paragraph.split('\n')
for line in lines:
count = 0
for word in line:
for letter in word:
if letter in vowels:
count +=1
else:
continue
counter.append(count)
return counter
t = "Apple\npear and kiwi"
print(lineStats(t)) # [2, 5]
The problem states it wants the result to be a list of counts, so changing counter to be a list that we can append to, then use that list to store the vowel count for each line. That may be the only major change to your code that you need to get the required output.
However, there is the concern of newlines ('\n') in the "paragraph", so we str.split() the paragraph into individual lines before entering the for-loop. This will break the count for each line, instead of the total count that you were getting.
I am trying to make an up language translator. Simple task for me in python. Or so i thought. If you are unaware, up language is when you take a word and say it while adding up before every vowel. for example, Andrew would be Upandrupew. I am trying to find out how find all of the vowels in a user submitted word, and put up before them. Is there a way to cut up a word before all vowels. so excellent would be exc ell ent? thanks.
maybe
VOWELS = 'aeiou'
def up_it(word):
letters = []
for letter in word:
if letter.lower() in VOWELS:
letters.append('Up')
letters.append(letter)
return ''.join(letters)
can be simplified to
def up_it(word):
return ''.join('up'+c if c.lower() in 'aeiou' else c for c in word)
You could do that with a regex:
import re
a = "Hello World."
b = re.sub("(?i)([aeiou])", "up\\1", a)
The (?i) makes it case-insensitive. \\1 refers to the character that was matched inside ([aeiou]).
''.join(['up' + v if v.lower() in 'aeiou' else v for v in phrase])
for vowel in [“a“,“e“,“i“,“o“,“u“]:
Word = Word.replace(vowel,“up“+vowel)
print(Word)
import re
sentence = "whatever"
q = re.sub(r"([aieou])", r"up\1", sentence, flags=re.I)
vowels = ['a', 'e', 'i', 'o', 'u']
def upped_word(word):
output = ''
for character in word:
if character.lower() in vowels:
output += "up"
output += character
return output
Here is a one-liner for the entire problem
>>> "".join(('up' + x if x.upper() in 'AEIOU' else x for x in 'andrew'))
'upandrupew'
Here's one way of doing it.
wordList = list(string.lower())
wordList2 = []
for letter in wordList:
if letter in 'aeiou':
upLetter = "up" + letter
wordList2.append(upLetter)
else:
wordList2.append(letter)
"".join(wordList2)
Create a list of letters (wordList), iterate through those letters and append it to a second list, which is joined at the end.
Returns:
10: 'upandrupew'
In one line:
"".join(list("up"+letter if letter in "aeiou" else letter for letter in list(string.lower())))
I'd probably go with RegExp but there are already many answers using it. My second choice is the map function, witch is better then iterate through every letter.
>>> vowels = 'aeiou'
>>> text = 'this is a test'
>>> ''.join(map(lambda x: 'up%s'%x if x in vowels else x, text))
'thupis upis upa tupest'
>>>
def is_vowel(word):
''' Check if `word` is vowel, returns bool. '''
# Split word in two equals parts
if len(word) % 2 == 0:
parts = [word[0:len(word)/2], word[len(word)/2:]]
else:
parts = [word[0:len(word)/2], word[(len(word)/2)+1:]]
# Check if first part and reverse second part are same.
if parts[0] == parts[1][::-1]:
return True
else:
return False
This is a smart solution which helps you to count and find vowels in input string:
name = input("Name:- ")
counter = []
list(name)
for i in name: #It will check every alphabet in your string
if i in ['a','e','i','o','u']: # Math vowels to your string
print(i," This is a vowel")
counter.append(i) # If he finds vowels then he adds that vowel in empty counter
else:
print(i)
print("\n")
print("Total no of words in your name ")
print(len(name))
print("Total no of vowels in your name ")
print(len(counter))