Structured Programming In Python Ling Helsinki Fi-Books Pdf

Structured Programming in Python ling helsinki fi
16 Nov 2019 | 144 views | 0 downloads | 31 Pages | 1.15 MB

Share Pdf : Structured Programming In Python Ling Helsinki Fi

Download and Preview : Structured Programming In Python Ling Helsinki Fi

Report CopyRight/DMCA Form For : Structured Programming In Python Ling Helsinki Fi



Transcription

5 2 Back to the Basics, i am the walrus, Although this does the job it is not idiomatic Python By contrast Python s for statement allows. us to achieve the same effect much more succinctly . sent I am the Walrus , for s in sent , print s lower . i am the walrus, We ll start with the most innocuous operation of all assignment Then we will look at sequence. types in detail , 5 2 1 Assignment, Python s assignment statement operates on values But what is a value Consider the following code. word1 Monty , word2 word1 , word1 Python a, Monty .
This code shows that when we write word2 word1 in line the value of word1 the string. Monty is assigned to word2 That is word2 is a copy of word1 so when we overwrite word1. with a new string Python in line a the value of word2 is not affected . However assignment statements do not always involve making copies in this way An important. subtlety of Python is that the value of a structured object such as a list is actually a reference to the. object In the following example line assigns the reference of list1 to the new variable list2 . When we modify something inside list1 on line a we can see that the contents of list2 have also. been changed , list1 Monty Python , list2 list1 . list1 1 Bodkin a, Monty Bodkin , Figure 5 1 List Assignment and Computer Memory. August 27 2008 2 Bird Klein Loper, 5 Structured Programming in Python Introduction to Natural Language Processing DRAFT . Thus line does not copy the contents of the variable only its object reference To understand. what is going on here we need to know how lists are stored in the computer s memory In Figure 5 1 . we see that a list sent1 is a reference to an object stored at location 3133 which is itself a series of. pointers to other locations holding strings When we assign sent2 sent1 it is just the object. reference 3133 that gets copied , 5 2 2 Sequences Strings Lists and Tuples. We have seen three kinds of sequence object strings lists and tuples As sequences they have some. common properties they can be indexed and they have a length . text I turned off the spectroroute , words I turned off the spectroroute .
pair 6 turned , text 2 words 3 pair 1 , t the turned . len text len words len pair , 29 5 2 , We can iterate over the items in a sequence s in a variety of useful ways as shown in Table 5 1 . Python Expression Comment, for item in s iterate over the items of s. for item in sorted s iterate over the items of s in order. for item in set s iterate over unique elements of s. for item in reversed s iterate over elements of s in reverse. for item in set s difference t iterate over elements of s not in t. for item in random shuffle s iterate over elements of s in random order. Table 5 1 Various ways to iterate over sequences, The sequence functions illustrated in Table 5 1 can be combined in various ways for example to. get unique elements of s sorted in reverse use reversed sorted set s . We can convert between these sequence types For example tuple s converts any kind of. sequence into a tuple and list s converts any kind of sequence into a list We can convert a list of. strings to a single string using the join function e g join words . Notice in the above code sample that we computed multiple values on a single line separated by. commas These comma separated expressions are actually just tuples Python allows us to omit the. parentheses around tuples if there is no ambiguity When we print a tuple the parentheses are always. displayed By using tuples in this way we are implicitly aggregating items together . In the next example we use tuples to re arrange the contents of our list We can omit the. parentheses because the comma has higher precedence than assignment . words 2 words 3 words 4 words 3 words 4 words 2 , I turned the spectroroute off .
Bird Klein Loper 3 August 27 2008, 5 2 Back to the Basics. This is an idiomatic and readable way to move items inside a list It is equivalent to the following. traditional way of doing such tasks that does not use tuples notice that this method needs a temporary. variable tmp , tmp words 2 , words 2 words 3 , words 3 words 4 . words 4 tmp, As we have seen Python has sequence functions such as sorted and reversed that. rearrange the items of a sequence There are also functions that modify the structure of a sequence. and which can be handy for language processing Thus zip takes the items of two sequences and. zips them together into a single list of pairs Given a sequence s enumerate s returns an iterator. that produces a pair of an index and the item at that index . words I turned off the spectroroute , tags NNP VBD IN DT NN . zip words tags , I NNP turned VBD off IN , the DT spectroroute NN .
list enumerate words , 0 I 1 turned 2 off 3 the 4 spectroroute . 5 2 3 Combining Different Sequence Types, Let s combine our knowledge of these three sequence types together with list comprehensions to. perform the task of sorting the words in a string by their length . words I turned off the spectroroute split , wordlens len word word for word in words a. wordlens, 1 I 6 turned 3 off 3 the 12 spectroroute . wordlens sort b, join word for count word in wordlens c.
I off the turned spectroroute , Each of the above lines of code contains a significant feature Line demonstrates that a simple. string is actually an object with methods defined on it such as split Line a shows the construc . tion of a list of tuples where each tuple consists of a number the word length and the word e g . 3 the Line b sorts the list modifying the list in place Finally line c discards the length. information then joins the words back into a single string . We began by talking about the commonalities in these sequence types but the above code illustrates. important differences in their roles First strings appear at the beginning and the end this is typical in. the context where our program is reading in some text and producing output for us to read Lists and. tuples are used in the middle but for different purposes 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 In contrast 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 This distinction between the use of lists. and tuples takes some getting used to so here is another example . lexicon , the DT Di D , August 27 2008 4 Bird Klein Loper. 5 Structured Programming in Python Introduction to Natural Language Processing DRAFT . off IN Qf O f , Here a lexicon is represented as a list because it is a collection of objects of a single type lexical. entries of no predetermined length An individual entry is represented as a tuple because it is a. collection of objects with different interpretations such as the orthographic form the part of speech . and the pronunciations represented in the SAMPA computer readable phonetic alphabet Note that. these pronunciations are stored using a list Why , The distinction between lists and tuples has been described in terms of usage However there is. a more fundamental difference in Python lists are mutable while tuples are immutable In other. words lists can be modified while tuples cannot Here are some of the operations on lists that do. in place modification of the list None of these operations is permitted on a tuple a fact you should. confirm for yourself , lexicon sort , lexicon 1 turned VBD t3 nd t3 nd .
del lexicon 0 , 5 2 4 Stacks and Queues, Lists are a particularly versatile data type We can use lists to implement higher level data types such. as stacks and queues A stack is a container that has a last in first out policy for adding and removing. items see Figure 5 2 , Figure 5 2 Stacks and Queues. Stacks are used to keep track of the current context in computer processing of natural languages. and programming languages too We will seldom have to deal with stacks explicitly as the implemen . tation of NLTK parsers treebank corpus readers and even Python functions all use stacks behind the. scenes However it is important to understand what stacks are and how they work . In Python we can treat a list as a stack by limiting ourselves to the three operations defined on. stacks append item to push item onto the stack pop to pop the item off the top of the. stack and 1 to access the item on the top of the stack Listing 5 1 processes a sentence with phrase. markers and checks that the parentheses are balanced The loop pushes material onto the stack when. it gets an open parenthesis and pops the stack when it gets a close parenthesis We see that two are left. on the stack at the end i e the parentheses are not balanced . Bird Klein Loper 5 August 27 2008, 5 2 Back to the Basics. Listing 5 1 Check parentheses are balanced, def check parens tokens . stack , for token in tokens , if token push, stack append token .
elif token pop, stack pop , return stack, phrase the cat sat on the mat . print check parens phrase split , , Although Listing 5 1 is a useful illustration of stacks it is overkill because we could have done a. direct count phrase count phrase count However we can use stacks. for more sophisticated processing of strings containing nested structure as shown in Listing 5 2 . Here we build a potentially deeply nested list of lists Whenever a token other than a parenthesis. is encountered we add it to a list at the appropriate level of nesting The stack cleverly keeps track of. this level of nesting exploiting the fact that the item at the top of the stack is actually shared with a. more deeply nested item Hint add diagnostic print statements to the function to help you see what it. is doing , Lists can be used to represent another important data structure A queue is a container that has a. first in first out policy for adding and removing items see Figure 5 2 Queues are used for scheduling. activities or resources As with stacks we will seldom have to deal with queues explicitly as the. implementation of NLTK n gram taggers Section 3 5 5 and chart parsers Section 8 2 use queues. behind the scenes However we will take a brief look at how queues are implemented using lists . queue the cat sat , queue append on , queue append the . queue append mat , queue pop 0 , queue pop 0 , sat on the mat .
5 2 5 More List Comprehensions, You may recall that in Chapter 2 we introduced list comprehensions with examples like the following . sent The dog gave John the newspaper , word lower for word in sent . the dog gave john the newspaper , List comprehensions are a convenient and readable way to express list operations in Python and. they have a wide range of uses in natural language processing In this section we will see some more. August 27 2008 6 Bird Klein Loper, 5 Structured Programming in Python Introduction to Natural Language Processing DRAFT . Listing 5 2 Convert a nested phrase into a nested list using a stack. def convert parens tokens , stack , for token in tokens .
if token push, sublist , stack 1 append sublist . stack append sublist , elif token pop, stack pop . else update top of stack, stack 1 append token , return stack 0 . phrase the cat sat on the mat , print convert parens phrase split . the cat sat on the mat , examples The first of these takes successive overlapping slices of size n a sliding window from a.
list pay particular attention to the range of the variable i . sent The dog gave John the newspaper , sent i i n for i in range len sent n 1 . The dog gave , dog gave John , gave John the , John the newspaper . You can also use list comprehensions for a kind of multiplication or cartesian product Here we. generate all combinations of two determiners two adjectives and two nouns The list comprehension. is split across three lines for readability , dt jj nn for dt in two three . for jj in old blind , for nn in men mice , two old men two old mice two blind men . two blind mice three old men three old mice , three blind men three blind mice .
The above example contains three independent for loops These loops have no variables in common . and we could have put them in any order We can also have nested loops with shared variables The. next example iterates over all sentences in a section of the Brown Corpus and for each sentence iterates. over each word , word for word in nltk corpus brown words categories a . if len word 17 , September October Sheraton Biltmore anti organization . anti organization Washington Oregon York Pennsylvania . misunderstandings Sheraton Biltmore neo stagnationist . Bird Klein Loper 7 August 27 2008, 5 2 Back to the Basics. cross examination bronzy green gold Oh the pain of it . Secretary General Secretary General textile importing . textile exporting textile producing textile producing . As you will see the list comprehension in this example contains a final if clause that allows us to. filter out any words that fail to meet the specified condition . Another way to use loop variables is to ignore them This is the standard method for building. Structured Programming in Python 5 1 Introduction In Part I you had an intensive introduction to Python Chapter 1 followed by chapters on words tags and chunks Chapters 2 6 These chapters contain many examples and exercises that should have helped you consolidate your Python skills and apply them to simple NLP tasks So far our programs

Related Books

DELLA CHIESA CATTOLICA

DELLA CHIESA CATTOLICA

Cattolica contenente una formulazione pi sintetica dei medesimi contenuti di fede Dopo due anni di lavoro fu preparato un progetto di compendio che fu inviato per la consultazione ai Cardinali e ai Presidenti delle Conferenze Episcopali Il progetto nel suo complesso ha avuto una valutazione positiva da parte della maggioranza assoluta di quanti hanno risposto

della Chiesa Cattolica BreviarioDigitale com

della Chiesa Cattolica BreviarioDigitale com

Catechismo della Chiesa Cattolica BREVIARIO DIGITALE SOMMARIO PARTE PRIMA INDICE ANALITICO INDICE GENERALE Sei tu che lo stimoli a dilettarsi delle tue lodi perch ci hai fatti per te e il nostro cuore non ha posa finch non riposa in te 7 II Le vie che portano alla conoscenza di Dio 31 Creato a immagine di Dio chiamato a

EVENTS amp DOCUMENTS NATIONAL CHRONICLES SEI Editrice

EVENTS amp DOCUMENTS NATIONAL CHRONICLES SEI Editrice

La Chiesa Cattolica e Al Azhar attraverso la comune cooperazione annunciano e promettono di portare questo Documento alle Autorit ai Leader influenti agli uomini di religione di tutto il mondo alle organizzazioni regionali e internazionali competenti alle organizzazioni della societ civile alle istituzioni religiose e ai leader del pensiero e di impegnarsi nel diffondere i principi

A basic guide to the Court of Protection

A basic guide to the Court of Protection

This guide gives you a basic explanation about the Court of Protection in England and Wales It is for people who are involved in proceedings in the Court of Protection or who want to find out if the Court of Protection can help themor someone they know Most of the guide is about welfare cases These are cases about

A Lifelong Aversion to Writing What If Writing Courses

 A Lifelong Aversion to Writing What If Writing Courses

fair share of aversion to writing especially in my basic writing classes I have also worked with a number of area high school English teachers over the last several years and I have heard plenty about student aversion to writing from them as well Is it possible that the most lasting and significant learning outcome many students take away from English classes is a life long aversion to

Institut f r Analysis und Scientific Computing TU Wien

Institut f r Analysis und Scientific Computing TU Wien

Programm Ein Computerprogramm oder kurz Programm ist eine Folge von Anweisungen die den Regeln einer Programmiersprache gen ugen um auf einem Computer eine bestimmte Funktio

Ad n el Hombre Libro Esoterico

Ad n el Hombre Libro Esoterico

Ad n nuestro progenitor el primer hombre fue al igual que Cristo un esp ritu pre existente y al igual que Cristo tom un cuerpo apropiado el cuerpo de un hombre y as se convirtio en alma viviente La doctrina de la pre existencia revelada de manera tan clara especialmente en los ltimos d as arroja luz sobre el misterioso problema del origen del hombre Demuestra que el

P01 Datos Madriman

P01 Datos Madriman

Hasta el ltimo hombre P01 Datos Cine P02 P er sonaj Intentad construir por grupos la descripci n de los personajes de la historia para as conocerles m s Cuando un personaje evoluciona diremos que es redondo mientras que si permanece igual de principio a fin diremos que es plano Despu s de realizar el ejercicio valorad c mo son los personajes de la pel cula De cada uno

STUDENT SUCCESS canyons edu

STUDENT SUCCESS canyons edu

improvisation group Who knows what you will discover it just may be greatness 1 2 ACADEMIC INTEGRITY The college expects students to adhere to the highest standards of good citizenship honesty and integrity in their academic work and in their personal conduct It is your responsibility as a student to behave honestly and

Fundamentals of Piano Practice 2nd Edition

Fundamentals of Piano Practice 2nd Edition

conduct research and improve their teaching methods are more successful Numer ous pianists mentioned that they were taught all wrong by previous teachers Many who liked their teachers noted that these teachers used methods similar to those in this book There is almost uniform agreement on what is right and what is wrong

Case study example Deloitte United States

Case study example Deloitte United States

The strong success of this line has caused Duraflex smanagement to re evaluate their position in work boots With limited In January of 2014 Duraflex hired a leading consulting firm to conduct research to help management in its decision making To make an informed recommendation the consultants realized they needed to collect information that would enable them to size the market and