This page is located in archive. Go to the latest version of this course pages.

Spam filter - step 4

Create 3 simple non-adaptive filters, paranoid, naive, and random, and evaluate their quality.


Required features of Python:

  • You should already know how to work with text files.
  • How to get a directory listing using function os.listdir()

You should think about and write down on a piece of paper:

  • How is a spam filter actually used?
  • What is the difference (from the implementation standpoint) between a learning filter and a non-learning filter?
  • Is there any part which all of the spam filters have in common?

Optional (for more advanced programmers): Read how the inheritance of OOP works in Python. You can find more information here:

Simple filters


  • In module simplefilters.py, create 3 classes representing 3 simple filters:
    • NaiveFilter which classifies all the emails as OK,
    • ParanoidFilter which classifies all the emails as SPAM, and
    • RandomFilter which assigns the lables OK and SPAM randomly.
    • Optional: If these 3 filters have some functionality in common, try to extract it into a common ancestor called BaseFilter in module basefilter.py.

Why do we need it?

  • These simple filters will demonstrate the skeleton of the filter and will show the parts common to all filters. We will also have some baseline filters to compare using the functions from step 3.


To facilitate later automatic testing of the final filter, we require your filter to be named MyFilter and defined in module filter.py. In this step, however, you shall create 3 classes called NaiveFilter, ParanoidFilter, and RandomFilter placed in module named simplefilters.py.

A filter will be represented by a class with at least 2 public methods: train() and test(). Filters unable to learn from data will probably have the method train() empty. The rest of the class structure is up to you.

Methods train():

Inputs A path to training corpus, i.e. to a directory with emails, containing also the !truth.txt file. (Irrelevant for the simple filters.)
Outputs None.
Effects Setup of the inner data structures of the filter, so that they can be later used to classify emails using the test() method.

Method test():

Inputs A path to a corpus to be evaluated. (The directory will not contain the !truth.txt file.)
Outputs None.
Effects Creates the !prediction.txt file containing the predictions of the filter.

Evaluating the quality of simple filters

Create a simple script that computes the quality of a specified filter. The script shall:

  • import the class of the chosen filter,
  • call method train() on the first dataset,
  • call method test() on the second dataset,
  • call function compute_quality_for_corpus() for the second corpus,
  • print out the quality, and
  • remove the file !prediction.txt from the corpus.
courses/be5b33prg/homeworks/spam/step4.txt · Last modified: 2018/08/13 09:48 (external edit)