c# convert pdf to image free : Convert pdf password protected to word online control Library system azure .net windows console ch1-30-part45

Chapter 1
Language Processing and Python
Itiseasytogetourhandsonmillionsofwordsoftext.Whatcanwedowithit,assumingwecanwrite
somesimpleprograms?Inthischapterwe’lltacklethefollowingquestions:
1. whatcanweachievebycombiningsimpleprogrammingtechniqueswithlargequantities
oftext?
2. howcanweautomaticallyextractkeywordsandphrasesthatsumupthestyleandcontent
ofatext?
3. isthePythonprogramminglanguagesuitableforsuchwork?
4. whataresomeoftheinterestingchallengesofnaturallanguageprocessing?
Thischapterisdividedintosectionsthatskipbetweentwoquitedifferentstyles. Inthe“computing
withlanguage” sectionswewilltakeonsomelinguistically-motivatedprogrammingtaskswithout
necessarilyunderstandinghowtheywork.Inthe“closerlookatPython”sectionswewillsystematically
reviewkeyprogrammingconcepts.We’llflagthetwostylesinthesectiontitles,butlaterchapterswill
mixbothstyleswithoutbeingsoup-frontaboutit. Wehopethisstyleofintroductiongivesyouan
authentictasteofwhatwillcomelater,whilecoveringarangeofelementaryconceptsinlinguistics
andcomputerscience. IfyouhavebasicfamiliaritywithbothareasyoucanskiptoSection1.5;we
willrepeatanyimportantpointsinlaterchapters,andifyoumissanythingyoucaneasilyconsultthe
onlinereferencematerialathttp://www.nltk.org/.
1.1 ComputingwithLanguage:TextsandWords
We’reallveryfamiliarwithtext,sincewereadandwriteiteveryday. Butherewewilltreattextas
rawdatafortheprogramswewrite,programsthatmanipulateandanalyzeitinavarietyofinteresting
ways.Beforewecandothis,wehavetogetstartedwiththePythoninterpreter.
1
Convert pdf password protected to word online - C# PDF Password Library: add, remove, edit PDF file password in C#.net, ASP.NET, MVC, WinForms, WPF
Help to Improve the Security of Your PDF Document by Setting Password
adding a password to a pdf file; copy protecting pdf files
Convert pdf password protected to word online - VB.NET PDF Password Library: add, remove, edit PDF file password in vb.net, ASP.NET, MVC, WinForms, WPF
Help to Improve the Security of Your PDF Document by Setting Password
annotate protected pdf; pdf user password
1.1.ComputingwithLanguage:TextsandWords
GettingStarted
OneofthefriendlythingsaboutPythonisthatitallowsyoutotypedirectlyintotheinteractiveinter-
pretertheprogramthatwillberunningyourPythonprograms. YoucanaccessthePythoninter-
preterusingasimplegraphicalinterfacecalledtheInteractiveDeveLopmentEnvironment(IDLE).Ona
MacyoucanfindthisunderApplications’MacPython,andonWindowsunderAllPrograms’Python.
UnderUnixyoucanrunPythonfromtheshell bytyping
idle
(if thisisnot installed, trytyping
python
). Theinterpreter will printablurbaboutyourPythonversion; simplycheckthat youare
runningPython2.4orgreater(hereitis2.5.1):
Python 2.5.1 (r251:54863, Apr r 15 2008, 22:57:26)
[GCC 4.0.1 (Apple Inc. build 5465)] on darwin
Type "help", "copyright", "credits" or "license" " for more information.
>>>
IfyouareunabletorunthePythoninterpreter,youprobablydon’thavePythoninstalled
correctly.Pleasevisithttp://python.org/fordetailedinstructions.
The
>>>
promptindicatesthatthePythoninterpreterisnowwaitingforinput.Whencopyingexamples
fromthisbookbesurenottotypeinthe
>>>
promptyourself. Now,let’sbeginbyusingPythonasa
calculator:
>>> 1 + + 5
*
2 - 3
8
>>>
Oncetheinterpreterhasfinishedcalculatingtheansweranddisplayingit,thepromptreappears. This
meansthePythoninterpreteriswaitingforanotherinstruction.
Your Turn: Enter a few more expressions of yourown. Youcan use asterisk (
*
) for
multiplicationandslash(/)fordivision,andparenthesesforbracketingexpressions. Note
thatdivisiondoesn’talwaysbehaveasyoumightexpectitdoesintegerdivisionorfloating
pointdivisiondependingonwhetheryoutype1/3or1.0/3.0.
Theseexamplesdemonstratehow youcanworkinteractivelywiththeinterpreter, allowingyouto
experimentandexplore.Nowlet’stryanonsensicalexpressiontoseehowtheinterpreterhandlesit:
>>> 1 +
File "<stdin>", line 1
1 +
^
SyntaxError: invalid syntax
>>>
Herewehaveproducedasyntaxerror. Itdoesn’tmakesensetoendaninstructionwithaplussign.
ThePythoninterpreterindicatesthelinewheretheproblemoccurred(line1of“standardinput”).
SearchingText
NowthatwecanusethePythoninterpreter,let’sseehowwecanharnessitspowertoprocesstext.The
firststepistotypeaspecialcommandatthePythonpromptwhichtellstheinterpretertoloadsometexts
December18,2008
2
Bird,Klein&Loper
VB.NET PDF Convert to Word SDK: Convert PDF to Word library in vb.
Convert PDF document to DOC and DOCX formats in Visual Basic control to export Word from multiple PDF files in VB. Supports transfer from password protected PDF.
password pdf files; change password on pdf
C# PDF Convert to Word SDK: Convert PDF to Word library in C#.net
Convert PDF to multiple MS Word formats such as .doc and .docx. Create editable Word file online without email. Password protected PDF file can be printed to
break password on pdf; password pdf
1.LanguageProcessingandPython
NaturalLanguageProcessing(DRAFT)
Thechaptercontainsmanyexamplesandexercises;thereisnobetterwaytolearntoNLP
thantodiveinandtrytheseyourself.However,beforecontinuingyouneedtoinstallNLTK,
downloadable for free from http://www.nltk.org/. Onceyou’vedone this, install thedata
requiredforthebookbytypingnltk.download()atthePythonprompt,anddownload
thebookcollection.
forustoexplore:
from nltk.book import
*
i.e. loadNLTK’s
book
module,whichcontains
theexamplesyou’llbeworkingwithasyoureadthischapter. Afterprintingawelcomemessage,it
loadsthetextofseveralbooks,includingMobyDick. Typethefollowing,takingcaretogetspelling
andpunctuationexactlyright:
>>> from nltk.book import
*
***
Introductory Examples for the NLTK Book
***
Loading: text1, ..., text8 and sent1, ..., sent8
Type the e name of f the text or sentence to o view it.
Type: ’texts()’ or ’sents()’ to list the e materials.
>>> text1
<Text: Moby y Dick by Herman Melville e 1851>
>>> text2
<Text: Sense and d Sensibility by Jane Austen n 1811>
>>>
Wecanexaminethecontentsofatextinavarietyofways. Aconcordanceviewshowsus every
occurrenceofagivenword,togetherwithsomecontext.Herewelookupthewordmonstrous.
>>> text1.concordance("monstrous")
mong the e former , one was of a most t monstrous size . ... . This came towards us s , o
ION OF THE PSALMS . . " Touching that t monstrous bulk of the whale or ork we have re
all over with a heathenish array y of monstrous clubs s and spears . . Some were thickl
ed as you gazed , and wondered what t monstrous cannibal and savage could ever have
that has survived the flood ; most t monstrous and most mountainous ! That Himmale
they might t scout at Moby Dick as a a monstrous fable e , or r still worse and d more det
ath of Radney .’" CHAPTER 55 Of the e monstrous Pictures of Whales s . I shall ere lo
ling Scenes s . In n connexion with the e monstrous pictures of whales s , I am strongly
>>>
Your Turn: Try seaching for otherwords; youcanuse theup-arrowkeytoaccessthe
previous commandandmodifythewordbeingsearched. Youcanalsotry searcheson
someoftheothertextswehaveincluded.Forexample,searchSenseandSensibilityforthe
wordaffection,usingtext2.concordance("affection").SearchthebookofGenesis
tofindouthowlongsomepeoplelived, using: text3.concordance("lived"). You
couldlookattext4,theUSPresidentialInauguralAddressestoseeexamplesofEnglish
datingbackto1789,andsearchforwordslikenation,terror,god toseehowthesewords
havebeenuseddifferentlyovertime. We’vealsoincludedtext5,theNPSChatCorpus:
searchthisforunconventionalwordslikeim,ur,lol.(Notethatthiscorpusisuncensored!)
Onceyou’vespentafewminutesexaminingthesetexts,wehopeyouhaveanewsenseoftherichness
anddiversityoflanguage. Inthenextchapteryouwilllearnhowtoaccessabroaderrangeoftext,
includingtextinlanguagesotherthanEnglish.
Itisonethingtoautomaticallydetectthataparticularwordoccursinatextandtodisplaysomewords
thatappearinthesamecontext. Wecanalsodeterminethelocationofawordinthetext:howmany
Bird,Klein&Loper
3
December18,2008
C# PDF Convert to Jpeg SDK: Convert PDF to JPEG images in C#.net
NET library to batch convert PDF files to jpg image files. Thumbnails can be created from PDF pages. Password protected PDF document can be converted and changed.
convert password protected pdf to excel online; copy text from protected pdf
VB.NET PDF Convert to Jpeg SDK: Convert PDF to JPEG images in vb.
VB.NET How-to, VB.NET PDF, VB.NET Word, VB.NET Excel Online VB.NET Tutorial for PDF to JPEG (JPG) Conversion in VB Able to convert password protected PDF document
reader save pdf with password; adding password to pdf
1.1.ComputingwithLanguage:TextsandWords
wordsinfromthebeginningitappears.Thispositionalinformationcanbedisplayedusingadispersion
plot. Eachstriperepresentsaninstanceofawordandeachrowrepresentstheentiretext. InFigure
1.1weseesomestrikingpatternsofwordusageoverthelast220years(inanartificialtextconstructed
byjoiningtheaddressesend-to-end).Youcanproducethisplotasshownbelow.Youmightliketotry
differentwords,anddifferenttexts. Canyoupredictthedispersionofawordbeforeyouviewit?As
before,takecaretogetthequotes,commas,bracketsandparenthesesexactlyright.
>>> text4.dispersion_plot(["citizens""democracy""freedom""duties""America"])
>>>
Figure1.1:LexicalDispersionPlotforWordsinUSPresidentialInauguralAddresses
YouneedtohavePython’sNumpyandPylabpackagesinstalledinordertoproducethe
graphicalplotsusedinthisbook.Pleaseseehttp://www.nltk.org/forinstallationinstructions.
Aconcordancepermitsustoseewordsincontext,e.g.wesawthatmonstrousappearedinthecontext
themonstrouspictures.Whatotherwordsappearinthesamecontextsthatmonstrousappearsin?We
canfindoutasfollows:
>>> text1.similar("monstrous")
imperial subtly impalpable pitiable curious abundant perilous
trustworthy untoward singular r lamentable few determined maddens
horrible tyrannical lazy y mystifying christian n exasperate
>>> text2.similar("monstrous")
great very so o good vast a exceedingly y heartily amazingly as sweet
remarkably extremely
>>>
Observethatwegetdifferentresultsfordifferentbooks. MelvilleandAustenusethiswordquite
differently.ForAustenmonstroushaspositiveconnotations,andmightevenfunctionasanintensifier,
likethewordvery.Let’sexaminethecontextsthataresharedbymonstrousandvery
>>> text2.common_contexts(["monstrous", "very"])
be_glad am_glad a_pretty is_pretty a_lucky
>>>
December18,2008
4
Bird,Klein&Loper
C# PDF: C#.NET PDF Document Merging & Splitting Control SDK
SDK > C# > Merge and Split Document(s). "This online guide content PDF document merging & splitting toolkit SDK to split password-protected PDF document using
password on pdf; add copy protection pdf
Online Convert PDF file to Word. Best free online PDF Conversion
Online PDF to Word Converter. Download Free Trial. Convert a PDF File to Word. Just upload your file by clicking on the blue button
break pdf password; copy protected pdf to word converter online
1.LanguageProcessingandPython
NaturalLanguageProcessing(DRAFT)
Your Turn: Pick anotherwordand compare its usage in twodifferent texts, usingthe
similar()andcommon_contexts()methods.
Now,justforfun,let’strygeneratingsomerandomtextinthevariousstyleswehavejustseen.Todo
this,wetypethenameofthetextfollowedbythe“generate”function:
>>> text3.generate()
In the beginning g of his brother is a hairy man , , whose top may reach
unto heaven n ; and ye shall sow the land of Egypt t there was no bread d in
all that he was taken out of the e month , , upon the earth . So shall thy
wages be e ? And they y made their father ; and d Isaac was old , , and kissed
him : and Laban with his s cattle in the midst of the e hands of Esau thy
first born , and d Phichol the chief butler unto his son Isaac , she
>>>
Notethatfirsttimeyourunthis,itisslowbecauseitgathersstatisticsaboutwordsequences. Each
timeyourunit,youwillgetdifferentoutputtext. Nowtrygeneratingrandomtextinthestyleofan
inauguraladdressoraninternetchatroom.Althoughthetextisrandom,itre-usescommonwordsand
phrasesfromthesourcetextandgivesusasenseofitsstyleandcontent.
Whentextisprinted,punctuationhasbeensplitofffromthepreviousword.Althoughthisis
notcorrectformattingforEnglishtext,wedothistomakeitclearthatpunctuationdoesnot
belongtotheword.Thisiscalled“tokenization”,andyouwilllearnaboutitinChapter3.
CountingVocabulary
Themost obviousfactabouttextsthatemergesfromtheprevioussectionisthattheydifferinthe
vocabularytheyuse. Inthissectionwewillseehowtousethecomputer tocountthewordsina
text,inavarietyofusefulways. AsbeforeyouwilljumprightinandexperimentwiththePython
interpreter,eventhoughyoumaynothavestudiedPythonsystematicallyyet.Testyourunderstanding
bymodifyingtheexamples,andtryingtheexercisesattheendofthechapter.
Let’sbeginbyfindingoutthelengthofatextfromstarttofinish,intermsofthewordsandpunctuation
symbolsthatappear.We’llusethetextofMobyDickagain:
>>> len(text1)
260819
>>>
That’saquarterofamillionwordslong!Buthowmanydistinctwordsdoesthistextcontain?Towork
thisoutinPythonwehavetoposethequestionslightlydifferently.Thevocabularyofatextisjustthe
setofwordsthatituses,andinPythonwecanlistthevocabularyof
text3
withthecommand:
set(
text3)
(manyscreensofwordswillflypast).Nowtrythefollowing:
>>> sorted(set(text3))
[’!’, "’", ’(’, ’)’, ’,’, ’,)’, ’.’, ’.)’, ’:’, ’;’, ’;)’, ’?’, ’?)’,
’A’, ’Abel’, ’Abelmizraim’, , ’Abidah’, ’Abide’, ’Abimael’, ’Abimelech’,
’Abr’, ’Abrah’, ’Abraham’, ’Abram’, , ’Accad’, ’Achbor’, ’Adah’, ...]
>>> len(set(text3))
2789
Bird,Klein&Loper
5
December18,2008
Online Convert Word to PDF file. Best free online export docx, doc
Online Word to PDF Converter. Download Free Trial. Convert a Word File to PDF. Just upload your file by clicking on the blue button
pdf passwords; add password to pdf document
.NET PDF SDK - Description of All PDF Processing Control Feastures
Convert PDF to Word (.docx); Convert PDF to HTML; Convert PDF to text; Convert PDF to Jpeg images; More Able to Open password protected PDF; Allow users to add
pdf password recovery; convert password protected pdf to normal pdf online
1.1.ComputingwithLanguage:TextsandWords
>>> len(text3) / / len(set(text3))
16
>>>
Herewecanseeasortedlistofvocabularyitems,beginningwithvariouspunctuationsymbolsand
continuingwithwordsstartingwithA.Allcapitalizedwordsprecedelowercasewords. Wediscover
thesizeofthevocabularyindirectly,byaskingforthelengthoftheset. Therearefewerthan3,000
distinctwordsinthisbook. Finally,wecancalculateameasureofthelexicalrichnessofthetextand
learnthateachwordisused16timesonaverage.
Next,let’sfocusonparticularwords. Wecancounthowoftenawordoccursinatext,andcompute
whatpercentageofthetextistakenupbyaspecificword:
>>> text3.count("smote")
5
>>> 100.0
*
text4.count(’a’) / len(text4)
1.4587672822333748
>>>
YourTurn: Howmanytimesdoesthewordlol appearintext5? Howmuchisthisasa
percentageofthetotalnumberofwordsinthistext?
Youmightliketorepeatsuchcalculationsonseveral texts,butit istedioustokeepretypingit for
differenttexts. Instead,wecancomeupwithourownnameforatask,e.g. “score”,andassociateit
withablockofcode.Nowweonlyhavetotypeashortnameinsteadofoneormorecompletelinesof
Pythoncode,andwecanre-useitasoftenaswelike:
>>> def score(text):
...
return len(text) ) / len(set(text))
...
>>> score(text3)
16
>>> score(text5)
4
>>>
Caution!
ThePythoninterpreterchangesthepromptfrom>>>to... afterencounteringthecolon
attheendofthefirstline.The... promptindicatesthatPythonexpectsanindentedcode
blocktoappearnext.Itisuptoyoutodotheindentation,bytypingfourspaces.Tofinishthe
indentedblockjustenterablankline.
Thekeyword
def
isshortfor“define”,andtheabovecodedefinesafunction:dt“called”score“. We
usedthefunctionbytypingitsname,followedbyanopenparenthesis,thenameofthetext,thena
closeparenthesis. Thisisjustwhatwedidforthe
len
and
set
functionsearlier. Theseparentheses
willshowupoften:theirroleistoseparatethenameofatasksuchas
score
fromthedatathat
thetaskistobeperformedonsuchas
text3
.Functionsareanadvancedconceptinprogramming
andweonlymentionthemattheoutset togivenewcomersasenseofthepowerandcreativityof
programming. Laterwe’llseehowtousesuchfunctionswhentabulatingdata,likeTable1.1. Each
rowofthetablewillinvolvethesamecomputationbutwithdifferentdata,andwe’lldothisrepetitive
workusingfunctions.
December18,2008
6
Bird,Klein&Loper
1.LanguageProcessingandPython
NaturalLanguageProcessing(DRAFT)
Genre
TokenCount
TypeCount
Score
skillandhobbies
82345
11935
6.9
humor
21695
5017
4.3
fiction:science
14470
3233
4.5
press:reportage
100554
14394
7.0
fiction:romance
70022
8452
8.3
religion
39399
6373
6.2
Table 1.1: Lexical Diversity of VariousGenresin the Brown
Corpus
1.2 ACloserLookatPython:TextsasListsofWords
You’veseensomeimportantbuildingblocksofthePythonprogramminglanguage.Let’sreviewthem
systematically.
Lists
Whatisatext? Atonelevel,it isasequenceofsymbolsonapage,suchasthisone. At another
level,itisasequenceofchapters,madeupofasequenceofsections,whereeachsectionisasequence
ofparagraphs,andsoon. However,forourpurposes,wewillthinkofatextasnothingmorethana
sequenceofwordsandpunctuation. Here’showwerepresenttextinPython,inthiscasetheopening
sentenceofMobyDick:
>>> sent1 = = [’Call’’me’’Ishmael’’.’]
>>>
Afterthepromptwe’vegivenanamewemadeup,
sent1
,followedbytheequalssign,andthensome
quotedwords,separatedwithcommas,andsurroundedwithbrackets.Thisbracketedmaterialisknown
asalistinPython:itishowwestoreatext.Wecaninspectitbytypingthename,andwecanaskfor
itslength:
>>> sent1
[’Call’, ’me’, ’Ishmael’, ’.’]
>>> len(sent1)
4
>>> score(sent1)
1
>>>
Wecanevenapplyourown”score“functiontoit.Somemorelistshavebeendefinedforyou,onefor
theopeningsentenceofeachofourtexts,
sent2
&
sent8
.Weinspecttwoofthemhere;youcansee
therestforyourselfusingthePythoninterpreter.
>>> sent2
[’The’, ’family’, ’of’, ’Dashwood’, , ’had’, ’long’,
’been’, ’settled’, ’in’, , ’Sussex’, ’.’]
>>> sent3
Bird,Klein&Loper
7
December18,2008
1.2.ACloserLookatPython:TextsasListsofWords
[’In’, ’the’, , ’beginning’, ’God’, ’created’, ’the’,
’heaven’, ’and’, , ’the’, ’earth’, , ’.’]
>>>
Your Turn: Makeupafewsentencesof yourown, bytypinganame,equalssign, and
alistofwords,likethis:ex1 = [’Monty’’Python’’and’’the’’Holy’
Grail’]RepeatsomeoftheotherPythonoperationswesawaboveinSection1.1,e.g.
sorted(ex1),len(set(ex1)),ex1.count(’the’).
WecanalsodoarithmeticoperationswithlistsinPython.Multiplyingalistbyanumber,e.g.
sent1
*
2
,createsalongerlistwithmultiplecopiesoftheitemsintheoriginallist. Addingtwolists,e.g.
sent4 + sent1
,createsanewlistwitheverythingfromthefirstlist,followedbyeverythingfromthe
secondlist:
>>> sent1
*
2
[’Call’, ’me’, ’Ishmael’, ’.’, ’Call’, ’me’, ’Ishmael’, ’.’]
>>> sent4 + sent1
[’Fellow’, ’-’, ’Citizens’, ’of’, ’the’, , ’Senate’, ’and’, ’of’, ’the’,
’House’, ’of’, ’Representatives’, ’:’, ’Call’, ’me’, ’Ishmael’, ’.’]
>>>
Thisspecialuseoftheadditionoperationiscalledconcatenation;itlinkstheliststogether
intoasinglelist.Wecanconcatenatesentencestobuildupatext.
IndexingLists
Aswehaveseen,atextinPythonisjustalistofwords,representedusingaparticularcombinationof
bracketsandquotes.Justaswithanordinarypageoftext,wecancountupthetotalnumberofwords
using
len(text1)
,andcounttheoccurrencesofaparticularwordusing
text1.count(’heaven
)
.Andjustaswecanpickoutthefirst,tenth,oreven14,278thwordinaprintedtext,wecanidentify
theelementsofalistbytheirnumber,orindex,byfollowingthenameofthetextwiththeindexinside
brackets.Wecanalsofindtheindexofthefirstoccurrenceofanyword:
>>> text4[173]
’awaken’
>>> text4.index(’awaken’)
173
>>>
Indexesturnouttobeacommonwaytoaccessthewordsofatext,ormoregenerallytheelements
ofalist.Pythonpermitsustoaccesssublistsaswell,extractingmanageablepiecesoflanguagefrom
largetexts,atechniqueknownasslicing.
>>> text5[16715:16735]
[’U86’, ’thats’, , ’why’, ’something’, ’like’, ’gamefly’, ’is’, , ’so’, ’good’,
’because’, ’you’, ’can’, , ’actually’, ’play’, ’a’, ’full’, ’game’, ’without’,
’buying’, ’it’]
>>> text6[1600:1625]
[’We’, "’", ’re’, ’an’, ’anarcho’, ’-’, ’syndicalist’, ’commune’, ’.’, ’We’,
’take’, ’it’, , ’in’, ’turns’, ’to’, ’act’, ’as’, ’a’, ’sort’, ’of’, , ’executive’,
’officer’, ’for’, ’the’, , ’week’]
>>>
December18,2008
8
Bird,Klein&Loper
1.LanguageProcessingandPython
NaturalLanguageProcessing(DRAFT)
Indexeshavesomesubtleties,andwe’llexplorethesewiththehelpofanartificialsentence:
>>> sent = = [’word1’’word2’’word3’’word4’’word5’,
...
’word6’, ’word7’, ’word8’, ’word9’, ’word10’,
...
’word11’, ’word12’, ’word13’, ’word14’’word15’,
...
’word16’, ’word17’, ’word18’, ’word19’’word20’]
>>> sent[0]
’word1’
>>> sent[19]
’word20’
>>>
Noticethat ourindexesstartfromzero:
sent
element zero,written
sent[0]
, isthefirstword,
word1’
,while
sent
element19is
’word20’
.Thereasonissimple:themomentPythonaccessesthe
contentofalistfromthecomputer’smemory,itisalreadyatthefirstelement;wehavetotellithow
manyelementsforwardtogo.Thus,zerostepsforwardleavesitatthefirstelement.
Thisisinitiallyconfusing,buttypicalofmodernprogramminglanguages. You’llquicklyget
thehangofthisifyou’vemasteredthesystemofcountingcenturieswhere19XYisayearin
the20thcentury,orifyouliveinacountrywherethefloorsofabuildingarenumberedfrom
1,andsowalkingupn−1flightsofstairstakesyoutoleveln.
Now,ifwetellittogotoofar,byusinganindexvaluethatistoolarge,wegetanerror:
>>> sent[20]
Traceback (most recent call l last):
File "<stdin>", line 1, in ?
IndexError: list index out of range
>>>
Thistimeitisnotasyntaxerror,fortheprogramfragmentissyntacticallycorrect. Instead,itisa
runtimeerror,anditproducesa
Traceback
messagethatshowsthecontextoftheerror,followedby
thenameoftheerror,
IndexError
,andabriefexplanation.
Let’stakeacloserlookatslicing,usingourartificialsentenceagain:
>>> sent[17:20]
[’word18’, ’word19’, ’word20’]
>>> sent[17]
’word18’
>>> sent[18]
’word19’
>>> sent[19]
’word20’
>>>
Thus,theslice
17:20
includes
sent
elements17,18,and19. Byconvention,
m:n
meanselements
m&n-1. Wecanomitthefirstnumberiftheslicebeginsatthestartofthelist,andwecanomitthe
secondnumberiftheslicegoestotheend:
>>> sent[:3]
[’word1’, ’word2’, ’word3’]
>>> text2[141525:]
[’among’, ’the’, , ’merits’, ’and’, ’the’, , ’happiness’, ’of’, , ’Elinor’, ’and’, ’Marianne’,
’,’, ’let’, , ’it’, ’not’, , ’be’, ’ranked’, , ’as’, ’the’, ’least’, ’considerable’, ’,’,
’that’, ’though’, ’sisters’, ’,’, ’and’, , ’living’, ’almost’, ’within’, ’sight’, ’of’,
’each’, ’other’, , ’,’, ’they’, ’could’, ’live’, ’without’, ’disagreement’, ’between’,
Bird,Klein&Loper
9
December18,2008
1.2.ACloserLookatPython:TextsasListsofWords
’themselves’, ’,’, ’or’, , ’producing’, ’coolness’, ’between’, ’their’, , ’husbands’, ’.’,
’THE’, ’END’]
>>>
Wecanmodifyanelementofalistbyassigningtooneofitsindexvalues,e.g. putting
sent[0]
on
theleftoftheequalssign.Wecanalsoreplaceanentireslicewithnewmaterial:
>>> sent[0] = ’First Word’
>>> sent[19] = ’Last Word’
>>> sent[1:19] = = [’Second Word’’Third Word’]
>>> sent
[’First Word’, ’Second Word’, , ’Third Word’, ’Last Word’]
>>>
Takeafewminutestodefineasentenceofyourownandmodifyindividualwordsandgroupsofwords
(slices)usingthesamemethodsusedabove.Checkyourunderstandingbytryingtheexercisesonlists
attheendofthischapter.
Variables
FromthestartofSection1.1,youhavehadaccesstextscalled
text1
,
text2
,andsoon. Itsaveda
lotoftypingtobeabletorefertoa250,000-wordbookwithashortnamelikethis!Ingeneral,wecan
makeupnamesforanythingwecaretocalculate.Wedidthisourselvesintheprevioussections,e.g.
definingavariable
sent1
asfollows:
>>> sent1 = [’Call’’me’’Ishmael’’.’]
>>>
Suchlineshavetheform: variable=expression. Pythonwillevaluatetheexpression,andsaveits
resulttothevariable. Thisprocessiscalledassignment. Itdoesnotgenerateanyoutput;youhave
totypethevariableonalineofitsowntoinspectitscontents. Theequalssignisslightlymisleading,
sinceinformationiscopiedfromtherightsidetotheleft. Itmighthelptothinkofitasaleft-arrow.
Thevariablecanbeanythingyoulike,e.g.
my_sent
,
sentence
,
xyzzy
. Itmuststartwithaletter,
andcanincludenumbersandunderscores.Herearesomeexamplesofvariablesandassignments:
>>> mySent = [’Bravely’’bold’’Sir’’Robin’’,’’rode’’forth’,
...
’from’, ’Camelot’, ’.’]
>>> noun_phrase = mySent[1:4]
>>> noun_phrase
[’bold’, ’Sir’, ’Robin’]
>>> wOrDs = sorted(noun_phrase)
>>> wOrDs
[’Robin’, ’Sir’, , ’bold’]
>>>
ItisgoodtochoosemeaningfulvariablenamestohelpyouandanyonewhoreadsyourPythoncode
tounderstandwhatyourcodeismeanttodo. Pythondoesnottrytomakesenseofthenames;it
blindlyfollowsyourinstructions,anddoesnotobjectifyoudosomethingconfusing,suchas
one =
’two’
or
two = 3
.AvariablenamecannotbeanyofPython’sreservedwords,suchas
if
,
not
,and
import
.Ifyouuseareservedword,Pythonwillproduceasyntaxerror:
>>> not ’Camelot’
File "<stdin>", line 1
December18,2008
10
Bird,Klein&Loper
Documents you may be interested
Documents you may be interested