lang.wordnet 模块

此模块包含低级函数和高级类,用于将wordnet prolog下载的prolog文件“wn_s.pl”解析为适合查找同义词和执行查询扩展的对象。

http://wordnetcode.princeton.edu/3.0/WNprolog-3.0.tar.gz

叙词表

class whoosh.lang.wordnet.Thesaurus

表示WordNet同义词数据库,要么从wn_s.pl prolog文件加载到内存中,要么存储在磁盘上的whoosh索引中。

此类允许您将prolog文件“wn_s.pl”从wordnet prolog download解析为适合查找同义词和执行查询扩展的对象。

http://wordnetcode.princeton.edu/3.0/WNprolog-3.0.tar.gz

从wn_s.pl文件加载同义词库对象…

>>> t = Thesaurus.from_filename("wn_s.pl")

要将内存中的同义词库保存到whoosh索引中…

>>> from whoosh.filedb.filestore import FileStorage
>>> fs = FileStorage("index")
>>> t.to_storage(fs)

从whoosh索引加载同义词库对象…

>>> t = Thesaurus.from_storage(fs)

因此,同义词库对象可以通过两种方式使用:

  • 将wn_s.pl文件解析到内存中(同义词库.from_x),然后在内存中查找同义词。这对于解析文件有启动成本,并且使用相当多的内存来存储两个大型字典,但是同义词查找非常快。

  • 将wn_s.pl文件解析到内存中(同义词库.from_filename),然后将其保存到索引(to_storage)。从那时起,从保存的索引中打开同义词库(同义词库.from_storage)。这对于存储索引有很大的成本,但是之后打开同义词库(比重新解析文件)要快一些,但查找同义词要慢一些。

以下是我的(快速)Windows机器上各种任务的时间安排,这可能给出内存与磁盘的相对成本。

任务

大约时间

正在分析wn_s.pl文件

1.045

保存到磁盘索引

13.084

从磁盘索引加载

0.082

查找“light”的同义词(在内存中)

0.0011

查找“light”的同义词(从磁盘加载)

0.0028

基本上,如果你能花足够的内存来解析同义词库,然后缓存它,那么速度会更快。否则,请使用磁盘索引。

classmethod from_file(fileobj)

从给定的类似文件的对象创建同义词库对象,该对象应包含wordnet wn_s.pl文件。

>>> f = open("wn_s.pl")
>>> t = Thesaurus.from_file(f)
>>> t.synonyms("hail")
['acclaim', 'come', 'herald']
classmethod from_filename(filename)

从给定的文件名创建同义词库对象,该文件应包含wordnet wn's.pl文件。

>>> t = Thesaurus.from_filename("wn_s.pl")
>>> t.synonyms("hail")
['acclaim', 'come', 'herald']
classmethod from_storage(storage, indexname='THES')

从给定的存储对象创建同义词库对象,该对象应包含同义词库.to_storage()创建的索引。

>>> from whoosh.filedb.filestore import FileStorage
>>> fs = FileStorage("index")
>>> t = Thesaurus.from_storage(fs)
>>> t.synonyms("hail")
['acclaim', 'come', 'herald']
参数
  • storage -- A whoosh.store.Storage 从中加载索引的对象。

  • indexname -- 索引的名称。这允许您在同一个存储对象中存储多个索引。

synonyms(word)

返回给定单词的同义词列表。

>>> thesaurus.synonyms("hail")
['acclaim', 'come', 'herald']
to_storage(storage, indexname='THES')

根据从WordNet文件加载的同义词在给定存储对象中创建AM索引。

>>> from whoosh.filedb.filestore import FileStorage
>>> fs = FileStorage("index")
>>> t = Thesaurus.from_filename("wn_s.pl")
>>> t.to_storage(fs)
参数
  • storage -- A whoosh.store.Storage 保存索引的对象。

  • indexname -- 索引的名称。这允许您在同一个存储对象中存储多个索引。

低级功能

whoosh.lang.wordnet.parse_file(f)

分析wordnet wn_s.pl prolog文件并返回两个字典:word2nums和num2words。

whoosh.lang.wordnet.synonyms(word2nums, num2words, word)

使用word2nums和num2words dicts查找给定单词的同义词。返回同义词字符串列表。

whoosh.lang.wordnet.make_index(storage, indexname, word2nums, num2words)

在给定的存储对象中创建一个whoosh索引,其中包含取自word2nums和num2words的同义词。返回索引对象。