python - TypeError:__init__() 缺少 2 个必需的位置参数

标签 python python-3.x path typeerror

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

错误消息:TypeError:__init__() 缺少 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 = Corpus(path, path2, path3, order=3)

关于python - TypeError:__init__() 缺少 2 个必需的位置参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67366751/

相关文章:

python - Gstreamer 消息从视频源(网络摄像头)发出新帧信号

python - 使用 netCDF4 python 模块时出错

java.lang.UnsatisfiedLinkError : no cplex122 in java. 库.path

php - 如何在 Windows 上确定 php.exe 的路径 - 搜索默认路径

string - 固定宽度数字格式化python 3

path - 文件/URL 路径的每个以斜杠分隔的部分的正确名称是什么?

python - 对字典/列表列表中的多个参数进行排序

python - 使用 Scrapy 从动态 JSON 响应中提取内容

python-3.x - python : finding elements of a webpage to scrape in python when page content is loaded using Java script

python - 使用来自其他带字符串的列的条件创建新列