This shows you the differences between two versions of the page.
ii:labs:s2:01:tasks:04 [2022/04/01 13:04] florin.stancu created |
ii:labs:s2:01:tasks:04 [2024/03/15 13:30] (current) florin.stancu [04. [20p] Word Map] |
||
---|---|---|---|
Line 1: | Line 1: | ||
- | ==== 04. Bonus: File upload ==== | + | ==== 04. [25p] Word Map ==== |
- | As a final task, we want to upload some files using our web application. | + | First, take a couple (3-4) of paragraphs [[https://www.gutenberg.org/files/2701/2701-h/2701-h.htm#link2HCH0001|from here]] and store them inside a ''.txt'' file. |
- | + | ||
- | * Create a new ''upload.html'' form page with a ''<input type=file ...>'' field (and, ofc., a submit button!); | + | |
- | * Note: you need to use ''multipart/form-data'' form encoding for HTTP file uploading to work. | + | |
- | * Create the appropriate Flask route that serves the ''upload.html'' page; | + | |
- | * Check Flask's [[https://flask.palletsprojects.com/en/2.1.x/api/#incoming-request-data|''Request'' object reference]] for accessing the uploaded files; | + | |
- | * Enhance your route function to save the uploaded file on disk (e.g., the ''upload/'' directory); | + | |
- | * Finally, test it! | + | |
+ | * Open the file and read its contents into string; | ||
+ | * Build a ''dict'' with each word from this text (lower cased as key), counting their frequency (as its value); | ||
+ | * Use it to print out the most frequent 5 words in this text (together with their value!), e.g.: | ||
+ | <code> | ||
+ | [('the', 29), ('of', 18), ('and', 15), ('to', 10), ('a', 9)] | ||
+ | </code> | ||