python nltk 基本操作
2015-04-22 16:08
183 查看
分词
nltk.sent_tokenize(text) #按句子分割nltk.word_tokenize(sentence) #分词
nltk的分词是句子级别的,所以对于一篇文档首先要将文章按句子进行分割,然后句子进行分词:
词性标注
nltk.pos_tag(tokens) #对分词后的句子进行词性标注[code]tags = [nltk.pos_tag(tokens) for tokens in words] >>>tags [[('This', 'DT'), ('is', 'VBZ'), ('a', 'DT'), ('text', 'NN'), ('for', 'IN'), ('test', 'NN'), ('.', '.')], [('And', 'CC'), ('I', 'PRP'), ('want', 'VBP'), ('to', 'TO'), ('learn', 'VB'), ('how', 'WRB'), ('to', 'TO'), ('use', 'VB'), ('nltk', 'NN'), ('.', '.')]]
分块 Chunking
用于实体识别的基本技术是分块,可以理解为把对个token组成词组。名词短语分块
NP-chunking,寻找单独名词短语对应的块。为了创建NP分块,首先需要定义分块语法,规定句子如何分块。下面使用的是一个简单的正则,这条规则规定由可选的且后面跟着任意数量形容词(JJ)的限定词(DJ)和名词(NN)组成。[code]>>> text = "the little yellow dog barked at the cat" >>> sentence = nltk.word_tokenize(text) >>> sentence = nltk.pos_tag(sentence) >>> sentence [('the', 'DT'), ('little', 'JJ'), ('yellow', 'NN'), ('dog', 'NN'), ('barked', 'VBD'), ('at', 'IN'), ('the', 'DT'), ('cat', 'NN')] >>> grammar = "NP: {<DT>?<JJ>*<NN>}" >>> cp = nltk.RegexpParser(grammar) >>> result = cp.parse(sentence) >>> result Tree('S', [Tree('NP', [('the', 'DT'), ('little', 'JJ'), ('yellow', 'NN')]), Tree('NP', [('dog', 'NN')]), ('barked', 'VBD'), ('at', 'IN'), Tree('NP', [('the', 'DT'), ('cat', 'NN')])]) >>> result.draw()
命名实体识别
ref: http://www.pythontip.com/blog/post/10012/相关文章推荐
- Python数据类型中的字典-创建和基本操作
- Python-OpenCV 处理图像(一):基本操作
- Python 列表(list)、字典(dict)、字符串(string)常用基本操作小结
- Python 列表(list)、字典(dict)、字符串(string)常用基本操作小结
- Python-字典的基本操作
- 零基础学python-8.6 字典的基本操作与原处修改
- python3全栈开发-并发编程,多进程的基本操作
- 零基础学python-8.2 列表的基本操作、迭代和解析
- Python图像处理的基本操作(一)
- python String 的一些基本操作
- 【图像处理】Python-Image模块基本的图像处理操作
- python操作excel的基本语句
- Python SQLAlchemy基本操作和常用技巧(包含大量实例,非常好)
- Python中的基本list操作
- Python数据分析库pandas基本操作
- Python SQLAlchemy基本操作和常用技巧(包含大量实例,非常好)
- linux 一些基本操作(设置默认python版本)
- Python 字符串基本操作
- [Python]python正则入门--基本元字符匹配操作
- python基本操作总结(二)