pickle load文件时报:AttributeError: Can‘t get attribute ‘Vocabulary‘ on <module ‘__main__

文章讲述了如何处理在使用pickle.load加载文件时遇到的VocabularyAttributeError,提供了一段代码示例。
摘要由CSDN通过智能技术生成
# 解决:pickle load文件时报:AttributeError: Can't get attribute 'Vocabulary' on <module '__main__
class Vocabulary(object):    
    def __init__(self):
        self.word2idx = {}
        self.idx2word = {}
        self.idx = 0

    def add_word(self, word):
        if not word in self.word2idx:
            self.word2idx[word] = self.idx
            self.idx2word[self.idx] = word
            self.idx += 1

    def __call__(self, word):
        if not word in self.word2idx:
            return self.word2idx['<unk>']
        return self.word2idx[word]
    
    def get_word(self, id):
        if not id in self.idx2word:
            return '<unk>'
        return self.idx2word[id]

    def __len__(self):
        return len(self.word2idx)


vocab_path='./vocab.pkl'
with open(vocab_path, 'rb') as f:
    vocab = pickle.load(f) 

print(vocab)
exit()

看了网上那些都没有解决我的问题,然后看了代码发现大佬这么写。。 把它摘录过来,感觉特牛,整我互联网还没找到这样的文章

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值