Reputation: 55
I am trying to test if the word I am iterating through is capital. If the word is not capital and not in the dictionary it should be printed as is. If the word is not capital and not in the dictionary it should be replaced and printed. If the word is capital it needs to be made lowercase to test if it is in the dictionary. If it is capital and in the dictionary the word should be replaced and printed. If it is capital and not in the dictionary the word should be made capital again and printed.
I have tried both of the codes below and they appear to be doing the same thing. It is not changing the capital letters. It is possibly not even iterating over them.
def replace_mode(text_list,misspelling):
update = ''
#loop iterates over the lines and over each word
for line in text_list:
word = line.split(' ')
for key in word:
if key.isupper():
key = key.lower()
if key in misspelling.keys(): #checks if the word is in the dictionary
update = misspelling[key]
print(update.capitalize(), end=(' '))
else:
print(key.capitalize(), end=(' '))
elif key in misspelling.keys():
update = misspelling[key]
print(update, end=(' '))
else:
print(key, end=(' '))
print()
def replace_mode(text_list,misspelling):
update = ''
#loop iterates over the lines and over each word
for line in text_list:
word = line.split(' ')
for key in word:
capital = False
if key.isupper():
capital = True
key = key.lower()
if capital == True:
if key in misspelling.keys(): #checks if the word is in the dictionary
update = misspelling[key]
print(update.capitalize(), end=(' '))
else:
print(key.capitalize(), end=(' '))
if capital == False:
if key in misspelling.keys():
update = misspelling[key]
print(update, end=(' '))
else:
print(key, end=(' '))
print()
--- OUTPUT --- | --- OUTPUT ---
Sitting in the Morni sun | Sitting in the Morning sun
I will be sitting when the evening comes | I will be sitting when the evening comes
Watching the Ships roll in | Watching the Ships roll in
Then I watch them roll away again yeah | Then I watch them roll away again yeah
|
I am sitting on the dock of the bay | I am sitting on the dock of the bay
Watchin the tide roll away ooh | Watching the tide roll away ooh
I am just Siting on the dock of the bay | I am just Sitting on the dock of the bay
Wastin time | Wasting time
Upvotes: 0
Views: 148
Reputation: 2414
Your code is not formatted correctly in your post so it's a little hard to read, but I think that I'm gleaning what you're trying to do. A few tips:
isupper()
checks whether all of the characters in a string are upper-case, but usually a word is considered capitalized if just the first letter is upper-case.
keys()
returns an iterable containing a dictionary's keys, but checking for membership in a dictionary is even easier than that: if key in misspelling:
will check if key
is one of the keys in the dict misspelling
When it comes to bools, you don't have to compare them to other bools in conditionals; instead of if capital == True:
you should write if capital
. And you could use else instead of then checking for a False
condition.
Putting these ideas together:
# Is the first letter of this word capitalized?
capital = key[0].isupper()
# Is the lowercased version of the word in the dictionary?
in_dict = key.lower() in misspelling
if capital and in_dict:
# Word is capitalized and in the dictionary
pass # do something
elif capital and not in_dict:
# Word is capitalized and not in the dictionary
pass # do something
elif capital and in_dict:
# Word is not capitalized and in the dictionary
pass # do something
else:
# Word is not capitalized and not in the dictionary
pass # do something
To update a dictionary, you must access a key and replace it with someone else. update = misspelling[key]
does not do this; it accesses the value corresponding to key
and stores it in the variable update
. What you probably meant to do was misspelling[key] = key
; or something like that, it's unclear from your post what the dictionary is supposed to contain
Upvotes: 1