def searchDict(keyword): keyword = keyword.lower() if keyword in data: i = 1 for df in data[keyword]: print(str(i) + '. ' + df) i = i + 1 elif len(match(keyword, data.keys())) > 0: reply = input("Did you mean %s instead ? Y/N " % match(keyword, data.keys())[0]) reply = reply.lower() if reply == 'y': i = 1 for df in data[match(keyword, data.keys())[0]]: print(str(i) + '. ' + df) i = i + 1 else: print("Sorry we don't know the word you are looking for") elif keyword == 'q': quit() else: return print( "The word does'nt exist, kindly double check your spellings")
def search(word): #lowering the word given by the user, so the capital letters will be irrelevant word = word.lower() #trying to understand the word user provide by matching it with existing ones missType = match(word, data.keys(), cutoff=0.8, n=1) #checking if the word exists if word in data: return data[word] elif len(missType) > 0: print("\nDid you mean %s instead?" % missType[0]) decision = input( "\nType 'Y' to accept %s, or type 'N' to enter another word: " % missType[0]) decision = decision.lower() if decision == "y": #changing the global variable of input so the print statement will show the right-spelled word global w w = missType[0] return data[missType[0]] elif decision == "n": return "Ok, let's try another word." else: return "Doesn't understand your entry" else: return "The word doesn't exist. Please double check it "
def get_definitions(word): if word not in data.keys(): # Check for the lower case if word.lower() in data.keys(): word = word.lower() # Check for the upper case elif word.upper() in data.keys(): word = word.upper() # Check for the upper case elif word.title() in data.keys(): word = word.title() # Check for closest match else: max_match_ratio = 0 new_word = "" for key in data.keys(): match_ratio = match(isjunk=None, a=word.lower(), b=key.lower()).ratio() * 100 if match_ratio > 70 and match_ratio > max_match_ratio: max_match_ratio = match_ratio new_word = key # print(key, match_ratio) if max_match_ratio: if True if input("Word not found! Did you mean '" + new_word + "'? (y/n) : ").lower() == "y" else False: word = new_word else: return [] else: # Word not found. Closest match doesn't pass the threshold match percentage print( "The word doesn't exist in the dictionary! Kindly check the spellings!" ) return [] # Display number of definitions found print("Definitions found for", word, ":", len(data[word])) print(sep("-", 80)) return data[word]
while True: query = input('''Please enter your query or \rPress Enter for a random acronym: ''') query = query.upper() # When user doesn't enter anyting if query == "": random_word = random.choice(list(slang.keys())) print(random_word + ": " + slang[random_word]) # When queried string is present in dictionary keys elif query in slang: print(query + ": " + slang[query]) # When queried string matches some chars of slang keys elif len(match(query, slang.keys())) > 0: alternate = match(query, slang.keys())[0] print("Do you mean " + alternate + " ?") decide = input("If yes enter Y or N for No: ") if decide.lower() == "y": print(alternate + ": " + slang[alternate]) else: print("Sorry , we couldn't solve your query") else: print("your query isnt in our database") decide = input("\nWould you like to do more searches? Y or N: ") if decide.lower() == "y": continue
def searchDisc(track_name="", artist="", album="", filename="", search_type="song"): music = MusicFile() if search_type == "file": disc_search = d.search(query=filename) # if no search results, return empty if not len(disc_search) > 0: return music master = find_master(disc_search) if not master: return music # Check if artist name is duplicate, ie. Artist(2), Artist(3) etc multi_check = re.search("\([0-9]\)", master.artists[0].name) if multi_check: start = multi_check.regs[0][0] end = multi_check.regs[0][1] multi_str = master.artists[0].name[start:end] pos_artist = master.artists[0].name.replace(multi_str, "").rstrip() else: pos_artist = master.artists[0].name pos_track = re.sub(pos_artist, "", filename).replace("- ", "").lstrip() pos_track = string.capwords(pos_track) try: track = next(i for i in master.tracklist if i.title.lower() == pos_track.lower()) if track: music.song = track.title music.track_number = track.position else: music.song = pos_track except StopIteration: music.song = match(pos_track, [i.title for i in master.tracklist], cutoff=0.4)[0] # music.song = string.capwords(music.song) # capitalize words in string music.track_number = next(i.position for i in master.tracklist if i.title == music.song) try: # Check if track number is for vinyl or cd int(music.track_number) except ValueError: # If track number is for vinyl, grab integer music.track_number = [ind for ind, i in enumerate(master.tracklist, 1) if i.position == music.track_number][0] music.track_number = str(music.track_number) # convert to string elif search_type == "song": if album: disc_search = d.search(track=track_name, artist=artist, release_title=album) else: disc_search = d.search(track=track_name, artist=artist) # if no search results, return empty if not len(disc_search) > 0: return music master = find_master(disc_search) if not master: return music music.song = match(track_name, [i.title for i in master.tracklist], cutoff=0.4)[0] music.track_number = next(i.position for i in master.tracklist if i.title == music.song) elif search_type == "album": disc_search = d.search(album, type="Release", artist=artist) # if no search results, return empty if not len(disc_search) > 0: return music master = find_master(disc_search) if not master: return music music.artist = master.artists[0].name music.album_artist = master.artists[0].name music.album = master.title music.album_tracks = [s.title for s in master.tracklist] if master.genres and master.styles: music.genres = master.genres + master.styles elif master.genres: music.genres = master.genres elif master.styles: music.genres = master.styles music.year = master.year music.album_cover = master.images[0]['uri'] music.lyrics = get_lyrics(music, filename=filename) # self.test_fun(music) return music
elif m[:3] == "--s": qu = m[4:] mdlf = [x for x in mdl if qu in x.lower()] omdlf = [x for x in omdl if qu in x.lower()] p = 1 mp = int(str(math.ceil((len(mdlf) + .0) / 5))[:-2]) if mp == 0: print("None found.") os.system("pause") mdlf = mdl omdlf = omdl mp = int(str(math.ceil((len(mdlf) + .0) / 5))[:-2]) else: continue else: m = match(m, omdlf, len(omdlf), 0) try: m = m[0] except: m = None print(m) if m in onli: print("Mod is already active.") os.system("pause") elif not m in omdlf: print("Mod doesnt exist or is not in query.") os.system("pause") else: blo = omdlf.index(m) ate = mds[int(blo)] os.chdir(ate)
def similitude(word1, word2): return match(None, word1, word2).ratio()