在nltk中扩展类。- Python

睡觉

目的是在中的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.WordNetCorpusReaderhttp://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] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

在nltk中扩展类。- Python

来自分类Dev

在python 2.7中扩展类,使用super()

来自分类Dev

扩展超类,在Python中调用子类

来自分类Dev

在Python中扩展类层次结构

来自分类Dev

在Python中扩展类时执行代码

来自分类Dev

python OOP:扩展类

来自分类Dev

在Python 3中扩展类并使用__init__构造它

来自分类Dev

如何在Python中修改或扩展标准套接字类?

来自分类Dev

在Python中扩展现有的类实例

来自分类Dev

在Python中扩展类时应将什么传递给super()?

来自分类Dev

如何让子类扩展python中超类的方法

来自分类Dev

在 Python 中的项目级别扩展嵌套类列表

来自分类Dev

如何使用Python nltk的ProbDistI类

来自分类Dev

Python(NLTK)中的成分树

来自分类Dev

父类的Python扩展方法

来自分类Dev

用模块扩展python类

来自分类Dev

扩展 Python 类的 init 方法

来自分类Dev

扩展 Python 类的正确方法

来自分类Dev

Python-遍历扩展类的类列表?

来自分类Dev

让C扩展类从Python类继承

来自分类Dev

Python类扩展协议并实现类

来自分类Dev

类中的Python类

来自分类Dev

有没有办法扩展swig / python中的所有类?

来自分类Dev

如何使用 JPype 作为其与 Java 的接口机制在 Python 中扩展 Java 类?

来自分类Dev

Python在nltk.tree中定位单词

来自分类Dev

在Python 3.4中安装NLTK时出错

来自分类Dev

NLTK,Python中的FCFG错误。语法问题

来自分类Dev

Azure函数python中的nltk路径

来自分类Dev

在nltk python中查找单词词干