Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/facebook/8.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 TypeError:\uuuu init\uuuuu()缺少2个必需的位置参数_Python_Python 3.x_Path_Typeerror - Fatal编程技术网

Python TypeError:\uuuu init\uuuuu()缺少2个必需的位置参数

Python TypeError:\uuuu init\uuuuu()缺少2个必需的位置参数,python,python-3.x,path,typeerror,Python,Python 3.x,Path,Typeerror,我目前遇到了这个错误。 我不知道这个错误是由什么引起的,因为我已经在我的代码中声明了位置参数path2和path3,但是错误表明缺少这两个参数 错误消息:类型错误:\uuuu init\uuuuu()缺少2个必需的位置参数:“path2”和“path3” 这是我的代码: import os from tqdm import tqdm from utils import SOS, EOS, UNK, process class Corpus(object): def __init__

我目前遇到了这个错误。
我不知道这个错误是由什么引起的,因为我已经在我的代码中声明了位置参数
path2
path3
,但是错误表明缺少这两个参数

错误消息:
类型错误:\uuuu init\uuuuu()缺少2个必需的位置参数:“path2”和“path3”

这是我的代码:

import os
from tqdm import tqdm

from utils import SOS, EOS, UNK, process


class Corpus(object):
    def __init__(self, path, path2, path3, order, lower=False, max_lines=-1):
        self.order = order
        self.lower = lower
        self.max_lines = max_lines
        self.vocab = set()
        self.train = self.tokenize(os.path.join(path), training_set=True)
        self.valid = self.tokenize(os.path.join(path2))
        self.test = self.tokenize(os.path.join(path3))

    def tokenize(self, path, training_set=False):
        """Tokenizes a text file."""
        #assert os.path.exists(path)
        with open(path, path2, path3) as fin:
            num_lines = sum(1 for _ in fin.readlines())
        with open(path, path2, path3, 'r', encoding="utf8") as f:
            words = []
            for i, line in enumerate(tqdm(f, total=num_lines)):
                if self.max_lines > 0 and i > self.max_lines:
                    break
                line = line.strip()
                if not line:
                    continue  # Skip empty lines.
                elif line.startswith('='):
                    continue  # Skip headers.
                else:
                    sentence = (self.order - 1) * [SOS] + \
                        [process(word, self.lower) for word in line.split()] + [EOS]
                    if training_set:
                        words.extend(sentence)
                        self.vocab.update(sentence)
                    else:
                        sentence = [word if word in self.vocab else UNK for word in sentence]
                        words.extend(sentence)
        return words


if __name__ == '__main__':
    path = 'C://Users//supre//Documents//Python Programme//kenlm//wikitext-2//wiki.train.tokens'
    path2 = 'C://Users//supre//Documents//Python Programme//kenlm//wikitext-2//wiki.valid.tokens'
    path3 = 'C://Users//supre//Documents//Python Programme//kenlm//wikitext-2//wiki.test.tokens'
    corpus = Corpus(path, order=3)
    print(len(corpus.test))
    print(corpus.test[:100])

提前感谢您的帮助和建议:)

调用类
语料库的对象时需要传递这些参数

corpus=corpus(path,path2,path3,order=3)

构造函数中参数的名称与传递给它的变量的名称无关,因此您需要将它们全部传递给它,没有获取相同名称变量的机制

class Corpus(object):
    def __init__(self, path, path2, path3, order, lower=False, max_lines=-1):
        self.order = order
        self.lower = lower
        self.max_lines = max_lines
        self.vocab = set()
        self.train = self.tokenize(path, training_set=True)
        self.valid = self.tokenize(path2)
        self.test = self.tokenize(path3)

if __name__ == '__main__':
    pa = 'C://Users//...//kenlm//wikitext-2//wiki.train.tokens'
    pa2 = 'C://Users//...//kenlm//wikitext-2//wiki.valid.tokens'
    pa3 = 'C://Users//...//kenlm//wikitext-2//wiki.test.tokens'

   corpus = Corpus(pa, pa2, pa3, order=3)


另外,
os.path.join
with 1 variable没有什么特别的作用

构造函数等待它们,并且在实例化微粒时不会传递它们。非常感谢“我已经声明了位置参数
path2
path3
”,您已经声明了两个同名变量。构造函数无法猜测这些参数是否是参数。