Computer Programming for Speech and Language Processing:
Tutorial 2
1 creating your first module
• Start idle and select file, new from the top menu.
• In the window that opens define a simple function to print your name and then save the file
calling it mytest.py
• To load this module in python:
– First restart the python shell (interpreter) from the shell menu (This restarts python
without quitting idle)
– import your module with the command: import mytest
• Test your function
• Add to this module a function which takes a list of integers and prints only those which are
less than 100.
• Test your function
• Add to the module a function with takes a list and a dictionary as arguments and tells you
whether each item in the list is in the dictionary and if so what its value is.
• Test this function
Work out on the system in which directory/folder the file mytest.py is because this is where you
will need to save other files in this tutorial.
2 Using predefined modules
• From WebCT download the files get word frequencies.py and scotland.txt from
the tutorials/code folder
• Load the module into python and run the function it defines. (Open the module in Idle to see
the code)
• Examine the object returned by the function
3 Adding to the module
• Add to the previous module a function which takes a dictionary as input and prints out the
contents.
• Notice the problems with the way we read the file as discussed in the lecture.
1
4 Harder and much harder problems
I don’t necessarily expect you to be able to do these:
• Adapt your print function to print the output in alphabetical order.
• Adapt your print function to print the output in frequency order
Hint: Read in the textbooks about the different methods available for dictionary objects.