AttributeError - > '过滤器'对象没有属性'追加'

时间:2017-09-14 15:21:26

标签: python python-2.7 python-3.x

我收到属性错误,其中过滤器对象没有属性'追加'。下面是特定方法build_vocab的代码,我在word2id.append(" UNK",(len(word2id),0))中收到错误。

def build_vocab(data, min_freq=2, max_percent=.1):
    word2id = {} # id, counts
    i = 1
    for item in data:
        for word in preprocess(item):
            if word not in word2id:
                word2id[word] = [i, 0]
                i += 1
            word2id[word][1] += 1
    total = len(word2id)
    filtered_ids = int(total * max_percent)
    word2id = sorted(word2id.items(), key=lambda x: x[1][1], reverse=True)[filtered_ids:]
    word2id = filter(lambda x: x[1][1] > min_freq, word2id)
    word2id.append(("UNK", (len(word2id), 0)))
    id2word = [k[0] for k in word2id]
    word2id = {k[0]: (i, k[1][1]) for i, k in enumerate(word2id)}
    return id2word, word2id

谢谢, 麦克

0 个答案:

没有答案