·搜索文本用到的相关函数
1.concordance() 函数;利用函数concordance可以显示指定单词在文本中出现情况,并显示上下文。
如下例中,查找单词monstrous
>>>from nltk.book import*
....
>>>text1.concordance("monstrous")
Building index...
Displaying 11 of 11matches:ong of former,one of a most monstrous size...This came towards us,ON OF THE PSALMS."Touching that monstrous bulk of the...."
...
2.similar()函数; 可以用来查找与指定单词意义,用法相似的词,可以 可以用在搜索引擎中的相关度识别 功能中
>>>text1.similar("monstrous")
Building word-context index...
subtly impalpable pitiable curious imperrial perilous trustworthy abundant untoward singular lamentable few maddens horrible loving lazy mystifying christan exasperate puzzled
3.common_contexts()函数 ;可以用来研究共用两个或两个以上词汇的上下文
>>>text2.common_contexts(["monstrous","very"])
be_gald am_glad a_pretty a_lucky
>>>
注:有时monstrous和very一样,作强调作用。
4.generate()函数;用于随机生成文本
>>>text3.generate()
In the beginning of his brother is a hairyman,whose top may reach unto heaven...
>>>
注:本文中所有代码均来自《Python自然语言处理》(Steven bird,Ewan Klein&Edward Loper)