Vowel Counting Sequence

This is not a homework question; it is an exam preparation question.

I have to define a function syllables(word)that takes into account the number of syllables in a Word as follows:

• The maximum sequence of vowels is a syllable;

• The final e in a word is not a syllable (or a sequence of vowels is part of Iz).

I do not need to deal with any special cases, such as the final e in the Monosyllabic (for example, be or bee).

>>> syllables(’honour’)
2
>>> syllables(’decode’)
2
>>> syllables(’oiseau’)
2

Should I use regex here or just understand the list?

+3
source share
9 answers

. ( , - . : "lower" "endswith", .)

import re
def syllables(word):
    word = word.lower()
    if word.endswith('e'):
        word = word[:-1]
    count = len(re.findall('[aeiou]+', word))
    return count

for word in ('honour', 'decode', 'decodes', 'oiseau', 'pie'):
    print word, syllables(word)

:

honour 2
decode 2
decodes 3
oiseau 2
pie 1

, 'decodes' , 'decode' ( , ).

. ? , ? , , , .

+2

- .

-e, .

+2

, .

.

+1

:

[abc] a, b c.

A + ,

$ .

(?<=x) , x.

(?!x) , x.

EDIT:

, , . , :

[aeiou]+(?!(?<=e)$)

, e (, u tongue o toe),

[aeiou]+(?=[^aeiou])|[aeiou]*[aiou]$

, , , .

+1

. ( ) . :

def syllables(word):
    word = word.lower()
    if word.endswith('e'):
        word = word[:-1]
    vowels = 'aeiou'
    in_vowel_group = False
    vowel_groups = 0
    for letter in word:
        if letter in vowels:
            if not in_vowel_group:
                in_vowel_group = True
                vowel_groups += 1
        else:
            in_vowel_group = False
    return vowel_groups
+1

. , . , . , , . :

, , : " , ". . ~

, , , .

0

, , , . , , , , PyParsing. !

0

, a, e, i, o u, , ... 1 true, 1 false, 2 true, 2 ..

To handle the case where the last letter "e" follows the consonant (as in food), just check the last two letters of the word before starting. If they match this pattern, trim the final e and process as usual.

0
source

This template works for your definition:

(?!e$)([aeiouy]+)

Just count how many times it happens.

0
source

Source: https://habr.com/ru/post/1773042/


All Articles