ebook img

Python for Linguists PDF

313 Pages·2020·3.685 MB·English
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview Python for Linguists

PythonforLinguists Specificallydesignedforlinguists,thisbookprovidesanintroductiontopro- gramming using Python for those with little to no experience with coding. Python is one of the most popular and widely used programming languages as it’s also available for free and runs on any operating system. All exam- plesinthetextinvolvelanguagedataandcanbeadaptedoruseddirectlyfor language research. The text focuses on key language-related issues: search- ing,textmanipulation,textencodingandinternetdata,providinganexcellent resource for language research. More experienced users of Python will also benefitfromtheadvancedchaptersongraphicaluserinterfacesandfunctional programming. MICHAEL HAMMONDisProfessorofLinguisticsandHumanLanguageTech- nologyattheUniversityofArizona.HisprevioustitlesincludeProgramming for Linguists: Perl for Language Professionals (2003) and Programming for Linguists:JavaTechnologyforLanguageProfessionals(2002). Python for Linguists MichaelHammond UniversityofArizona UniversityPrintingHouse,CambridgeCB28BS,UnitedKingdom OneLibertyPlaza,20thFloor,NewYork,NY10006,USA 477WilliamstownRoad,PortMelbourne,VIC3207,Australia 314–321,3rdFloor,Plot3,SplendorForum,JasolaDistrictCentre, NewDelhi–110025,India 79AnsonRoad,#06–04/06,Singapore079906 CambridgeUniversityPressispartoftheUniversityofCambridge. ItfurtherstheUniversity’smissionbydisseminatingknowledgeinthepursuitof education,learning,andresearchatthehighestinternationallevelsofexcellence. www.cambridge.org Informationonthistitle:www.cambridge.org/9781108493444 DOI:10.1017/9781108642408 ©MichaelHammond2020 Thispublicationisincopyright.Subjecttostatutoryexception andtotheprovisionsofrelevantcollectivelicensingagreements, noreproductionofanypartmaytakeplacewithoutthewritten permissionofCambridgeUniversityPress. Firstpublished2020 PrintedintheUnitedKingdombyTJInternationalLtd.PadstowCornwall AcataloguerecordforthispublicationisavailablefromtheBritishLibrary. ISBN978-1-108-49344-4Hardback ISBN978-1-108-73707-4Paperback CambridgeUniversityPresshasnoresponsibilityforthepersistenceoraccuracyof URLsforexternalorthird-partyinternetwebsitesreferredtointhispublication anddoesnotguaranteethatanycontentonsuchwebsitesis,orwillremain, accurateorappropriate. Contents Preface pageix WhyDoLinguistsNeedtoLearnHowtoProgram? ix WhyIsPythonaGoodChoice? x HowThisBookIsDifferent xi OverviewoftheBook xi HowtoUsetheBook xii Acknowledgments xii 1 InteractingwithPythonandBasicFunctions 1 1.1 InstallingandUsingPython 1 1.2 TheInteractiveEnvironment 2 1.3 BasicInteractions 3 1.4 EditandRun 6 1.5 Summary 7 1.6 Exercises 7 2 DataTypesandVariables 9 2.1 Assignment 9 2.2 VariableNames 11 2.3 BasicDataTypes 11 2.3.1 Numbers 11 2.3.2 Booleans 12 2.3.3 Strings 14 2.3.4 Lists 19 2.3.5 Tuples 21 2.3.6 Dictionaries 22 2.4 Mutability 24 2.5 Exercises 27 3 ControlStructures 29 3.1 GroupingandIndentation 29 3.2 if 32 3.3 DigressiononPrinting 34 3.4 for 35 3.5 while 41 3.6 breakandcontinue 46 3.7 MakingNonsenseItems 50 3.8 Summary 54 3.9 Exercises 54 v vi Contents 4 Input–Output 56 4.1 Command-LineInput 56 4.2 KeyboardInput 64 4.3 FileInput–Output 67 4.4 AliceinWonderland 72 4.5 Summary 79 4.6 Exercises 79 5 SubroutinesandModules 81 5.1 SimpleFunctions 82 5.2 FunctionsThatReturnValues 85 5.3 FunctionsThatTakeArguments 87 5.4 RecursiveandLambdaFunctions 91 5.5 Modules 94 5.6 WritingYourOwnModules 96 5.7 DocstringsandModules 101 5.8 AnalysisofSentences 102 5.9 Exercises 115 6 RegularExpressions 117 6.1 Matching 118 6.2 Patterns 122 6.3 Backreferences 125 6.4 InitialConsonantClusters 126 6.5 Exercises 136 7 TextManipulation 138 7.1 StringManipulationIsCostly 138 7.2 ManipulatingText 139 7.3 Morphology 142 7.4 Exercises 166 8 InternetData 167 8.1 RetrievingWebpages 167 8.2 HTML 168 8.3 ParsingHTML 172 8.4 Parallelism 175 8.5 UnicodeandTextEncoding 179 8.6 BytesandStrings 182 8.7 WhatIstheEncoding? 184 8.8 AWebcrawler 186 8.9 Exercises 204 9 Objects 206 9.1 GeneralLogic 206 9.2 ClassesandInstances 207 9.3 Inheritance 217 9.4 Syllabification 221 9.5 Exercises 231 Contents vii 10 GUIs 233 10.1 TheGeneralLogic 234 10.2 SomeSimpleExamples 236 10.3 WidgetOptions 240 10.4 PackingOptions 245 10.5 MoreWidgets 249 10.6 StemmingwithaGUI 252 10.7 Exercises 259 11 FunctionalProgramming 261 11.1 FunctionalProgrammingGenerally 261 11.2 Variables,State,andMutability 262 11.3 FunctionsasFirst-ClassObjects 265 11.4 OvertRecursion 269 11.5 Comprehensions 271 11.6 VectorizedComputation 273 11.7 Iterables,Iterators,andGenerators 275 11.8 ParallelProgramming 280 11.9 MakingNonsenseItemsAgain 288 11.10 Exercises 289 AppendixA NLTK 291 A.1 Installing 291 A.2 Corpora 291 A.3 Tokenizing 293 A.4 StopWords 294 A.5 Tagging 296 A.6 Summary 296 Index 297 Preface ThisisabookonhowtoprogramforlinguisticpurposesusingthePythonpro- gramminglanguage.Inthisprefaceweoutlineourgoals,justifyusingPython toachievethem,andexplainhowbesttousethisbook. WhyDoLinguistsNeedtoLearnHowtoProgram? Programming is an extremely useful skill in many areas of linguistics and in other language-related fields like speech and hearing sciences, psychology, psycholinguistics,andquantitativeliterarystudies. Within linguistics, it used to be the case that programming skills were requiredonlyforcomputationallinguists,butthisisfarfromtruethesedays. Programming now is used in phonology, syntax, morphology, semantics, pragmatics,psycholinguistics,phonetics,discourseanalysis,essentiallyevery area of linguistic investigation. This change reflects broader methodological changesinthefield,aresponsetothefactthat(i)moreandmoredataareavail- ableelectronically,and(ii)wehavemuchrichertechniquesforexaminingand manipulatingmassiveamountsofelectronicdata. Herearesomeexamplesofwhatyoucandowithfairlymodestprogramming skills: l Build a simple list of occurring words from a text file written in some languagealongwiththefrequencyofthosewords. l Finditemsforpsycholinguisticorphoneticexperimentsfromtextresources, e.g., frequent two-syllable words that begin with a three-consonant cluster anddon’totherwisecontainnasalconsonants. l Construct every possible one-syllable word given a set of possible onsets, vowels,andcodas. l Constructeverypossibletwo-wordcompoundgivenalistofwords. l Findtheaveragenumberofwordspersentencefromatextcorpusandfind thelongestsentenceinthatcorpus. ix

See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.