methods in computational linguistics ii queens college lecture 7: structuring things
TRANSCRIPT
Methods in Computational Linguistics II
Queens College
Lecture 7: Structuring Things
2
Today
• Functions• Object Oriented Programming• Trees• Graphs• Recursion
3
Functions in Python
• Functions are a way to structure code for reuse.
• Return Values
• Arguments
4
Multiple return values
x, y = swapTwoValues(x, y)
def swapTwoValues(x, y): return y, x
5
Arguments
• The information you give to a method is called an argument
def swapTwoValues(x, y): return y, x
6
Default Arguments
• Arguments can be optional
def myFunction(text, offset=0, prefix=‘hi ’): print ‘ ‘*offset + prefix + text
myFunction(“andrew”)myFunction(“andrew”, 5)
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)
8
Object Oriented Programming
• aka Object Oriented Design.
• Structure functionality into “objects” with associated “member variables” and “methods”.
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.
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.
11
Classes in Python
class MyClass: def __init__(self): self.text = ‘hello’
def __repr__(self): return self.text
also __len__ __str__ and __add__
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 *
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.
14
Shape Example
Shape
Rectangle Triangle Circle
Square
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”
16
Tree Example
5
a
xyz15
The
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.
18
Binary Search Tree Example
5
2
41
9
What does this have to do with Binary Search?
19
Tree class
• What does a tree class require?
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.
21
Graph Example
5
2
41
9
a
a
b
c
cd
22
What does a Graph class require?
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
24
Recursion
• A function that calls itself is called a recursive function.
• A good recursive function must include – A stopping condition– Modification
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.
26
Next Time
• Machine Learning– A primer on machine learning and its role in
Computational Linguistics.– Classification in NLTK (after break)