Structured programming 3 Day 33 LING 681.02 Computational Linguistics Harry Howard Tulane University.

Slides:



Advertisements
Similar presentations
The Web Warrior Guide to Web Design Technologies
Advertisements

Strings and regular expressions Day 10 LING Computational Linguistics Harry Howard Tulane University.
 2005 Pearson Education, Inc. All rights reserved Introduction.
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
Lists Introduction to Computing Science and Programming I.
Working with JavaScript. 2 Objectives Introducing JavaScript Inserting JavaScript into a Web Page File Writing Output to the Web Page Working with Variables.
XP 1 Working with JavaScript Creating a Programmable Web Page for North Pole Novelties Tutorial 10.
 2003 Prentice Hall, Inc. All rights reserved. Customized by Sana Odeh for the use of this class. 1 Introduction to Computers and Programming in JAVA.
Modules, Hierarchy Charts, and Documentation
About the Presentations The presentations cover the objectives found in the opening of each chapter. All chapter objectives are listed in the beginning.
Functions and abstraction Michael Ernst UW CSE 190p Summer 2012.
Using Data Active Server Pages Objectives In this chapter, you will: Learn about variables and constants Explore application and session variables Learn.
A First Program Using C#
COMPUTATION WITH STRINGS 4 DAY 5 - 9/05/14 LING 3820 & 6820 Natural Language Processing Harry Howard Tulane University.
Lists and More About Strings CS303E: Elements of Computers and Programming.
Functions Part I (Syntax). What is a function? A function is a set of statements which is split off into a separate entity that can be used like a “new.
Builtins, namespaces, functions. There are objects that are predefined in Python Python built-ins When you use something without defining it, it means.
Structured programming 4 Day 34 LING Computational Linguistics Harry Howard Tulane University.
A First Book of C++: From Here To There, Third Edition2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single.
Chapter 2 Overview of C Part I J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National Taipei University.
1 CSC 221: Introduction to Programming Fall 2012 Functions & Modules  standard modules: math, random  Python documentation, help  user-defined functions,
XP Tutorial 10New Perspectives on Creating Web Pages with HTML, XHTML, and XML 1 Working with JavaScript Creating a Programmable Web Page for North Pole.
Built-in Data Structures in Python An Introduction.
CPS120: Introduction to Computer Science Lecture 14 Functions.
Introducing Python CS 4320, SPRING Resources We will be following the Python tutorialPython tutorial These notes will cover the following sections.
Algorithms  Problem: Write pseudocode for a program that keeps asking the user to input integers until the user enters zero, and then determines and outputs.
Python uses boolean variables to evaluate conditions. The boolean values True and False are returned when an expression is compared or evaluated.
Neal Stublen What's a class?  A class is comprised of a set of data and the actions taken on that data  Each action is represented.
Python Functions.
_______________________________________________________________________________________________________________ PHP Bible, 2 nd Edition1  Wiley and the.
Introducing Python CS 4320, SPRING Lexical Structure Two aspects of Python syntax may be challenging to Java programmers Indenting ◦Indenting is.
ProgLan Python Session 4. Functions are a convenient way to divide your code into useful blocks, allowing us to: order our code, make it more readable,
COMPUTATION WITH STRINGS 1 DAY 2 - 8/27/14 LING 3820 & 6820 Natural Language Processing Harry Howard Tulane University.
NLTK & Python Day 8 LING Computational Linguistics Harry Howard Tulane University.
Python Let’s get started!.
CS190/295 Programming in Python for Life Sciences: Lecture 6 Instructor: Xiaohui Xie University of California, Irvine.
FUNCTIONS. Midterm questions (1-10) review 1. Every line in a C program should end with a semicolon. 2. In C language lowercase letters are significant.
OCR Computing GCSE © Hodder Education 2013 Slide 1 OCR GCSE Computing Python programming 3: Built-in functions.
Today… Style, Cont. – Naming Things! Methods and Functions Aside - Python Help System Punctuation Winter 2016CISC101 - Prof. McLeod1.
OCR A Level F453: The function and purpose of translators Translators a. describe the need for, and use of, translators to convert source code.
XP Tutorial 10New Perspectives on HTML, XHTML, and DHTML, Comprehensive 1 Working with JavaScript Creating a Programmable Web Page for North Pole Novelties.
L071 Introduction to C Topics Compilation Using the gcc Compiler The Anatomy of a C Program Reading Sections
CONTROL 3 DAY /29/14 LING 3820 & 6820 Natural Language Processing Harry Howard Tulane University.
Winter 2016CISC101 - Prof. McLeod1 CISC101 Reminders Quiz 3 this week – last section on Friday. Assignment 4 is posted. Data mining: –Designing functions.
FILES AND EXCEPTIONS Topics Introduction to File Input and Output Using Loops to Process Files Processing Records Exceptions.
UMBC CMSC 104 – Section 01, Fall 2016
Fundamentals of Python: First Programs
User-Written Functions
CS170 – Week 1 Lecture 3: Foundation Ismail abumuhfouz.
Python Let’s get started!.
Introduction to Python
Algorithms Problem: Write pseudocode for a program that keeps asking the user to input integers until the user enters zero, and then determines and outputs.
Variables, Expressions, and IO
Introduction to C Topics Compilation Using the gcc Compiler
Functions CIS 40 – Introduction to Programming in Python
Introduction to C Topics Compilation Using the gcc Compiler
4. Javascript Pemrograman Web I Program Studi Teknik Informatika
Python Primer 2: Functions and Control Flow
2.1 Parts of a C++ Program.
CS190/295 Programming in Python for Life Sciences: Lecture 6
Creating your first C program
Regular expressions 3 Day /26/16
Introduction to Programming
CISC101 Reminders All assignments are now posted.
Introduction to Computer Science
Introduction to Programming
Functions, Procedures, and Abstraction
Control 1 Day /30/16 LING 3820 & 6820 Natural Language Processing
PYTHON - VARIABLES AND OPERATORS
Presentation transcript:

Structured programming 3 Day 33 LING Computational Linguistics Harry Howard Tulane University

13-Nov-2009LING , Prof. Howard, Tulane University2 Course organization 

Structured programming NLPP §4

13-Nov-2009LING , Prof. Howard, Tulane University4 Summary  Strings are used at the beginning and the end of a NLP task:  a program reads in some text and produces output for us to read  Lists and tuples are used in the middle:  A list is typically a sequence of objects all having the same type, of arbitrary length.  We often use lists to hold sequences of words.  A tuple is typically a collection of objects of different types, of fixed length.  We often use a tuple to hold a record, a collection of different fields relating to some entity.

13-Nov-2009LING , Prof. Howard, Tulane University5 Another example >>> lexicon = [... ('the', 'det', ['Di:', ('off', 'prep', ['Qf', 'O:f'])... ]

13-Nov-2009LING , Prof. Howard, Tulane University6 More summary  Lists are mutable; they can be modified. >>> lexicon.sort() >>> lexicon[1] = ('turned', 'VBD', ['t3:nd', 't3`nd']) >>> del lexicon[0]  Tuples are immutable; tuples cannot be modified.  Convert lexicon to a tuple, using lexicon = tuple(lexicon),  then try each of the above operations, to confirm that none of them is permitted on tuples.

Questions of Style NLPP 4.3

13-Nov-2009LING , Prof. Howard, Tulane University8 Style  Programming is as much an art as a science.  The undisputed "bible" of programming, a 2,500 page multi-volume work by Donald Knuth, is called The Art of Computer Programming.  Many books have been written on Literate Programming, recognizing that humans, not just computers, must read and understand programs.  Here we pick up on some issues of programming style that have important ramifications for the readability of your code, including code layout, procedural vs declarative style, and the use of loop variables.

13-Nov-2009LING , Prof. Howard, Tulane University9 Style in Python  When writing programs you make many subtle choices about names, spacing, comments, and so on.  When you look at code written by other people, needless differences in style make it harder to interpret the code.  Therefore, the designers of the Python language have published a style guide for Python code, available at  The underlying value presented in the style guide is consistency, for the purpose of maximizing the readability of code.

13-Nov-2009LING , Prof. Howard, Tulane University10 Some recommendations  Code layout should use four spaces per indentation level.  You should make sure that when you write Python code in a file, you avoid tabs for indentation, since these can be misinterpreted by different text editors and the indentation can be messed up.  Lines should be less than 80 characters long.  If necessary you can break a line inside parentheses, brackets, or braces, because Python is able to detect that the line continues over to the next line.  Example next slide.

13-Nov-2009LING , Prof. Howard, Tulane University11 Adding () or \ >>> if (len(syllables) > 4 and len(syllables[2]) == 3 and... syllables[2][2] in [aeiou] and syllables[2][3] == syllables[1][3]):... process(syllables) >>> if len(syllables) > 4 and len(syllables[2]) == 3 and \... syllables[2][2] in [aeiou] and syllables[2][3] == syllables[1][3]:... process(syllables)

13-Nov-2009LING , Prof. Howard, Tulane University12 Procedural vs declarative style  Version 1 (more procedural) >>> tokens = nltk.corpus.brown.words(categories='news') >>> count = 0 >>> total = 0 >>> for token in tokens:... count += 1... total += len(token) >>> print total / count  Version 2 (more declarative) >>> total = sum(len(t) for t in tokens) >>> print total / len(tokens)

13-Nov-2009LING , Prof. Howard, Tulane University13 Another example  Version 1 (more procedural) >>> word_list = [] >>> len_word_list = len(word_list) >>> i = 0 >>> while i < len(tokens):... j = 0... while j < len_word_list and word_list[j] < tokens[i]:... j += 1... if j == 0 or tokens[i] != word_list[j]:... word_list.insert(j, tokens[i])... len_word_list += 1... i += 1  Version 2 (more declarative) >>> word_list = sorted(set(tokens))

13-Nov-2009LING , Prof. Howard, Tulane University14 Looping vs. list comprehension  Version 1 (loop): >>> text = nltk.corpus.gutenberg.words('milton-paradise.txt') >>> longest = '' >>> for word in text:... if len(word) > len(longest):... longest = word >>> longest 'unextinguishable'  Version 2 (list comprehension): >>> maxlen = max(len(word) for word in text) >>> [word for word in text if len(word) == maxlen] ['unextinguishable', 'transubstantiate', 'inextinguishable', 'incomprehensible']

Functions: The Foundation of Structured Programming SLPP 4.4

13-Nov-2009LING , Prof. Howard, Tulane University16 Functions  Functions provide an effective way to package and re-use program code, as already explained in Section 2.3.  For example, suppose we find that we often want to read text from an HTML file.  This involves several steps:  opening the file,  reading it in,  normalizing whitespace, and  stripping HTML markup.

13-Nov-2009LING , Prof. Howard, Tulane University17 Example  We can collect these steps into a function, and give it a name such as get_text(): import re def get_text(file): """Read text from a file, normalizing whitespace and stripping HTML markup.""" text = open(file).read() text = re.sub('\s+', ' ', text) text = re.sub(r' ', ' ', text) return text

13-Nov-2009LING , Prof. Howard, Tulane University18 Usage  Now, any time we want to get cleaned-up text from an HTML file, we can just call get_text() with the name of the file as its only argument.  It will return a string, and we can assign this to a variable: contents = get_text("test.html")  Each time we want to use this series of steps we only have to call the function.

13-Nov-2009LING , Prof. Howard, Tulane University19 Advantages  Using functions has the benefit of saving space in our program.  More importantly, our choice of name for the function helps make the program readable.  In the case of the above example, whenever our program needs to read cleaned-up text from a file we don't have to clutter the program with four lines of code, we simply need to call get_text().  This naming helps to provide some "semantic interpretation" — it helps a reader of our program to see what the program "means".

13-Nov-2009LING , Prof. Howard, Tulane University20 Documentation  Notice that the get_text() definition contains a string highlighted in red.  The first string inside a function definition is called a docstring.  Not only does it document the purpose of the function to someone reading the code, it is accessible to a programmer who has loaded the code from a file: >>> help(get_text) Help on function get_text: get_text(file) Read text from a file, normalizing whitespace and stripping HTML markup.

13-Nov-2009LING , Prof. Howard, Tulane University21 Function inputs and outputs  Information is passed to a function using its parameters, the parenthesized list of variables and constants following the function's name in the function definition: >>> def repeat(msg, num):... return ' '.join([msg] * num) >>> monty = 'Monty Python' >>> repeat(monty, 3) 'Monty Python Monty Python Monty Python'  The function is defined to take two parameters, msg and num.  The function is called and two arguments, monty and 3, are passed to it.  These arguments fill the "placeholders" provided by the parameters and provide values for the occurrences of msg and num in the function body.

Next time Q10 Finish §4