lstm 根据前文预测词_NLP(二十三)使用LSTM进行语言建模以预测最优词

N元模型

预测要输入的连续词,比如

5219474bb4df97c175b218c26078c538.png

如果抽取两个连续的词汇,则称之为二元模型

准备工作

数据集使用 Alice in Wonderland

将初始数据提取N-grams

import nltk

import string

with open('alice_in_wonderland.txt', 'r') as content_file:

content = content_file.read()

content2 = " ".join("".join([" " if ch in string.punctuation else ch for ch in content]).split())

tokens = nltk.word_tokenize(content2)

tokens = [word.lower() for word in tokens if len(word)>=2]

N = 3

quads = list(nltk.ngrams(tokens,N))

"""

Return the ngrams generated from a sequence of items, as an iterator.

For example:

>>> from nltk.util import ngrams

>>> list(ngrams([1,2,3,4,5], 3))

[(1, 2, 3), (2, 3, 4), (3, 4, 5)]

"""

newl_app = []

for ln in quads:

new1 = ' '.join(ln)

newl_app.append(new1)

print(newl_app[:3])

输出:

['alice adventures in', 'adventures in wonderland', 'in wonderland alice']

如何实现

1.预处理:词转换为词向量

2.创建模型和验证:将输入映射到输出的收敛-发散模型(convergent-divergent)

3.预测:最优词预测

代码

from __future__ import print_function

from sklearn.model_selection import train_test_split

import nltk

import numpy as np

import string</

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值