Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/293.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/jpa/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何调用以self作为参数的函数?_Python_Nltk - Fatal编程技术网

Python 如何调用以self作为参数的函数?

Python 如何调用以self作为参数的函数?,python,nltk,Python,Nltk,我在网上找到了一些代码,可以帮助我完成一个项目,问题是它使用了两个参数,其中一个是self。我不知道如何实现这样的功能,如果有人能解释一下,我将不胜感激 这是密码 #code found online def common_contexts(self, words, num=20): """ Find contexts where the specified words appear; list most frequent common contexts first.

我在网上找到了一些代码,可以帮助我完成一个项目,问题是它使用了两个参数,其中一个是self。我不知道如何实现这样的功能,如果有人能解释一下,我将不胜感激

这是密码

#code found online
def common_contexts(self, words, num=20):
    """
    Find contexts where the specified words appear; list
    most frequent common contexts first.

    :param word: The word used to seed the similarity search
    :type word: str
    :param num: The number of words to generate (default=20)
    :type num: int
    :seealso: ContextIndex.common_contexts()
    """
    if '_word_context_index' not in self.__dict__:
        #print('Building word-context index...')
        self._word_context_index = ContextIndex(self.tokens,
                                                key=lambda s:s.lower())

    try:
        fd = self._word_context_index.common_contexts(words, True)
        if not fd:
            print("No common contexts were found")
        else:
            ranked_contexts = [w for w, _ in fd.most_common(num)]
            print(tokenwrap(w1+"_"+w2 for w1,w2 in ranked_contexts))

    except ValueError as e:
        print(e)
#How I tried to call it, lines[1] is an array containing words
df=common_contexts(lines[1])
print(df)
我只想看到输出,这样我就可以知道我是否可以在我的项目中使用这段代码,这是一种方法。您正在查看,这是
ContextIndex.common_contexts()

您需要创建
ContextIndex
类的instace,然后调用该类的方法:

ci = ContextIndex(tokens)
df = ci.common_contexts(lines[1])
在这里,
common_context
被绑定到
ci
实例。然后调用该方法会导致Python为
self
参数提供一个值;调用方法的实例。

这是一个方法。您正在查看,这是
ContextIndex.common_contexts()

您需要创建
ContextIndex
类的instace,然后调用该类的方法:

ci = ContextIndex(tokens)
df = ci.common_contexts(lines[1])
在这里,
common_context
被绑定到
ci
实例。然后调用该方法会导致Python为
self
参数提供一个值;调用方法的实例。

这是一个方法。您正在查看,这是
ContextIndex.common_contexts()

您需要创建
ContextIndex
类的instace,然后调用该类的方法:

ci = ContextIndex(tokens)
df = ci.common_contexts(lines[1])
在这里,
common_context
被绑定到
ci
实例。然后调用该方法会导致Python为
self
参数提供一个值;调用方法的实例。

这是一个方法。您正在查看,这是
ContextIndex.common_contexts()

您需要创建
ContextIndex
类的instace,然后调用该类的方法:

ci = ContextIndex(tokens)
df = ci.common_contexts(lines[1])

在这里,
common_context
被绑定到
ci
实例。然后调用该方法会导致Python为
self
参数提供一个值;调用方法的实例。

通常,这不是一个函数,而是一个方法。它将在一个类上,您可以在该类的实例上调用它。在这种情况下,Python为您提供了
self
。通常,这不是一个函数,而是一个方法。它将在一个类上,您可以在该类的实例上调用它。在这种情况下,Python为您提供了
self
。通常,这不是一个函数,而是一个方法。它将在一个类上,您可以在该类的实例上调用它。在这种情况下,Python为您提供了
self
。通常,这不是一个函数,而是一个方法。它将在一个类上,您可以在该类的实例上调用它。在这种情况下,Python为您提供了
self
。谢谢伙计,我觉得没有注意到我在看nltk源代码有点简单。谢谢伙计,我觉得没有注意到我在看nltk源代码有点简单。谢谢伙计,我觉得没有注意到我在看nltk源代码有点简单。谢谢伙计,我觉得有点简单,因为我没有注意到我在看nltk源代码。