LING 388: Computers and Language Lecture 9
Administrivia Reminder: next week TA Patricia Lee will lead the class Lecture 8 slides updated with examples from last class Today topic: file handling
Python: Files Like all other programming languages, uses a file handle, called file variable: open() infile = open("file.txt","r") outfile = open("results.txt,"w")
Python: Files https://docs.python.org/3/tutorial/inputoutput.html#reading-and- writing-files
Sample text file Course webpage: falconheavylaunch.txt http://www.bbc.com/news/science- environment-42969020 Course webpage: falconheavylaunch.txt
Class Exercise Download falconheavylaunch.txt to your computer. Answer the following questions. How many characters are there in the article? How many lines of text are there in the article? How many lines excluding blank lines? How many words are there in the article? How many characters per word? What are the most common words? Recall from lecture 7: from collections import Counter c = Counter() c.most_common()
Answers to class exercise
Class Example: Questions 1 and 2
Class Example: Question 3
Class Example: Question 4
Class Example: Question 5 4.88 characters per word
Class Example: Question 6 Closed class items only in most common 10. First open class items to appear are: rocket, Falcon, Jeavy, aircraft, Elon Recall text summarizati on?
Python: Files Writing (assuming you've opened a file for writing):
Python: json JSON = JavaScript Object Notation 7.2.2. Saving structured data with json https://docs.python.org/3/library/json.ht ml#module-json
Class Example: Python: json Explanation: we loaded it back in as a dict. most_common() is a method of Counter