目的是在中的wordnet类中添加其他功能nltk
,例如:
from nltk.corpus import wordnet
class WN(wordnet):
def foobar(self):
print 'foobar'
x = WN
WN.foobar()
但它给出了一个错误:
Traceback (most recent call last):
File "/home/alvas/workspace/pybabel-fresh/babelnet/utils/pybabel_WordNet.py", line 5, in <module>
class WN(wordnet):
File "/usr/local/lib/python2.7/dist-packages/nltk/corpus/util.py", line 44, in __init__
assert issubclass(reader_cls, CorpusReader)
TypeError: Error when calling the metaclass bases
issubclass() arg 1 must be a class
因此,我尝试使用nltk.corpus.reader.WordNetCorpusReader
(http://www.nltk.org/_modules/nltk/corpus/reader/wordnet.html#WordNetCorpusReader):
from nltk.corpus.reader import WordNetCorpusReader
class WN(WordNetCorpusReader):
def __init__(self):
self = WN.__init__()
def foobar(self):
return "foobar"
x = WN
x.foobar()
仍然好像如果我使用WordNetCorpusReader,则需要实例化它,所以我得到了:
Traceback (most recent call last):
File "/home/alvas/workspace/pybabel-fresh/babelnet/utils/pybabel_WordNet.py", line 13, in <module>
x.foobar()
TypeError: unbound method foobar() must be called with WN instance as first argument (got nothing instead)
然后我尝试了:
from nltk.corpus.reader import WordNetCorpusReader
class WN(WordNetCorpusReader):
def foobar(self):
return "foobar"
x = WN
for i in x.all_synsets():
print i
[出去]:
Traceback (most recent call last):
File "/home/alvas/workspace/pybabel-fresh/babelnet/utils/pybabel_WordNet.py", line 10, in <module>
for i in x.all_synsets():
TypeError: unbound method all_synsets() must be called with WN instance as first argument (got nothing instead)
如何使用新功能扩展nltk wordnet API?注意:其目的是使用新功能创建一个新类。
您的第二次尝试似乎最接近。您的构造函数存在问题:
class WN(WordNetCorpusReader): def __init__(self): self = WN.__init__() # needs an instance as the first argument, recursive, and no need to assign to self
该__init__
方法需要一个实例作为其第一个参数(在此处self
),此外,您正在调用__init__
错误类的方法。这将导致RuntimeError: maximum recursion depth exceeded
错误。最后,您只需要调用该方法即可;您无需将方法的结果分配给self
。
我认为您打算这样做:
from nltk.corpus.reader import WordNetCorpusReader
import nltk
class WN(WordNetCorpusReader):
def __init__(self, *args):
WordNetCorpusReader.__init__(self, *args)
def foobar(self):
return "foobar"
但是要注意的是,您将需要将必需的WordNetCorpusReader.__init__
args传递给新类。在我的版本中nltk
,这意味着您将需要传递一个root
参数,如下所示:
>>> x = WN(nltk.data.find('corpora/wordnet'))
>>> x.foobar()
'foobar'
>>> x.synsets('run')
[Synset('run.n.01'), Synset('test.n.05'), ...]
一种更有效的方法
一种更有效的方法来做同样的事情,如下所示:
class WN(WordNetCorpusReader):
root = nltk.data.find('corpora/wordnet') # make root a class variable, so you only need to load it once
def __init__(self, *args, **kwargs):
WordNetCorpusReader.__init__(self, WN.root, *args, **kwargs) # add root yourself here, so no arguments are required
def foobar(self):
return "foobar"
现在测试一下:
>>> x = WN()
>>> x.foobar()
'foobar'
>>> x.synsets('run')
[Synset('run.n.01'), Synset('test.n.05'), ...]
顺便说一句,我很高兴看到您在nltk
标签上的工作。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句