Verizon 3:49 PM Attachment Title froan sets to lets will consume Phase reher to
ID: 3739596 • Letter: V
Question
Verizon 3:49 PM Attachment Title froan sets to lets will consume Phase reher to ame your files hwGPart1.py and hw6Part2py A always, make sare you folow the prog stcture gidelors You will e graded on pegta coerectness as well as good progam structure eoler as wel that we will be coting to test howork for similarityo follow o d las kr the acorptalle kwilm of colliration. You cm downkad the guidla?hm theres antes ection in Piazaa if you need a eefreshve. Note that this incudes usingso dse's eode froma peevious mer. Malan wue the code you henit i truly you ow How does astoccerect work? We use it almost every day. The idea ebind it is veey simple If y type a word that is not in my dictionaryI go throagh all posible ways you could have mispelnda Fair warning: we will ve a simlified veion of autocorect in this part Yot et. W wil revisit the sae probils in Howok 7 with a moxe compes solution wing dictio You will be able to euse most ofwhat you write here at that tne so spend soe tne to stwture your ode well, aul ue fwtions to male ? mcee milular Think aluut E? fter you od a k froen now trying to ad and modify your code, and be nice to that peron To solve thi roless your peogram will read the ase of two files the fist costaining a ditionary wods and the ond costaining a list of wods to autoconect Both files have a singde woed pr lne. be done wh sets our progr wll then go thaough eway single wond in the input file and autocurrect each woed To coret a single woed, you will coidr the foowing od and go on to the net word DROP H the word is sot foand coidee all posbile was to daop a single eter from the woed ?any od tarm are ? the dictionary, then print the word as our ected with drup, and stop. Foe example, guinecux can be dad to qiscunx by dropping froen the wsed. E any of one of thrse in the dictionary, the pr int the wed as corected wit wap, and stop. For example serednipity can be tranedonaed to sermdipity by ng the alphabet for this part Emy of thw aar in the dictinary, thrn print it as corrected ?ith teplar, aad stup. F ample, sockpolager can be changed to socikdslager by replacing p with d NO MATCH If the woed is not conected by any of the above stee, prist NO MATCH II thete are suipe mateles in any step eet us the match thalt is smallest eicograpkically. Tis Implesment the potential ways to do aatocomction in the oeder gives to you Match. Drop, Swap Replace. Yo will get plenty of partial cedit foe all of your ponihle matdes When peing woeds and matcbes, Sormal them to to 15 chazactersExplanation / Answer
hw6Part1.py
letters = [ 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k','l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'y', 'z' ]
def make_set(file):
#makes a set of all words from the input file
words=set()
file=open(file)
for line in file:
words.add(line.strip())
return words
def make_list(file):
words=[]
file=open(file)
for line in file:
words.append(line.strip())
return words
def found(word,words):
#returns true if the element is in the set
return word in words
def drop(word,words):
#drops each letter from the word and checks if it is in the set
for i in range(len(word)):
drop=word[0:i]+word[i+1:]
if drop in words:
return True, drop
return False, ""
def swap(word,words):
#swaps two letters int the word and checks if it is in the set
word=list(word)
for i in range(len(word)-1):
working_copy=word[:]
temp=working_copy[i]
working_copy[i]=working_copy[i+1]
working_copy[i+1]=temp
swap= "".join(working_copy)
if swap in words:
return True, swap
return False, ""
def replace(word,words):
#replaces each letter of the word with each letter of the alphabet, then checks if it is in the set
# letters=list(string.ascii_lowercase)
word=list(word)
for i in range(len(word)):
for letter in letters:
working_copy=word[:]
working_copy[i]=letter
replace="".join(working_copy)
if replace in words:
return True, replace
return False, ""
if __name__ == '__main__':
dictionary=input("Dictionary file => ").strip()
print(dictionary)
bad_words=input("Input file => ").strip()
print(bad_words)
# dictionary="words_10pt.txt"
# bad_words="input_words.txt"
dictionary=make_set(dictionary)
bad_words=make_list(bad_words)
seen=set()
for word in bad_words:
if word in seen:
continue
seen.add(word)
if found(word,dictionary):
print("{:<15} -> {:<15} :FOUND".format(word,word))
continue
dropped,fixed=drop(word,dictionary)
if dropped:
print("{:<15} -> {:<15} :DROP".format(word,fixed))
continue
swapped,fixed=swap(word,dictionary)
if swapped:
print("{:<15} -> {:<15} :SWAP".format(word,fixed))
continue
replaced,fixed=replace(word,dictionary)
if replaced:
print("{:<15} -> {:<15} :REPLACE".format(word,fixed))
continue
print("{:<15} -> {:<15} :NO MATCH".format(word,word))
hw6Part2.py
import textwrap
def get_info(file,title):
#finds the title and a set of beasts for the given input
file=open(file)
for line in file:
if title in line.split("|")[0]:
title=line.split("|")[0]
beasts=set(line.strip().split("|")[1:])
return title, beasts
def other_titles(file_list,beasts):
#finds the other titles with the beasts in common
titles_common=set()
for i in range(len(file_list)):
for beast in beasts:
if beast in file_list[i][1:]:
titles_common.add(file_list[i][0])
titles_common=sorted(titles_common)
str_titles="Other titles containing beasts in common: "+", ".join(titles_common)
str_titles=textwrap.wrap(str_titles)
return str_titles
def make_list(file):
file_list=[]
file=open(file)
for line in file:
file_list.append(line.strip().split("|"))
return file_list
def remove_line(i, stuff):
return stuff[0:i]+stuff[i+1:]
def unique(file_list,beasts):
#finds the unique beasts for the title
for i in range(len(file_list)):
beasts-=set(file_list[i][1:])
unique_beasts=sorted(beasts)
str_unique="Beasts appearing only in this title: "+", ".join(unique_beasts)
str_unique=textwrap.wrap(str_unique)
return str_unique
if __name__ == '__main__':
while True:
file="titles.txt"
input_title=input("Enter a title (stop to end) => ")
print(input_title)
input_title=input_title.title().strip() #matches to title case of text file
if input_title=="Stop":
print()
break
if get_info(file,input_title) == None:
print(" This title is not found!")
continue
title,beasts=get_info(file,input_title) #sets the two variables to the output of the function
beasts_sorted=sorted(beasts) #sorted alphabetically to match output
beasts_str="Beasts in this title: "+", ".join(beasts_sorted)
beasts_str=textwrap.wrap(beasts_str) #makes the line not a long mess
file_list=make_list(file)
#finds the output of the other_titles function to format printing later
#titles_str=other_titles(file_list,beasts_sorted)
unique_str=""
title_str=""
for i in range(len(file_list)):
if title in file_list[i][0]:
titles_str=other_titles(remove_line(i,file_list),beasts)
unique_str=unique(remove_line(i,file_list),beasts)
break
print()
print("Found the following title: {}".format(title))
for line in beasts_str:
print(line)
print()
for line in titles_str:
print(line)
print()
for line in unique_str:
print(line)
print()
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.