Search
We are going to create a function, which can read the information from files !truth.txt or !prediction.txt into the dictionary data structure.
!truth.txt
!prediction.txt
Working with a dictionary
items()
eng_to_cz = {'cat': 'kocka', 'dog': 'pes', 'house': 'dum' } for eng, cz in eng_to_cz.items(): print(eng, ',', cz)
Working with (text) files
with
The usage of section ''if __name__ == "__main__":''
Method ''split()'' of string values
Task:
utils.py
read_classification_from_file()
Why do we need it:
Function read_classification_from_file() (in module utils.py) has to conform to the following specifications:
read_classification_from_file(fpath)
SPAM
OK
The function loads a text file contaning a pair of strings per line, separated by single space, like this:
email01 OK email02 OK email03 SPAM email1234 OK
{'email1234': 'OK', 'email03': 'SPAM', 'email02': 'OK', 'email01': 'OK'}
If the file is empty, it returns an empty dictionary.
write_classification_to_file()
Function write_classification_to_file() (in module utils.py) should conform to the following specifications:
write_classification_to_file(cls_dict, fpath)
The following code
>>> cls_dict = {'email1234': 'OK', 'email03': 'SPAM', 'email02': 'OK', 'email01': 'OK'} >>> fpath = '1/!prediction.txt' >>> write_classification_to_file(cls_dict, fpath)
shall create file !prediction.txt in directory 1 (the directory must exist) with the following contents:
1
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.
cls_dict