Created
March 21, 2018 20:31
-
-
Save ramachandrajr/86ff25f8f0576b7f4398215ca9c8f75b to your computer and use it in GitHub Desktop.
Calculates vowel probabilities in a text
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import re | |
import sys | |
yevgeny = "" | |
vowels_after_vowels = 0.0 | |
vowels_after_consonants = 0.0 | |
number_of_vowels = 0.0 | |
vowels = [ "a", "e", "i", "o", "u", "A", "E", "I", "O", "U" ] | |
with open('yevgenyOnegin.txt') as _yev: | |
yevgeny = _yev.read() | |
# strip all spaces in string | |
yevgeny = re.sub(r'((?!\w).)|\s+', '', yevgeny) | |
print(yevgeny) | |
yev_len = len(yevgeny) | |
for i in range( 1, yev_len ): | |
# Check if the letter is a vowel | |
if not yevgeny[i] in vowels: | |
print(yevgeny[i]) | |
continue | |
number_of_vowels += 1 | |
# Check previous letter is vowel | |
if yevgeny[i - 1] in vowels: | |
vowels_after_vowels += 1 | |
elif not yevgeny[i - 1] in vowels: | |
vowels_after_consonants += 1 | |
# Results | |
print( "Prob of\n" ) | |
print( "Vowel after vowel: %f" % (vowels_after_vowels / yev_len) ) | |
print( "Vowel after consonant: %f" % (vowels_after_consonants / yev_len) ) | |
print( "Vowel: %f" % (number_of_vowels / yev_len) ) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment