python如何提取文本信息?
python如何提取文本信息?
1、信息提取
先用句子分段器将文档的原始文本分成句子,再用记号赋值器将每个句子进一步分成单词。其次,给每一个句子做词性标记。以nltk中的默认工具为例,将句子分段器、分词器、词性标记器连接。
defie_preprocess(document):
#nltk默认的句子分段器
sentences=nltk.sent_tokenize(document)
#nltk默认分词器
sentences=[nltk.word_tokenize(sent)forsentinsentences]
#nltk默认词性标记
sentences=[nltk.pos_tag(sent)forsentinsentences]
2、词块划分
词块划分是实体识别的基础技术,对多个词的顺序进行划分和标记。
如NounPhraseChunking(名词短语词块划分)
使用正则表达式来定义一个语法,来进行名词短语词块的划分
3、开发和评估词块划分器
分区器可以用evaluate()方法评价分区器的性能好坏。
以下是使用一元标记来建立单词块分割器的学习。但是,不是确定每个单词的正确单词性标记,而是根据每个单词的单词性标记,确定正确的单词块标记。
#使用一元标注器建立一个词块划分器。根据每个词的词性标记,尝试确定正确的词块标记。
classUnigramChunker(nltk.ChunkParserI):
#constructor
def__init__(self,train_sents):
#将训练数据转换成适合训练标注器的形式。tree2conlltags()方法将每个词块树映射到一个三元组(word,tag,chunk)的列表
train_data=[[(t,c)forw,t,cinnltk.chunk.tree2conlltags(sent)]
forsentintrain_sents]
#训练一元分块器
#self.tagger=nltk.UnigramTagger(train_data)
#训练二元分块器
self.tagger=nltk.BigramTagger(train_data)
#sentence为一个已标注的句子
defparse(self,sentence):
#提取词性标记
pos_tags=[posfor(word,pos)insentence]
#使用标注器为词性标记标注IOB词块
tagged_pos_tags=self.tagger.tag(pos_tags)
#提取词块标记
chunktags=[chunktagfor(pos,chunktag)intagged_pos_tags]
#将词块标记与原句组合
conlltags=[(word,pos,chunktag)for((word,pos),chunktag)
inzip(sentence,chunktags)]
#转换成词块树
returnnltk.chunk.conlltags2tree(conlltags)
以上就是python提取文本信息的方法,希望能对大家有所帮助,更多Python学习教程请关注IT培训机构:千锋教育。

猜你喜欢LIKE
相关推荐HOT
更多>>
python如何创建UDP服务器和客户端?
python如何创建UDP服务器和客户端?本文教程操作环境:windows7系统、Python3.9.1,DELLG3电脑。1、创建UDP服务端的基本步骤(1)创建套接字并绑定...详情>>
2023-11-10 21:18:09
python条件判断分为哪几类?
python中条件判断分为哪几类?python中条件判断分为单项判断,双向判断和多向判断。1、单项判断单项判断最基本的就是if。stone=6ifstone>=6:prin...详情>>
2023-11-10 19:15:56
python多进程与多线程的简单区分
python多进程与多线程的简单区分1、区别多进程与多线程不同在于,多进程的每一个进程都有一份变量的拷贝,进程之间的操作互不影响。2、实例impo...详情>>
2023-11-10 16:04:13
python-=是什么意思
python中-=是什么意思当我们看到python中类似数学的算数符号时,如果是有加减的出现,就可以明确它们是赋值运算符的一种。本篇所要带来的是-=,...详情>>
2023-11-10 14:36:01热门推荐
技术干货






