Go to https://www.wordsapi.com/ and copy the API response for any word of your choice.
word_lookup = <the dictionary you copied>
- Choose a word that is not too rare, so that it has various attributes and several meanings.
- For each function you create:
- store the result in some variable (if the function returns something)
- print the result with some label (so that not only you know what it means)
- provide some message in the function if the word_lookup does not have the attribute you are looking for.
In the examples, I will use the word shenanigan.
1. Write a function count_results that returns how many results there were for your word (the list word_lookup["results"])
Input: dictionary word_lookup
Returns: integer - the number of results
2. Write a function pronunciation that
Input: dictionary word_lookup
Returns: "pronunciation" for your word
3. Write a function word_definitions that iterates through all dictionaries in word_lookup["results"] and prints the definitions of the word.
Input: dictionary word_lookup
Returns: returns nothing, prints "definition" for this word, each on a new line
4. Write a function most_common_POS that iterates through all dictionaries in word_lookup["results"], stores all "partOfSpeech" in a list, and returns the partOfSpeech that occurs most frequently (use Counter)
Input: dictionary word_lookup
Returns: partOfSpeech that occurs most frequently
5. For any list (you can take a list of synonyms for your word) write a lambda function with map() to convert the list of lowercase strings to uppercase.
6. Write a function synonyms that iterates through all dictionaries in word_lookup["results"] (for loop), stores all synonyms from "synonyms" in one list (.append or .extend), and returns unique values from that list.
Input: dictionary word_lookup
Returns: unique synonyms
Example result:
{'deviltry', 'rascality', 'trickery', 'guile', 'mischievousness', 'mischief', 'devilment', 'roguishness', 'devilry', 'roguery', 'chicanery', 'wile', 'chicane', 'mischief-making'}
7. Write a function is_type_of that iterates through all dictionaries in word_lookup["results"] (for loop), and for each element in the list "typeOf" (another for loop inside) prints the line "<Word>" is a type of <typeOf>
Input: dictionary word_lookup
Returns: returns nothing, prints all typeOf records.
Example result:
"Shenanigan" is a type of deception.
"Shenanigan" is a type of deceit.
...
Note that "Shenanigan" is capitalized.
8. Write a function syllables that retrieves syllables from "syllables" and returns them as a single string separated by hyphens ('-'.join(...)).
Input: dictionary word_lookup
Returns: returns a string of syllables separated by hyphens.
Example result:
'she-nan-i-gan'
9. Function definition_and_more. Does the same as the function from task 3, but also takes Keyword Arguments additional_parameter, which takes the name of some additional attribute that should also be printed (default is None)
Input: dictionary word_lookup and (optionally) some additional parameter.
Returns: returns nothing, prints the definition and possibly something else.
Example function call:
definition_and_more(word_lookup, additional_parameter = "hasTypes")
Result (formatting is arbitrary, the main thing is that everything you called is included):
* reckless or malicious behavior that causes discomfort or annoyance in others
hasTypes: vandalism, hell, hooliganism, malicious mischief, monkey business, blaze
* the use of tricks to deceive someone (usually to extract money from them)
hasTypes: put-on, fraudulence, hoax, dupery, fraud, humbug, jugglery
10. A function that takes any number of attributes from those in "results" and outputs the values of those attributes.
(If there are no attributes, only word_lookup["word"] is output)
Example function call:
word_anything(word_lookup, 'hasTypes', 'partOfSpeech')
Result (formatting is arbitrary, the main thing is that word_lookup["word"] and the attributes you entered are included):
SHENANIGAN
----------
hasTypes : ['vandalism', 'hell', 'hooliganism', 'malicious mischief', 'monkey business', 'blaze']
partOfSpeech : noun
----------
hasTypes : ['put-on', 'fraudulence', 'hoax', 'dupery', 'fraud', 'humbug', 'jugglery']
partOfSpeech : noun
----------