动手学深度学习 Task2学习笔记-文本预处理等

文本预处理;语言模型;循环神经网络
2.11-2.14
一、文本预处理
(1)读入文本

import collections
import re
#逐行读取文本
def read_time_machine():
    with open('/home/kesci/input/timemachine7163/timemachine.txt', 'r') as f:
        lines = [re.sub('[^a-z]+', ' ', line.strip().lower()) for line in f]
    return lines


lines = read_time_machine()
print('# sentences %d' % len(lines))

(2)分词

def tokenize(sentences, token='word'):
    """Split sentences into word or char tokens"""
    if token == 'word': #按单次分词
        return [sentence.split(' ') for sentence in sentences]
    elif token == 'char': #按字符分词
        return [list(sentence) for sentence in sentences]
    else:
        print('ERROR: unkown token type '+token)

tokens = tokenize(lines)
tokens[0:2]

(3)建立字典

class Vocab(object)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值