# 解决: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()
看了网上那些都没有解决我的问题,然后看了代码发现大佬这么写。。 把它摘录过来,感觉特牛,整我互联网还没找到这样的文章