This is a follow-up to Searching online English dictionaries:
import webbrowser
import random
available_dicts = [
('Merriam-Webster Dictionary', 'https://www.merriam-webster.com/dictionary/', '%20'),
('Oxford Dictionary', 'https://en.oxforddictionaries.com/definition/', '_'),
('Camberidge Dictionary', 'https://dictionary.cambridge.org/dictionary/english/', '-'),
('Collins Dictionary','https://www.collinsdictionary.com/dictionary/english/', '-'),
('Longman Dictionary of Contemporary English', 'https://www.ldoceonline.com/dictionary/', '-'),
('The American Heritage Dictionary of the English Language', 'https://ahdictionary.com/word/search.html?q=', '%20'),
('Almaany English-Arabic Dictionary', 'www.almaany.com/en/dict/ar-en/', '-'),
('Wordreference English-Arabic Dictionary', 'http://www.wordreference.com/enar/', '%20')
]
instructions = """
All of the ablove are the available online English dictionaries.
To select a dictionary, type its corresponding numeral.
To select more than one dictionary, seperate the numerals with ",".
To select all of them, type "*".
To let the program select a dictionary randomly, type "?".
Do not type the quotation marks.
Press Enter to exit.\n"""
class OnlineDictionary:
"""Represents an online dictionary.
Attributes:
name: str representing the dictionary's name
url: str representing the URL used to look the query up
sep: str used to seperate the parts of the query if there are white spaces between them
"""
def __init__(self, name, url, sep):
"""Initializes an OnlineDictionary object.
name: str
url: str
sep: str
"""
self.name = name
self.url = url
self.sep = sep
def search_for(self, query):
"""Searches the dictionary for a given query.
query: str
"""
query = self.sep.join(query.split())
query_url = self.url + query
webbrowser.open(query_url)
def print_data(available_dicts, instructions):
"""Prints the available dictionaries, their corresponding numerals and the instructions.
available_dicts: list of tuples
instructions: str
"""
for numeral, (name, _, _) in enumerate(available_dicts):
print(numeral, name)
print(instructions)
def get_dicts(available_dicts):
"""Asks the user for which dictionary or dictionaries to use.
available_dicts: list of tuples
Returns: tuple
"""
dicts_to_use = input('Specify which dictionary or dictionaries to use.\n')
if dicts_to_use == '':
exit()
elif dicts_to_use == '*':
dicts_to_use = available_dicts
elif dicts_to_use == '?':
dicts_to_use = [random.choice(available_dicts)]
else:
try:
dicts_to_use = dicts_to_use.replace(' ', '').split(',')
numerals = []
for num in dicts_to_use:
num = int(num)
if num not in range(len(available_dicts)):
raise ValueError
numerals.append(num)
dicts_to_use = [available_dicts[num] for num in set(numerals)] # set class is used to get rid of duplicates
except ValueError:
return get_dicts(available_dicts)
return dicts_to_use
def use_continuous_mode():
"Asks the user for what mode to use."
message = """In Continuous Mode the selected dictionary or dictionaries are
used for your next queries without asking you to select any again.
Do you want to use it? Type "Yes" or "No", ommiting the quotation marks.\n"""
mode = input(message).lower()
return exit() if mode == '' else True if mode == 'yes' else False if mode == 'no' else use_continuous_mode()
def get_query():
"""Asks the user for what to search for."""
query = input('Type something to search for.\n')
return exit() if query == '' else query
def search_selected_dicts(selected_dicts, query):
"""Searches the selected dictionaries for the given query
by making a list of OnlineDictionary objects out of them.
selected_dicts: list of tuples
query: str
"""
online_dicts = [OnlineDictionary(name, url, sep) for name, url, sep in selected_dicts]
for online_dic in online_dicts:
print('Searching %s for "%s"...' % (online_dic.name, query))
online_dic.search_for(query)
print('\nFinished searching. Check your Web browser.\n')
def main():
print_data(available_dicts, instructions)
continuous_mode = use_continuous_mode()
if continuous_mode:
selected_dicts = get_dicts(available_dicts)
while True:
query = get_query()
print()
search_selected_dicts(selected_dicts, query)
else:
while True:
selected_dicts, query = get_dicts(available_dicts), get_query()
print()
search_selected_dicts(selected_dicts, query)
if __name__ == '__main__':
main()