split string at vowel + surrounding consonants - python

I am new to coding and this is my first try. I want to divide words into syllables from a phonetic language.
Rules to make syllables out of words from a phonetic language:
consider all consonants until the first vowel, consider that vowel.
repeat.
Example:
ma - ri- a
a - le - ksa - nda - r
This is how far I've come :
word = 'aleksandar'
vowels = ['a','e','i','o','u']
consonants = ['b', 'c', 'd', 'f', 'g', 'h', 'j', 'k', 'l', 'm', 'n', 'p', 'q', 'r', 's', 't', 'v', 'w', 'x', 'y', 'z']
for vowel in vowels:
if vowels in word:
index_1 = int(word.index(vowel)) - 1
index_2 = int(word.index(vowel)) + 1
print(word[index_1:index_2])
else:
print(consonants)
IDK what is going wrong, please help!
Thanks in advance :)

This should solve your problem
word = 'aleksandar'
vowels = ['a','e','i','o','u']
new_word = ""
for letter in word:
if letter in vowels:
new_word += letter + "-"
else:
new_word += letter
print(new_word)

i have changed your code little bit and it works perfectly!!!
word = 'aleksandar'
word = list(word)
vowels = ['a','e','i','o','u']
s = ""
syllables = [ ]
for i in range(len(word)):
if word[i] not in vowels:
s = s + word[i]
else:
s = s + word[i]
syllables.append(s)
s = ""
print(syllables)
and the output is :
['a', 'le', 'ksa', 'nda']

Related

Pig Latin Rules with Vowels

so I'm trying to write a code that will check each word in a string if there is a vowel in the beginning. If there is a vowel (upper or lowercase) then it will return the word and "-way" appended. If it begins with a consonant, then it moves the first letter to the back and appends "-ay"
Ex: anchor = anchor-way, computer = omputer-cay
.This is what I have but it seems like it's returning all words with these conditions and it's not checking for vowels. What am I doing wrong?
def pig_latin(text):
say = []
vowel = ['A', 'E', 'I', 'O', 'U', 'a', 'e', 'i', 'o', 'u']
words = text.split()
for vowel in words:
vowel = vowel[0:] + "-way"
say.append(vowel)
if vowel not in words:
vowel = vowel[1:] + "-" + vowel[0] + "ay"
say.append(vowel)
return " ".join(say)
if __name__ == '__main__':
result = pig_latin("hi how are you")
print(result)
When you write for vowel in words:, you aren't checking for vowels in words or doing any comparison. The expression for i in iterable: is a loop that will one-at-a-time set (in this case) i to each item in the iterable. In your case, it is setting the variable vowels to the first, then second, then third, ... item in your list called words. That is, it is overwriting your vowel list you created.
Try something like this.
def pig_latin(text):
say = []
vowel = ['A', 'E', 'I', 'O', 'U', 'a', 'e', 'i', 'o', 'u']
words = text.split()
for word in words: # loop over each word in the list of words
if word[0] in vowel: # compare the first letter of the word against the vowel list
new_word = word + '-way'
else:
new_word = word[1:] + '-' + word[0] + 'ay'
say.append(new_word)
return " ".join(say)
if __name__ == '__main__':
result = pig_latin("hi how are you")
print(result)
there are a few things that you can do.
use the lower() function.
do not confuse vowels for words.
use enumerate to edit the existing list at the correct index.
This is a working version:
def pig_latin(sentance: str):
words = sentance.split()
vowels = ['a','e','i','o','u']
for i, word in enumerate(words):
if word[0].lower() in vowels:
words[i] = word + '-way'
else:
words[i] = word[1:] + '-' + word[0] + 'ay'
return words
r = pig_latin("hi how are you")
print(r)
returns
['i-hay', 'ow-hay', 'are-way', 'ou-yay']

What is the most efficient way to write out my Pig Latin converter?

This is a pig Latin sentence converter and was wondering if I could make my code any smaller. Pig Latin is basically like if a word starts with a vowel then ad yay to the end of the word. If the word starts with a consonant then take the first letter and put it at the end of the word and then add ay to it.
Example: apple = appleyay & tree = reetay
string_name =input('Enter String: ').lower().strip()
string_name =string_name.split()
pig_sentence =[]
for word in string_name:
if word[0] in ['a', 'e', 'i', 'o', 'u']:
pig_word =word+'yay'
pig_sentence.append(pig_word)
else:
pig_word =word[1:] + word[0] + "ay"
pig_sentence.append(pig_word)
pig_sentence =' '.join(pig_sentence)
print(pig_sentence)
The following should also work.
print(" ".join([word+"yay" if word[0] in ['a', 'e', 'i', 'o', 'u'] else word[1:] + word[0] + "ay" for word in input('Enter String: ').lower().strip().split()]))
But, as you can see, this example isn't very readable. You might also want to use:
string_name =input('Enter String: ').lower().strip()
string_name =string_name.split()
pigsentence=" ".join([word+"yay" if word[0] in ['a', 'e', 'i', 'o', 'u'] else word[1:] + word[0] + "ay" for word in string_name])
print(pigsentence)
or just continue using the code you've now, as shorter code isn't always better, and you don't need to really worry about efficiency.

why does the replace character remove both brackets?

Hey guys in new to Python. And I was playing around writing python and I'm stuck.
words = ['w','hello.','my','.name.','(is)','james.','whats','your','name?']
alphabet = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
'''#position of the invalid words i.e the ones that '''
inpos = -1
for word in words:
inpos = inpos + 1
#pass
for letter in word:
#print(letter)
if letter in alphabet:
pass
#print('Valid')
elif letter not in alphabet:
new_word = word.replace(letter,"")
print(word)
print(new_word)
words[inpos] = new_word
print(words)
This code is meant to clean the text (remove all full stops, commas, and other characters)
The problem is when I run it removes the adds the brackets
Heres the output:
Image of output
Can anyone explain why this is happening?
No, it does not add anything. You're printing both old and new word:
print(word)
print(new_word)
so when the new_word is (is the word is still (is).
BTW your code has a logical error: when you remove a character you put back new_word in the list, but word is still the old value. So only the last change for every word will be saved in the list words.

Pig_Latin Translator & for loop

Pig Latin translator is basically an introductory code for many projects, and it involves getting words that start with consonants to move the first letter to the end of the word and add 'ay', and adding only 'ay' to a word that starts with a vowel. I have basically finished the whole project except for some places, and one main thing is getting the program to complete a whole sentence with a for loop, especially in moving to the next list item
I have tried to do simple things such as n+1 at the end of the for loop, and researched online (majority on Stackoverflow)
latin = ""
n = 0
sentence = input ("Insert your sentence ")
word = sentence.split()
first_word = word [n]
first_letter = first_word [0]
rest = first_word [1:]
consonant = ['b', 'c', 'd', 'f', 'g', 'h', 'j', 'k', 'l', 'm', 'n', 'p', 'q', 'r', 's', 't', 'v', 'w', 'x', 'z')
vowel = ['a', 'e', 'i', 'o', 'u')
for one in consonant :
latin = latin + " " + rest + one + "ay"
print (latin)
n + 1
for one in vowel :
latin = latin + " " + first_word +ay
print (latin)
n + 1
There was no error message, however, the computer ran the variable 'one' not as the first (zeroth) letter of the variable first_word, rather, just running it from a - z. Is there anyway to fix this? Thank you
#!/usr/bin/env python
sentence = input("Insert your sentence ")
# don't forget "y" :)
consonants = ['b', 'c', 'd', 'f', 'g', 'h', 'j', 'k', 'l', 'm', 'n', 'p', 'q', 'r', 's', 't', 'v', 'w', 'x', 'y', 'z']
# this empty list will hold our output
output_words = list()
# we'll take the user's input, strip any trailing / leading white space (eg.: new lines), and split it into words, using spaces as separators.
for word in sentence.strip().split():
first_letter = word[0]
# check if the first letter (converted to lower case) is in the consonants list
if first_letter.lower() in consonants:
# if yes, take the rest of the word, add the lower case first_letter, then add "ay"
pig_word = word[1:] + first_letter.lower() + "ay"
# if it's not a consonant, do nothing :(
else:
pig_word = word
# add our word to the output list
output_words.append(pig_word)
# print the list of output words, joining them together with a space
print(" ".join(output_words))
The loop loops over each word in the sentence - no need to count anything with n. There's also no need to loop over the consonants or the vowels, all we care about is "does this word start with a consonant" - if yes, modify it as per your rules.
We store our (possibly) modified words in an output list, and when we're done, we print all of them, joined by a space.
Note that this code is very buggy - what happens if your user's input contains punctuation?
I opehay hattay elpshay, eyuleochoujay

How do I count a specific character that is surrounded by random letters

I'm trying to count punctuations that are: apostrophe (') and hyphen (-) using dictionaries. I want to see if I can pull this off using list/dictionary/for loops and boolean expressions. These punctuations MUST ONLY BE COUNTED if they are surrounded by any other letters! E.g. jack-in-a-box (that is 3 hyphens) and shouldn't (1 apostrophe). These letters can be anything from a to z. Also, since this is part of an assignment, no modules/libraries can be used. I'm out of ideas and don't know what to do.
Any help would be greatly appreciated.
This is what I tried: but I get an KeyError: 0
def countpunc2():
filename = input("Name of file? ")
text = open(filename, "r").read()
text = text.lower() #make all the words lowercase (for our convenience)
for ch in '!"#$%&()*+./:<=>?#[\\]^_`{|}~':
text = text.replace(ch, ' ')
for ch in '--':
text = text.replace(ch, ' ')
words = text.split('\n') #splitting the text for words
wordlist = str(words)
count = {} #create dictionary; the keys/values are added on
punctuations = ",;'-"
letters = "abcdefghijklmnopqrstuvwxyz"
for i, char in enumerate(wordlist):
if i < 1:
continue
if i > len(wordlist) - 2:
continue
if char in punctuations:
if char not in count:
count[char] = 0
if count[i-1] in letters and count[i+1] in letters:
count[char] += 1
print(count)
UPDATE:
I changed the code to:
def countpunc2():
filename = input("Name of file? ")
text = open(filename, "r").read()
text = text.lower() #make all the words lowercase (for our convenience)
for ch in '!"#$%&()*+./:<=>?#[\\]^_`{|}~':
text = text.replace(ch, ' ')
for ch in '--':
text = text.replace(ch, ' ')
words = text.split('\n') #splitting the text for words
wordlist = str(words)
count = {} #create dictionary; the keys/values are added on
punctuations = ",;'-"
letters = "abcdefghijklmnopqrstuvwxyz"
for i, char in enumerate(wordlist):
if i < 1:
continue
if i > len(wordlist) - 2:
continue
if char in punctuations:
if char not in count:
count[char] = 0
if wordlist[i-1] in letters and wordlist[i+1] in letters:
count[char] += 1
print(count)
While it is giving me an output it is not correct.
Sample file: https://www.dropbox.com/s/kqwvudflxnmldqr/sample1.txt?dl=0
The expected results must be: {',' : 27, '-' : 10, ';' : 5, "'" : 1}
I'd probably keep it simpler than that.
#!/usr/bin/env python3
sample = "I'd rather take a day off, it's hard work sitting down and writing a code. It's amazin' how some people find this so easy. Bunch of know-it-alls."
punc = "!\"#$%&\'()*+,-./:;<=>?#[\\]^_`{|}~"
letters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
d = {}
for i, char in enumerate(sample):
if i < 1:
continue
if i > len(sample) - 2:
continue
if char in punc:
if char not in d:
d[char] = 0
if sample[i - 1] in letters and sample[i + 1] in letters:
d[char] += 1
print(d)
Output:
{"'": 3, ',': 0, '.': 0, '-': 2}
Dunno where you're getting the ";" from. Also your comma has a space next to it.. so it doesn't count here.. if that does count add a space to the letters variable.
Explanation of what's happening:
We initiate a dict and read in sample text as sample and iterate it character by character, using enumerate to play with the indexes. If it is too close to the end or start to qualify, we skip it.
I check the character before and after the one we're at using the i variable from enumerate. and add to it's count if it qualifies.
NOTE: despite the shebang, this code works in python2
You could map the characters of your input string into 3 categories: alphabetic(a), punctuation(p) and spaces(s). Then group them in triples (sequences of 3 characters). From those, isolate the a-p-a triples and count the number of distinct punctuation characters.
for example:
string="""jack-in-a-box (that is 3 hyphens) and shouldn't (1 apostrophe)."""
categ = [ "pa"[c.isalpha()] if c != " " else "s" for c in string ]
triples = [ triple for triple in zip(categ,categ[1:],categ[2:]) ]
pChars = [ p for p,triple in zip(s[1:],triples) if triple==("a","p","a") ]
result = { p:pChars.count(p) for p in set(pChars) }
print(result) # {"'": 1, '-': 3}
If you're not allowed to use isAlpha() or zip(), you can code the equivalent using the in operator and for loops.
Here is an example that does it in a very spelled out way:
end_cap_characters = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']
special_characters = [";", ":", "'", "-", ","]
def count_special_characters(in_string):
result = {}
for i in range(1, len(in_string) - 1):
if in_string[i - 1] in end_cap_characters:
if in_string[i + 1] in end_cap_characters:
if in_string[i] in special_characters:
if in_string[i] not in result:
result[in_string[i]] = 1
else:
result[in_string[i]] +=1
return result
print(count_special_characters("jack-in-the-box"))
print(count_special_characters("shouldn't"))
print(count_special_characters("jack-in-the-box, shouldn't and a comma that works,is that one"))
Output:
{'-': 3}
{"'": 1}
{'-': 3, "'": 1, ',': 1}
Obviously this can be condensed, but I will leave that as an exercise for you ;).
Update
Based on your edited question and posted code, you need to update the line:
if count[i-1] in letters and count[i+1] in letters:
to:
if wordlist[i-1] in letters and wordlist[i+1] in letters:

Categories