基于TED(en-zh)数据集进行实现Seq2Seq模型

博客文章基于Google Tensorflow实战深度学习框架第九章部分内容,手写一遍代码加强模型理解。

代码在pycharm中编写运行

1、获取词汇表

# -*- coding:utf-8 -*-
'''
@Author:zhangy
@Modify:2019.7.5
'''
import codecs
import collections
from operator import itemgetter

#设置中英文类型,得到不同的vocab
DATA_TYPE = 'english'

if DATA_TYPE == 'chinese':
    RAW_DATA = 'train.txt.zh'
    VOCAB_OUTPUT = 'zh.vocab'
    VOCAB_SIZE = 4000
elif DATA_TYPE == 'english':
    RAW_DATA = 'train.txt.en'
    VOCAB_OUTPUT = 'en.vocab'
    VOCAB_SIZE = 10000

#对单词进行计数
counter = collections.Counter()

with codecs.open(RAW_DATA,'r','utf-8') as f:
    for line in f:
        for word in line.strip().split():
            counter[word] += 1
#依据词频进行降序排列
sorted_word_to_cnt = sorted(counter.items(),key=itemgetter(1),reverse=True)
#拿到对应的单词列表
sorted_word_list = [x[0] for x in sorted_word_to_cnt]

#加入句子的起止符号和unknown符号
sorted_word_list = ["<sos>","<unk>","<eos>"] + sorted_word_list

if len(sorted_word_list) > VOCAB_SIZE:
    sorted_word_list = sorted_word_list[:VOCAB_SIZE]

with codecs.open(VOCAB_OUTPUT,'w','utf-8') as file_output:
    for word in sorted_word_list:
        file_output.write(word + '\n')

2、将中英文文件根据词汇表转换为对应number

# -*- coding:utf-8 -*-
'''
@Author:zhangy
@Modify:2019.7.5
'''

import codecs

DATA_TYPE = "english"

if DATA_TYPE == 'chinese':
    RAW_DATA = 'train.txt.zh'
    VOCAB = 'zh.vocab'
    OUTPUT_DATA = 'train.zh'
elif DATA_TYPE == 'english':
    RAW_DATA = 'train.txt.en'
    VOCAB = 'en.vocab'
    OUTPUT_DATA = 'train.en'

with codecs.open(VOCAB,'r','utf-8') as f_vocab:
    #把所有单词转换为列表形式
    vocab = [w.strip() for w in f_vocab.readlines()]
word_to_id = {k:v for (k,v) in zip(vocab,range(len(vocab)))}

def get_id(word):
    return word_to_id[word] if word in word_to_id else word_to_id['<unk>']

fin = codecs.open(RAW_DATA,'r','utf-8')
fout = codecs.open(OUTPUT_DATA,'w','utf-8')

for line in fin:
    words = line.strip().split() + ['<eos>']
    out_line = " ".join([str(get_id(w)) for w in words]) + '\n'
    fout.write(out_line)

fin.close()
fout.close()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值