+ All Categories
Home > Documents > 1 Introduction to Python LING 5200 Computational Corpus Linguistics Martha Palmer.

1 Introduction to Python LING 5200 Computational Corpus Linguistics Martha Palmer.

Date post: 21-Jan-2016
Category:
Upload: ashley-park
View: 223 times
Download: 0 times
Share this document with a friend
Popular Tags:
22
1 Introduction to Python LING 5200 Computational Corpus Linguistics Martha Palmer
Transcript
Page 1: 1 Introduction to Python LING 5200 Computational Corpus Linguistics Martha Palmer.

1

Introduction to Python

LING 5200Computational Corpus LinguisticsMartha Palmer

Page 2: 1 Introduction to Python LING 5200 Computational Corpus Linguistics Martha Palmer.

LING 5200, 2006 BASED on Kevin Cohen’s LING

52002

What's a programming language? Structural elements Way of converting a text file to

instructions for the machine

Page 3: 1 Introduction to Python LING 5200 Computational Corpus Linguistics Martha Palmer.

LING 5200, 2006 BASED on Kevin Cohen’s LING

52003

What's a programming language? lexicon syntax

Vs. natural languages: no ambiguity

Page 4: 1 Introduction to Python LING 5200 Computational Corpus Linguistics Martha Palmer.

LING 5200, 2006 BASED on Kevin Cohen’s LING

52004

What does a program do?

Take in data (input) Do something with it (processing) Produce output

Page 5: 1 Introduction to Python LING 5200 Computational Corpus Linguistics Martha Palmer.

LING 5200, 2006 BASED on Kevin Cohen’s LING

52005

What does a program do?

Input your regex one or more files switches

For each line in each file, determine whether or not it matches your regex

Tell you about it

egrep '^[0-9]+\/' epw.cd

Page 6: 1 Introduction to Python LING 5200 Computational Corpus Linguistics Martha Palmer.

LING 5200, 2006 BASED on Kevin Cohen’s LING

52006

Producing output

print "hello, world"

Page 7: 1 Introduction to Python LING 5200 Computational Corpus Linguistics Martha Palmer.

LING 5200, 2006 BASED on Kevin Cohen’s LING

52007

Producing output

print "hello, world"

verb

Page 8: 1 Introduction to Python LING 5200 Computational Corpus Linguistics Martha Palmer.

LING 5200, 2006 BASED on Kevin Cohen’s LING

52008

Producing output

print "hello, world"

noun (object)

Page 9: 1 Introduction to Python LING 5200 Computational Corpus Linguistics Martha Palmer.

LING 5200, 2006 BASED on Kevin Cohen’s LING

52009

Producing output

#!/usr/local/bin/python

print "hello, world"

Page 10: 1 Introduction to Python LING 5200 Computational Corpus Linguistics Martha Palmer.

LING 5200, 2006 BASED on Kevin Cohen’s LING

520010

Producing output

Filename: helloWorld.py What do the file's permissions need to

be?

Page 11: 1 Introduction to Python LING 5200 Computational Corpus Linguistics Martha Palmer.

LING 5200, 2006 BASED on Kevin Cohen’s LING

520011

Producing output

babel>python helloWorld.py

hello, worldbabel>

Page 12: 1 Introduction to Python LING 5200 Computational Corpus Linguistics Martha Palmer.

LING 5200, 2006 BASED on Kevin Cohen’s LING

520012

Producing output

#!/usr/local/bin/python

print "hello, world\n";

"escape" character

Page 13: 1 Introduction to Python LING 5200 Computational Corpus Linguistics Martha Palmer.

LING 5200, 2006 BASED on Kevin Cohen’s LING

520013

Producing output

\t tab \n "newline"

Page 14: 1 Introduction to Python LING 5200 Computational Corpus Linguistics Martha Palmer.

LING 5200, 2006 BASED on Kevin Cohen’s LING

520014

Comments

#1 use for comments: adding notes to yourself/other programmers

# the purpose of this program

# is to print "hello, world" to

# the screen.

# author: [email protected]

# 303-492-1300

Page 15: 1 Introduction to Python LING 5200 Computational Corpus Linguistics Martha Palmer.

LING 5200, 2006 BASED on Kevin Cohen’s LING

520015

Comments

#!/usr/local/bin/perl

# the purpose of this program

# is to print "hello, world" to

# the screen.

# author:[email protected]

# 303-492-1300

# do the actual printing

print "hello, world\n"

"Commenting" your code

Page 16: 1 Introduction to Python LING 5200 Computational Corpus Linguistics Martha Palmer.

LING 5200, 2006 BASED on Kevin Cohen’s LING

520016

Comments

Other use: causing Python to ignore a line

# print "hello, world\n";print "goodbye, cruel world\n";

"Commenting out" a line of code

Page 17: 1 Introduction to Python LING 5200 Computational Corpus Linguistics Martha Palmer.

LING 5200, 2006 BASED on Kevin Cohen’s LING

520017

Comments

Own-line or end-of-line formats

# print it

print "hello, world\n"

print "hello, world\n" # print it

Page 18: 1 Introduction to Python LING 5200 Computational Corpus Linguistics Martha Palmer.

LING 5200, 2006 BASED on Kevin Cohen’s LING

520018

Producing output

I'd like to print something different every once in a while…

$name = “Martha”

print name

Page 19: 1 Introduction to Python LING 5200 Computational Corpus Linguistics Martha Palmer.

LING 5200, 2006 BASED on Kevin Cohen’s LING

520019

Variables

Name Contents Location in memory

Page 20: 1 Introduction to Python LING 5200 Computational Corpus Linguistics Martha Palmer.

LING 5200, 2006 BASED on Kevin Cohen’s LING

520020

Variables

Name (name) Contents (Kinder) Location in memory (13025)

$name

Page 21: 1 Introduction to Python LING 5200 Computational Corpus Linguistics Martha Palmer.

LING 5200, 2006 BASED on Kevin Cohen’s LING

520021

Using +

name = “Martha"

print "hello, ” + name

Page 22: 1 Introduction to Python LING 5200 Computational Corpus Linguistics Martha Palmer.

LING 5200, 2006 BASED on Kevin Cohen’s LING

520022

Python for Linguists

http://mime.colorado.edu/~bethard/Python%20for%20Linguists/Python-1.pdf


Recommended