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

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
Next revision Both sides next revision
courses:be5b33prg:homeworks:spam:step1 [2015/10/15 14:47]
xposik [Preparation]
courses:be5b33prg:homeworks:spam:step1 [2015/11/26 09:12]
xposik
Line 10: Line 10:
 =====Preparation===== =====Preparation=====
  
-++++ Working with a //dictionary// |+++++ Working with a dictionary |
   * See {[a4b99rph:​Pilgrim2009]},​ chapter [[http://​www.diveinto.org/​python3/​native-datatypes.html#​dictionaries|2.7]],​ or {[a4b99rph:​Wentworth2012]},​ chapter [[http://​openbookproject.net/​thinkcs/​python/​english3e/​dictionaries.html|20]]).   * See {[a4b99rph:​Pilgrim2009]},​ chapter [[http://​www.diveinto.org/​python3/​native-datatypes.html#​dictionaries|2.7]],​ or {[a4b99rph:​Wentworth2012]},​ chapter [[http://​openbookproject.net/​thinkcs/​python/​english3e/​dictionaries.html|20]]).
   * How to create an empty dictionary.   * How to create an empty dictionary.
Line 21: Line 21:
 </​code>​ </​code>​
 ++++ ++++
-++++Working with (text) files|+ 
 +++++ Working with (text) files |
   *(viz {[a4b99rph:​Pilgrim2009]},​ chapter [[http://​diveinto.org/​python3/​files.html|11]],​ or {[a4b99rph:​Wentworth2012]},​ chapter [[http://​openbookproject.net/​thinkcs/​python/​english3e/​files.html|13]]).   *(viz {[a4b99rph:​Pilgrim2009]},​ chapter [[http://​diveinto.org/​python3/​files.html|11]],​ or {[a4b99rph:​Wentworth2012]},​ chapter [[http://​openbookproject.net/​thinkcs/​python/​english3e/​files.html|13]]).
   * How to open and close a text file.   * How to open and close a text file.
Line 27: Line 28:
   * Reading a file line by line.   * Reading a file line by line.
   * Reading the whole file contents as a single string.   * Reading the whole file contents as a single string.
-++++The usage of section ​<​code>​if __name__ == "​__main__":​</​code>​|+++++ 
 + 
 +++++ The usage of section ​''​if __name__ == "​__main__":​'' ​|
   * See {[a4b99rph:​Pilgrim2009]},​ chapter [[http://​diveinto.org/​python3/​your-first-python-program.html#​runningscripts|1.10]]).   * See {[a4b99rph:​Pilgrim2009]},​ chapter [[http://​diveinto.org/​python3/​your-first-python-program.html#​runningscripts|1.10]]).
 ++++ ++++
-++++Method ''​split()''​ of string values|+ 
 +++++ Method ''​split()''​ of string values |
   * See the Python docs for [[http://​docs.python.org/​py3k/​library/​stdtypes.html?​highlight=split#​str.split|str.split()]])   * See the Python docs for [[http://​docs.python.org/​py3k/​library/​stdtypes.html?​highlight=split#​str.split|str.split()]])
 ++++ ++++
Line 66: Line 70:
  
 > {{page>​courses:​a4b99rph:​internal:​cviceni:​spam:​tyden07#​read_classification_from_file&​editbtn}} > {{page>​courses:​a4b99rph:​internal:​cviceni:​spam:​tyden07#​read_classification_from_file&​editbtn}}
 +
 +
 +===== Writing classification (predictions) to a file =====
 +
 +Task:
 +  * In module ''​utils.py'',​ create function ''​write_classification_to_file()''​ that will write the (usually predicted) mail classes to a text file.
 +
 +Why do we need it:
 +  * The function will come handy when writing the filter; it can be used to create the ''​!prediction.txt''​ file. 
 +
 +==== Specifications ====
 +Function ''​write_classification_to_file()''​ (in module ''​utils.py''​) should conform to the following specifications: ​
 +
 +^ ''​write_classification_to_file(cls_dict,​ fpath)''​ ^^
 +^  Inputs ​ | (1) dictionary containing the email file names as keys, and email classes (''​SPAM''​ or ''​OK''​) as values.
 +^          | (2) The path to the text file that shall be created. ​ |
 +^  Output ​ | None.  |
 +
 +The following code
 +
 +<code python>
 +>>>​ cls_dict = {'​email1234':​ '​OK',​ '​email03':​ '​SPAM',​ '​email02':​ '​OK',​ '​email01':​ '​OK'​}
 +>>>​ fpath = '​1/​!prediction.txt'​
 +>>>​ write_classification_to_file(cls_dict,​ fpath)
 +</​code>​
 +
 +shall create file ''​!prediction.txt''​ in directory ''​1''​ (the directory must exist) with the following contents:
 +
 +<​code>​
 +email01 OK
 +email02 OK
 +email03 SPAM
 +email1234 OK
 +</​code>​
 +
 +The actual order of individual rows in the file is not important.
 +
 +If the ''​cls_dict''​ is empty, the function shall create an empty file.
 +
  
courses/be5b33prg/homeworks/spam/step1.txt · Last modified: 2015/11/30 15:29 by xposik