0% found this document useful (0 votes)
23 views2 pages

Intro to Python for Language Processing

This document provides instructions for several computer programming exercises involving modules in Python: 1. It instructs the reader to create a simple module called "mytest.py" with basic functions, import and test the module, and add additional functions to print lists and check list items against a dictionary. 2. It describes downloading and running a pre-existing module that analyzes word frequencies in a text file, and examining the output object. 3. The reader is tasked with adding a function to the previous module to print a dictionary's contents, and notes potential issues reading files. 4. Two optional harder problems are outlined: adapting the print function to order output alphabetically or by frequency.

Uploaded by

gqwcx9dnpv
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views2 pages

Intro to Python for Language Processing

This document provides instructions for several computer programming exercises involving modules in Python: 1. It instructs the reader to create a simple module called "mytest.py" with basic functions, import and test the module, and add additional functions to print lists and check list items against a dictionary. 2. It describes downloading and running a pre-existing module that analyzes word frequencies in a text file, and examining the output object. 3. The reader is tasked with adding a function to the previous module to print a dictionary's contents, and notes potential issues reading files. 4. Two optional harder problems are outlined: adapting the print function to order output alphabetically or by frequency.

Uploaded by

gqwcx9dnpv
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

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.

You might also like