python自然語言處理學習筆記第三章__python

來源:互聯網
上載者:User
從本章開始往後我們的例子程式將假設你以下面的匯入語句開始你的交
互式會話或程式:
>>> from __future__ import division

>>> import nltk, re, pprint


讀取網路上儲存的資料:

>>> from __future__ import division
>>> import nltk,re,pprint
>>> from urllib import urlopen
>>> url = url = "http://www.gutenberg.org/files/2554/2554.txt"
>>> raw = urlopen(url).read()
>>> type(raw)
<type 'str'>
>>> len(raw)
1176893
>>> raw[:75]
'The Project Gutenberg EBook of Crime and Punishment, by Fyodor Dostoevsky\r\n'


如果使用代理,則用下面代碼:

如果你使用的網際網路代理程式Python不能正確檢測出來,你可能需要用下面的方法手動指定代理:

>>> proxies = {'http': 'http://www.someproxy.com:3128'}
>>> raw = urlopen(url, proxies=proxies).read()


對讀入的資料處理:

>>> tokens = nltk.word_tokenize(raw)
>>> type(tokens)
<type 'list'>
>>> len(tokens)
244484
>>> tokens[:10]
['The', 'Project', 'Gutenberg', 'EBook', 'of', 'Crime', 'and', 'Punishment', ',', 'by']
>>> tokens[:15]
['The', 'Project', 'Gutenberg', 'EBook', 'of', 'Crime', 'and', 'Punishment', ',', 'by', 'Fyodor', 'Dostoevsky', 'This', 'eBook', 'is']


對得到的文本進一步處理和操作:

>>> text = nltk.Text(tokens)
>>> type(text)
<class 'nltk.text.Text'>
>>> text[1020:1060]
['had', 'successfully', 'avoided', 'meeting', 'his', 'landlady', 'on', 'the', 'staircase.', 'His', 'garret', 'was', 'under', 'the', 'roof', 'of', 'a', 'high', ',', 'five-storied', 'house', 'and', 'was', 'more', 'like', 'a', 'cupboard', 'than', 'a', 'room.', 'The', 'landlady', 'who', 'provided', 'him', 'with', 'garret', ',', 'dinners', ',']
>>> text.collocations()
Building collocations list
Katerina Ivanovna; Pyotr Petrovitch; Pulcheria Alexandrovna; Avdotya
Romanovna; Marfa Petrovna; Rodion Romanovitch; Sofya Semyonovna; old
woman; Project Gutenberg-tm; Porfiry Petrovitch; Amalia Ivanovna;
great deal; Project Gutenberg; Andrey Semyonovitch; Nikodim Fomitch;
young man; Dmitri Prokofitch; n't know; Ilya Petrovitch; Good heavens
>>> raw.find("PART I")
5338
>>> raw.rfind("End of Project Gutenberg's Crime")
1157743
>>> raw = raw[5303:1157681]
>>> raw.find("PART I")
35




查看網路上html格式的檔案。

>>> url = "http://news.bbc.co.uk/2/hi/health/2284783.stm"
>>> html = urlopen(url).read()
>>> html[:60]
'<!doctype html public "-//W3C//DTD HTML 4.0 Transitional//EN'

通過 print html來列印得到的檔案。



從HTML 中提取文本是極其常見的任務,NLTK 提供了一個輔助函數nltk.clean_html()將HTML 字串作為參數,返回原始文本。然後我們可以對原始文本進行分詞。

>>> raw = nltk.clern_html(html)
Traceback (most recent call last):
  File "<pyshell#38>", line 1, in <module>
    raw = nltk.clern_html(html)
AttributeError: 'module' object has no attribute 'clern_html'
>>> raw = nltk.clean_html(html)     //消除html標記
>>> tokens = nltk.word_tokenize(raw)   //把內容轉換為列表
>>> tokens   //顯示出所有內容


>>> tokens =tokens[96:399]
>>> text = nltk.Text(tokens)
>>> text.concordance('gene')
Building index...
Displaying 4 of 4 matches:
hey say too few people now carry the gene for blondes to last beyond the next 
 have blonde hair , it must have the gene on both sides of the family in the g
ere is a disadvantage of having that gene or by chance. They do n't disappear 
des would disappear is if having the gene was a disadvantage and I do not thin



處理搜尋結果:
























聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.