Python自然语言处理--处理原始文本

本文介绍使用NLTK库进行文本处理的基本操作,包括从网页抓取数据、分词、创建文本对象等步骤,并展示了如何利用BeautifulSoup清理HTML内容。

NLTK API

导入工具包

>>from __future__ import division
>>import nltk,re,pprint

英文分词

>>raw = "I am a student......"
>>tokens = nltk.word_tokenize(raw)
# type(tokens)==list
# 分词列表创建nltk文本
>>text = nltk.Text(tokens)

从网页中提取文本

>>url = 'http://www.lfd.uci.edu/~gohlke/pythonlibs/#matplotlib'
>>html = urlopen(url).open()
>>raw = nltk.clean_html(html)
# clean_html()和clean_url()函数均已弃用,改用
# BeautifulSoup.get_text()对文本进行提取
>>from bs4 import BeautifulSoup
>>soup = BeautifulSoup(html)
>>raw = soup.get_text()
>>tokens = nltk.word_tokenize(raw)[10:21]
>>text = nltk.Text(tokens)
# 建立词汇表
>>words = [w.lower() for w in text]
>>vocab = sorted(set(words))
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值