Skip to content
Advertisement

count consecutive vowels in words

def openFile(fileName): 
  infile = open(fileName, "r")
  infile.readline() words = [] for data in infile: dataStrip = data.strip('n') 
  words.append(dataStrip) return words

def findVowels(words): 
  splitWord = words.split() 
  for word in splitWord: 
    if word == "a" or "e" or "i" or "o" or "u": 
    return True 
  else: return False

def main(): 
  file = openFile("vowels.txt") 
  for word in file: 
    vowelCount = 0 
    if findVowels(word): 
      vowelCount += 1 
    else: vowelCount = vowelCount 
  print(f"{word} has {vowelCount} vowels in it") 
main()

So, this is what I have so far to find vowels in each word in the text file. It doesn’t work, and I still need to be able to count how many consecutive vowels are in the words. If you can help, but make it to the level of simplicity I have done so far, that’d be great help. Lmk if you need more info.

Advertisement

Answer

My version which search highest consecutive amount.

I use list for this

  • if there is vowel then add +1 to last value on list (with curren consecutive vowels),
  • if there is no vowel then append 0 at the end of list (to count next consecutive vowels)

And later I get max()

def count_vowels(word):

    all_results = [0]
    
    current = 0
    sequence = 1

    for char in word:
        if char in 'aeiou':
            all_results[-1] += 1  # update last item
        else:
            all_results.append(0) # add new last item
          
        print(char, all_results)
  
    return max(all_results)

def main():
    words = "aaabeehi9ooguuute queueing aeioua aaaTee BTW aaa aTa".split()
    for word in words:
        vowel_count = count_vowels(word)
        print(f"{word} has {vowel_count} vowels in it")

main()

Result: (which display also history of counting)

a [1]
a [2]
a [3]
b [3, 0]
e [3, 1]
e [3, 2]
h [3, 2, 0]
i [3, 2, 1]
9 [3, 2, 1, 0]
o [3, 2, 1, 1]
o [3, 2, 1, 2]
g [3, 2, 1, 2, 0]
u [3, 2, 1, 2, 1]
u [3, 2, 1, 2, 2]
u [3, 2, 1, 2, 3]
t [3, 2, 1, 2, 3, 0]
e [3, 2, 1, 2, 3, 1]
aaabeehi9ooguuute has 3 vowels in it
q [0, 0]
u [0, 1]
e [0, 2]
u [0, 3]
e [0, 4]
i [0, 5]
n [0, 5, 0]
g [0, 5, 0, 0]
queueing has 5 vowels in it
a [1]
e [2]
i [3]
o [4]
u [5]
a [6]
aeioua has 6 vowels in it
a [1]
a [2]
a [3]
T [3, 0]
e [3, 1]
e [3, 2]
aaaTee has 3 vowels in it
B [0, 0]
T [0, 0, 0]
W [0, 0, 0, 0]
BTW has 0 vowels in it
a [1]
a [2]
a [3]
aaa has 3 vowels in it
a [1]
T [1, 0]
a [1, 1]
aTa has 1 vowels in it

EDIT:

Version which can count highest consecutive vowels and highest consecutive non-vowels. For non-volwes I use negative value and min(). Because sometime text may have only vowels or only non-vowels so I add extra 0 in all_results

def count_vowels(word):

    all_results = [0, 0]
    
    current = 0
    sequence = 1

    for char in word:
        if char in 'aeiou':
            if all_results[-1] < 0:
                all_results.append(0)
            all_results[-1] += 1  # update last item
        else:
            if all_results[-1] > 0:
                all_results.append(0) # add new last item
            all_results[-1] -= 1  # update last item
          
        print(char, all_results)
        
    return max(all_results), min(all_results)


def main():
    words = "aaabeehi9ooguuute queueing aeioua aaaTee BTW aaa aTa".split()
    for word in words:
        vowel_count = count_vowels(word)
        print(f"{word} | max vowels: {vowel_count[0]} | max non-vowels: {-vowel_count[1]}")

main()

Result:

a [0, 1]
a [0, 2]
a [0, 3]
b [0, 3, -1]
e [0, 3, -1, 1]
e [0, 3, -1, 2]
h [0, 3, -1, 2, -1]
i [0, 3, -1, 2, -1, 1]
9 [0, 3, -1, 2, -1, 1, -1]
o [0, 3, -1, 2, -1, 1, -1, 1]
o [0, 3, -1, 2, -1, 1, -1, 2]
g [0, 3, -1, 2, -1, 1, -1, 2, -1]
u [0, 3, -1, 2, -1, 1, -1, 2, -1, 1]
u [0, 3, -1, 2, -1, 1, -1, 2, -1, 2]
u [0, 3, -1, 2, -1, 1, -1, 2, -1, 3]
t [0, 3, -1, 2, -1, 1, -1, 2, -1, 3, -1]
e [0, 3, -1, 2, -1, 1, -1, 2, -1, 3, -1, 1]
aaabeehi9ooguuute | max vowels: 3 | max non-vowels: 1
q [0, -1]
u [0, -1, 1]
e [0, -1, 2]
u [0, -1, 3]
e [0, -1, 4]
i [0, -1, 5]
n [0, -1, 5, -1]
g [0, -1, 5, -2]
queueing | max vowels: 5 | max non-vowels: 2
a [0, 1]
e [0, 2]
i [0, 3]
o [0, 4]
u [0, 5]
a [0, 6]
aeioua | max vowels: 6 | max non-vowels: 0
a [0, 1]
a [0, 2]
a [0, 3]
T [0, 3, -1]
e [0, 3, -1, 1]
e [0, 3, -1, 2]
aaaTee | max vowels: 3 | max non-vowels: 1
B [0, -1]
T [0, -2]
W [0, -3]
BTW | max vowels: 0 | max non-vowels: 3
a [0, 1]
a [0, 2]
a [0, 3]
aaa | max vowels: 3 | max non-vowels: 0
a [0, 1]
T [0, 1, -1]
a [0, 1, -1, 1]
aTa | max vowels: 1 | max non-vowels: 1

BTW:

To count more types of chars – vowels, consonants, digits, others – I would use tuples ("vowels", 0), ("consonants", 0) instead of positive and negative values.


EDIT:

Full code

I assume you have every word in separated line and you don’t have header at the top of file.

def count_vowels(word):

    all_results = [0]
    
    current = 0
    sequence = 1

    for char in word:
        if char in 'aeiou':
            all_results[-1] += 1   # update last item
        else:
            all_results.append(0)  # add new last item
          
        #print(char, all_results)  # ie. `g [0, 5, 0, 0]`
  
    return max(all_results)

def read_words():
    fh = open("vowels.txt")   # `fh` means `file handler`

    text = fh.read()  # read all as one string

    fh.close()

    words = text.split("n")   # split string into list of lines
    #words = text.splitlines()  # split string into list of lines
   
    #words = words[1:]  # skip first line with header

    #words = "aaabeehi9ooguuute queueing aeioua aaaTee BTW aaa aTa".split()

    return words

def main():

    # --- read ---
    
    words = read_words()

    # --- count ---

    words_with_counts = []
    
    for word in words:
        vowel_count = count_vowels(word)
        words_with_counts.append( (vowel_count, word) )
        
    # --- sort ---
    
    sorted_words_with_counts = sorted(words_with_counts) #, reverse=True)

    # --- display ---
    
    for vowel_count, word in sorted_words_with_counts:
        print(f"{word} has {vowel_count} vowels in it")

# --- main ---

main()
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement