+ All Categories
Home > Documents > Methods in Computational Linguistics II Queens College Lecture 7: Structuring Things.

Methods in Computational Linguistics II Queens College Lecture 7: Structuring Things.

Date post: 27-Dec-2015
Category:
Upload: robert-hall
View: 219 times
Download: 3 times
Share this document with a friend
Popular Tags:
26
Methods in Computational Linguistics II Queens College Lecture 7: Structuring Things
Transcript
Page 1: Methods in Computational Linguistics II Queens College Lecture 7: Structuring Things.

Methods in Computational Linguistics II

Queens College

Lecture 7: Structuring Things

Page 2: Methods in Computational Linguistics II Queens College Lecture 7: Structuring Things.

2

Today

• Functions• Object Oriented Programming• Trees• Graphs• Recursion

Page 3: Methods in Computational Linguistics II Queens College Lecture 7: Structuring Things.

3

Functions in Python

• Functions are a way to structure code for reuse.

• Return Values

• Arguments

Page 4: Methods in Computational Linguistics II Queens College Lecture 7: Structuring Things.

4

Multiple return values

x, y = swapTwoValues(x, y)

def swapTwoValues(x, y): return y, x

Page 5: Methods in Computational Linguistics II Queens College Lecture 7: Structuring Things.

5

Arguments

• The information you give to a method is called an argument

def swapTwoValues(x, y): return y, x

Page 6: Methods in Computational Linguistics II Queens College Lecture 7: Structuring Things.

6

Default Arguments

• Arguments can be optional

def myFunction(text, offset=0, prefix=‘hi ’): print ‘ ‘*offset + prefix + text

myFunction(“andrew”)myFunction(“andrew”, 5)

Page 7: Methods in Computational Linguistics II Queens College Lecture 7: Structuring Things.

7

Named Arguments

• The specific argument can be named in the call to the function

def myFunction(text, offset=0, prefix=‘hi ’): print ‘ ‘*offset + prefix + text

myFunction(“andrew”)myFunction(“andrew”, prefix=“hello ”)myFunction(text=“andrew”, offset=2)

Page 8: Methods in Computational Linguistics II Queens College Lecture 7: Structuring Things.

8

Object Oriented Programming

• aka Object Oriented Design.

• Structure functionality into “objects” with associated “member variables” and “methods”.

Page 9: Methods in Computational Linguistics II Queens College Lecture 7: Structuring Things.

9

Objects vs. Types

• str and int are special classes• They have methods defined specifically for

them• However, these are called types because

they are built-in to the language.• The distinction is rather blurry.

Page 10: Methods in Computational Linguistics II Queens College Lecture 7: Structuring Things.

10

Classes and Instances

• int is a class• 1 is an instance of the class “int”

• fd = FreqDist()

• FreqDist is a class• fd is an instance of FreqDist.

Page 11: Methods in Computational Linguistics II Queens College Lecture 7: Structuring Things.

11

Classes in Python

class MyClass: def __init__(self): self.text = ‘hello’

def __repr__(self): return self.text

also __len__ __str__ and __add__

Page 12: Methods in Computational Linguistics II Queens College Lecture 7: Structuring Things.

12

Importing a class

from filename import class

The filename specifies the package name of the class. This can be used like any other package.

from filename import *

Page 13: Methods in Computational Linguistics II Queens College Lecture 7: Structuring Things.

13

Inheritance

• Objects can define relationships between objects.

• Membership operations allow for the representation of “has-a”, “has-many”, and arbitrary property relationships.

• Inheritance relationships allow for the representation of “is-a” relationships.

Page 14: Methods in Computational Linguistics II Queens College Lecture 7: Structuring Things.

14

Shape Example

Shape

Rectangle Triangle Circle

Square

Page 15: Methods in Computational Linguistics II Queens College Lecture 7: Structuring Things.

15

Trees

• Binary Trees are a commonly used data structure.

• The core element of a tree is a node

• These nodes can contain values, and include pointers to one or more children, that are differentiated as “left” and “right”

Page 16: Methods in Computational Linguistics II Queens College Lecture 7: Structuring Things.

16

Tree Example

5

a

xyz15

The

Page 17: Methods in Computational Linguistics II Queens College Lecture 7: Structuring Things.

17

Binary Search Trees

• Binary Search trees have the properties– the value (or key), of any node is greater than

the value of its left child– the value of any node is less than the value of

its right child.

Page 18: Methods in Computational Linguistics II Queens College Lecture 7: Structuring Things.

18

Binary Search Tree Example

5

2

41

9

What does this have to do with Binary Search?

Page 19: Methods in Computational Linguistics II Queens College Lecture 7: Structuring Things.

19

Tree class

• What does a tree class require?

Page 20: Methods in Computational Linguistics II Queens College Lecture 7: Structuring Things.

20

Graphs

• Graphs are similar to Trees

• Graphs have:– Nodes– Edges

• Nodes can contain values• Edges connect two notes, and can also

have a value.

Page 21: Methods in Computational Linguistics II Queens College Lecture 7: Structuring Things.

21

Graph Example

5

2

41

9

a

a

b

c

cd

Page 22: Methods in Computational Linguistics II Queens College Lecture 7: Structuring Things.

22

What does a Graph class require?

Page 23: Methods in Computational Linguistics II Queens College Lecture 7: Structuring Things.

23

In class coding

• Using an object in a program• Initializing data in an object• Objects that you can iterate over

• Write a Shape class• Write a Tree class• Write a Graph class

Page 24: Methods in Computational Linguistics II Queens College Lecture 7: Structuring Things.

24

Recursion

• A function that calls itself is called a recursive function.

• A good recursive function must include – A stopping condition– Modification

Page 25: Methods in Computational Linguistics II Queens College Lecture 7: Structuring Things.

25

Example of Recursion

• Print every element of a tree.

• Search for an entry in a tree.

• Print every element of a graph.

• Search in a graph.

Page 26: Methods in Computational Linguistics II Queens College Lecture 7: Structuring Things.

26

Next Time

• Machine Learning– A primer on machine learning and its role in

Computational Linguistics.– Classification in NLTK (after break)


Recommended