NLTK学习(一)

来源:互联网 发布:linux locale 修改 编辑:程序博客网 时间:2024/05/20 05:04

Python简单入门 

交互式开发环境(IDLE)

Windows下在开始菜单应用程序那里可以找到,而Unix在shell下输入idle即可运行。我选择的是Sublime Text的代码编辑器,下载安装插件SublimeREPL,下载完之后,在【Tool】【SublimeREPL】【Python】【Python】启动交互式开发环境

>>> 1+5*2-38>>> 1/30>>> 1.0/30.3333333333333333>>> from __future__ import division >>> 1/30.3333333333333333>>> 1+  File "<stdin>", line 1    1+     ^SyntaxError: invalid syntax

需要注意的是 from __future__ import division 可以获得我们平时期望的除法。SyntaxError表示产生了一个语法错误。


NLTK入门

在Windows下可以直接在cmd下运行下面的代码。

pip install nltkpython>>> import nltk>>> nltk.download()
然后选择下载NLTK的图书合集,提示:u"panlex_lite"的这个包大概有2个多G,下起来比较慢。

下载成功后,执行

>>> from nltk.book import **** Introductory Examples for the NLTK Book ***Loading text1, ..., text9 and sent1, ..., sent9Type the name of the text or sentence to view it.Type: 'texts()' or 'sents()' to list the materials.text1: Moby Dick by Herman Melville 1851text2: Sense and Sensibility by Jane Austen 1811text3: The Book of Genesistext4: Inaugural Address Corpustext5: Chat Corpustext6: Monty Python and the Holy Grailtext7: Wall Street Journaltext8: Personals Corpustext9: The Man Who Was Thursday by G . K . Chesterton 1908
然后输入

>>> text1<Text: Moby Dick by Herman Melville 1851>
或者输入

>>> texts()text1: Moby Dick by Herman Melville 1851text2: Sense and Sensibility by Jane Austen 1811text3: The Book of Genesistext4: Inaugural Address Corpustext5: Chat Corpustext6: Monty Python and the Holy Grailtext7: Wall Street Journaltext8: Personals Corpustext9: The Man Who Was Thursday by G . K . Chesterton 1908
现在我们可以用Python和这些数据了
>>> text1.concordance("monstrous")Displaying 11 of 11 matches:ong the former , one was of a most monstrous size . ... This came towards us , ON OF THE PSALMS . " Touching that monstrous bulk of the whale or ork we have rll over with a heathenish array of monstrous clubs and spears . Some were thickd as you gazed , and wondered what monstrous cannibal and savage could ever havthat has survived the flood ; most monstrous and most mountainous ! That Himmalthey might scout at Moby Dick as a monstrous fable , or still worse and more deth of Radney .'" CHAPTER 55 Of the Monstrous Pictures of Whales . I shall ere ling Scenes . In connexion with the monstrous pictures of whales , I am stronglyere to enter upon those still more monstrous stories of them which are to be foght have been rummaged out of this monstrous cabinet there is no telling . But of Whale - Bones ; for Whales of a monstrous size are oftentimes cast up dead u
比如:
>>> text2.concordance("affection")Displaying 25 of 79 matches:, however , and , as a mark of his affection for the three girls , he left themt . It was very well known that no affection was ever supposed to exist betweenderation of politeness or maternal affection on the side of the former , the twd the suspicion -- the hope of his affection for me may warrant , without impruhich forbade the indulgence of his affection . She knew that his mother neitherrd she gave one with still greater affection . Though her late conversation wit can never hope to feel or inspire affection again , and if her home be uncomfom of the sense , elegance , mutual affection , and domestic comfort of the fami, and which recommended him to her affection beyond every thing else . His sociween the parties might forward the affection of Mr . Willoughby , an equally st the most pointed assurance of her affection . Elinor could not be surprised athe natural consequence of a strong affection in a young and ardent mind . This  opinion . But by an appeal to her affection for her mother , by representing t every alteration of a place which affection had established as perfect with hie will always have one claim of my affection , which no other can possibly sharf the evening declared at once his affection and happiness . " Shall we see youause he took leave of us with less affection than his usual behaviour has shewnness ." " I want no proof of their affection ," said Elinor ; " but of their enonths , without telling her of his affection ;-- that they should part without ould be the natural result of your affection for her . She used to be all unresdistinguished Elinor by no mark of affection . Marianne saw and listened with ith no inclination for expense , no affection for strangers , no profession , antill distinguished her by the same affection which once she had felt no doubt oal of her confidence in Edward ' s affection , to the remembrance of every mark was made ? Had he never owned his affection to yourself ?" " Oh , no ; but if >>> text3.concordance("lived")Displaying 25 of 38 matches:ay when they were created . And Adam lived an hundred and thirty years , and beughters : And all the days that Adam lived were nine hundred and thirty yea andnd thirty yea and he died . And Seth lived an hundred and five years , and begave years , and begat Enos : And Seth lived after he begat Enos eight hundred anwelve years : and he died . And Enos lived ninety years , and begat Cainan : An years , and begat Cainan : And Enos lived after he begat Cainan eight hundred ive years : and he died . And Cainan lived seventy years and begat Mahalaleel :rs and begat Mahalaleel : And Cainan lived after he begat Mahalaleel eight hundyears : and he died . And Mahalaleel lived sixty and five years , and begat Jars , and begat Jared : And Mahalaleel lived after he begat Jared eight hundred aand five yea and he died . And Jared lived an hundred sixty and two years , ando years , and he begat Eno And Jared lived after he begat Enoch eight hundred y and two yea and he died . And Enoch lived sixty and five years , and begat Met ; for God took him . And Methuselah lived an hundred eighty and seven years ,  , and begat Lamech . And Methuselah lived after he begat Lamech seven hundred nd nine yea and he died . And Lamech lived an hundred eighty and two years , anch the LORD hath cursed . And Lamech lived after he begat Noah five hundred ninnaan shall be his servant . And Noah lived after the flood three hundred and fixad two years after the flo And Shem lived after he begat Arphaxad five hundredat sons and daughters . And Arphaxad lived five and thirty years , and begat Saars , and begat Salah : And Arphaxad lived after he begat Salah four hundred anbegat sons and daughters . And Salah lived thirty years , and begat Eber : And y years , and begat Eber : And Salah lived after he begat Eber four hundred and begat sons and daughters . And Eber lived four and thirty years , and begat Pey years , and begat Peleg : And Eber lived after he begat Peleg four hundred an
等等。

查找出现位置相似的词,比如:monstrous 出现在the __ pictures 和 the __ size 的相似上下文中,即

>>> text1.similar("monstrous")imperial subtly impalpable pitiable curious abundant periloustrustworthy untoward singular lamentable few determined maddenshorrible tyrannical lazy mystifying christian exasperate>>> text1.similar(u"你好")No matches





0 0
原创粉丝点击