Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Methods in Computational Linguistics II Queens College Lecture 7: Structuring Things."— Presentation transcript:

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

2 Today Functions Object Oriented Programming Trees Graphs Recursion 1

3 Functions in Python Functions are a way to structure code for reuse. Return Values Arguments 2

4 Multiple return values x, y = swapTwoValues(x, y) def swapTwoValues(x, y): return y, x 3

5 Arguments The information you give to a method is called an argument def swapTwoValues(x, y): return y, x 4

6 Default Arguments Arguments can be optional def myFunction(text, offset=0, prefix=‘hi ’): print ‘ ‘*offset + prefix + text myFunction(“andrew”) myFunction(“andrew”, 5) 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) 6

8 Object Oriented Programming aka Object Oriented Design. Structure functionality into “objects” with associated “member variables” and “methods”. 7

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

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. 9

11 Classes in Python class MyClass: def __init__(self): self.text = ‘hello’ def __repr__(self): return self.text also __len__ __str__ and __add__ 10

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 * 11

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. 12

14 Shape Example 13 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” 14

16 Tree Example 15 5 5 a a xyz 15 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. 16

18 Binary Search Tree Example 17 5 5 2 2 4 4 1 1 9 9 What does this have to do with Binary Search?

19 Tree class What does a tree class require? 18

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. 19

21 Graph Example 20 5 5 2 2 4 4 1 1 9 9 a a b c c d

22 What does a Graph class require? 21

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 22

24 Recursion A function that calls itself is called a recursive function. A good recursive function must include –A stopping condition –Modification 23

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. 24

26 Next Time Machine Learning –A primer on machine learning and its role in Computational Linguistics. –Classification in NLTK (after break) 25


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

Similar presentations


Ads by Google