Presentation is loading. Please wait.

Presentation is loading. Please wait.

LING 388: Computers and Language

Similar presentations


Presentation on theme: "LING 388: Computers and Language"— Presentation transcript:

1 LING 388: Computers and Language
Lecture 12

2 For those using Python via an IDLE
If you're wondering what directory you are in (by default): import os os.getcwd() If you want to change the working directory,use: os.chdir(path)

3 Last time: Homework 5 revisited
h/story/ why- our-ancestors-drilled- holes-in-each-others- skulls >>> import re >>> from collections import Counter >>> f = open("trepanation.txt") >>> s = f.read() >>> c = Counter([re.sub("[,.?;:\"'!]","",w) for w in s.split()]) >>> c.most_common(20) [('the', 114), ('of', 95), ('in', 38), ('a', 34), ('and', 32), ('to', 30), ('trepanation', 29), ('The', 24), ('been', 23), ('that', 23), ('skulls', 23), ('had', 22), ('have', 20), ('was', 19), ('trepanned', 16), ('skull', 16), ('all', 16), ('is', 16), ('trepanations', 15), ('were', 15)] >>> c['ritual'] 7 >>> c['rituals'] 0

4 Python numbers revisited
page 60: see factorial.py Python 3.x Python 2.7 ( )

5 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")

6 Python: Files writing-files

7 Sample text file Course webpage: falconheavylaunch.txt
environment Course webpage: falconheavylaunch.txt

8 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?

9 Python eval() Think of eval(input()) as being the same as the user typing directly at the Python console …

10 Python numbers revisited
factorial2.py

11 argv[1] and argv[2] ignored
Python argv List of arguments from the command line: what's argv[0] then? test.py argv[1] and argv[2] ignored

12 Formatted Output: floating point (f)
$100 principal for 10 years at 4% (0.04) compound interest = $148.02

13 Formatted Output Lots of ways: (Old way)
notation comes originally from the C Programming Language: sprintf function

14 Now use instead: <template-string>.format(<values>)
Formatted Output Now use instead: <template-string>.format(<values>) %<width>.<precision><type> <type> = d, f, s <width> = minimum number of characters; right-justified by default, -width => left-justified 0 = as wide as needed <precision> = number of places after decimal point e.g. 02d two digits wide, pad with 0 if needed

15 Formatted Output: examples
Function str() coerces integer or float above into type 'str'

16 Formatted Output Newer way:
formatting Use {} for each argument (can be numbered, e.g. {0}, {1},… or referenced by keyword {x}) {:width}

17 Formatted Output {0:..} and {1:…} index into the format arguments
0 in {0:..} can be omitted but not the colon


Download ppt "LING 388: Computers and Language"

Similar presentations


Ads by Google