Exercises
Contents
Exercises¶
Exercise 5.1: Plural form of nouns¶
a) Make a function that takes an English noun as argument, and returns the plural form of the word. Remember that different sets of words have dissimilar plural forms, and that you have to account for such differences in your function. Test your function on the words “sky”, “woman”, “box”, “bus”, “bush”, “shoe” and “church”. You may also test the function on other words of interest.
b) Can you find words that do not follow the rules of the function defined in exercise 5.1 a)? You may want to test the function for words such as “sheep”, “child” and “loaf”. What do you see?
Exercise 5.2: Conjugation of verbs¶
a) Make a function that takes the infinitive of a verb as input, and returns the past tense of the word. Test your function on the words “climb”, “pour”, “smile”, “pray”, “tap” and “try”.
b) Can you find any words that are not conjugated correctly by the function defined above?
Exercise 5.3: Concordance¶
One useful way of handling text on a computer is to generate a so-called concordance. In linguistics, a concordance lists every occurrence of a word with its context. Often the meaning of a word changes due to its context and we can use concordances to explore such effects.
a) To explore this topic, use the book “Anna Karenina” by Leo Tolstoy (https://www.gutenberg.org/files/1399/1399-0.txt), and print the ten words that occur before and after the word “party” (including the word itself). Do you see any difference in the meaning of the word, based on the context in which it appears?
Hint: It may be useful to make use of the built-in Python function enumerate to both iterate and numerate each item of a list when the list is looped over. The following code line shows how it is implemented.
string = "abcdefg"
for number, letter in enumerate(string):
print(number, letter)
0 a
1 b
2 c
3 d
4 e
5 f
6 g
b) Do the same exercise as above, but try the words “dress” and “suit” instead. Comment the result.
Exercise 5.4: Making dictionaries¶
a) Use the poem “På jorden et sted” given in the previous exercise, and make a dictionary that has the words in the text as key arguments and their respective lengths as corresponding values. Use string formatting to print the words and their lengths to screen. Do you notice a difference between the dictionary and the list of words? (Hint: Print the length of the list of words and the dictionary respectively.)
b) Now use dictionary-operations to change the dictionary obtained in 5.4 a) so that the value corresponding to each word in the dictionary is the number of occurrences of the word in the text, instead of the word’s length.
Hint: You may want to make use of the command
d = {x: 0 for x in d}
which sets all key values of the dictionary to zero. Can you see that this is indeed what happens from the command?