pos-tagging-explore.py 文件源码

python
阅读 48 收藏 0 点赞 0 评论 0

项目:Deep-Learning-with-Keras 作者: PacktPublishing 项目源码 文件源码
def build_tensor(filename, numrecs, word2index, maxlen, 
                 make_categorical=False):
    data = np.empty((numrecs, ), dtype=list)
    fin = open(filename, "rb")
    i = 0
    for line in fin:
        wids = []
        for word in line.strip().split():
            if word2index.has_key(word):
                wids.append(word2index[word])
            else:
                wids.append(word2index["UNK"])
        if make_categorical:
            data[i] = np_utils.to_categorical(
                wids, num_classes=len(word2index))
        else:
            data[i] = wids
        i += 1
    fin.close()
    pdata = sequence.pad_sequences(data, maxlen=maxlen)
    return pdata
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号