tensorflow2.0 预测新文本(一)

先记录一个报错 unindent does not match any outer indentation level,这里可能是缩进有的使用的空格,有的使用的是tab不一致。(改了半天,人🐎了)

1.导入库

import tensorflow as tf

from tensorflow.keras.preprocessing.sequence import pad_sequences
from tensorflow.keras.layers import Embedding, LSTM, Dense, Bidirectional
from tensorflow.keras.preprocessing.text import Tokenizer
from tensorflow.keras.models import Sequential
from tensorflow.keras.optimizers import Adam
import numpy as np 

2.数据准备

这里的data是一篇文章,用\n分割每一句话。

tokenizer = Tokenizer()

data="In the town of Athy one Jeremy Lanigan \n Battered away til he hadnt a pound. \nHis father died and made him a man again \n Left him a farm and ten acres of ground. \nHe gave a grand party for friends and relations \nWho didnt forget him when come to the wall, \nAnd if youll but listen Ill make your eyes glisten \nOf the rows and the ructions of Lanigans Ball. \nMyself to be sure got free invitation, \nFor all the nice girls and boys I might ask, \nAnd just in a minute both friends and relations \nWere dancing round merry as bees round a cask. \nJudy ODaly, that nice little milliner, \nShe tipped me a wink for to give her a call, \nAnd I soon arrived with Peggy McGilligan \nJust in time for Lanigans Ball. \nThere were lashings of punch and wine for the ladies, \nPotatoes and cakes; there was bacon and tea, \nThere were the Nolans, Dolans, OGradys \nCourting the girls and dancing away. \nSongs they went round as plenty as water, \nThe harp that once sounded in Taras old hall,\nSweet Nelly Gray and The Rat Catchers Daughter,\nAll singing together at Lanigans Ball. \nThey were doing all kinds of nonsensical polkas \nAll round the room in a whirligig. \nJulia and I, we banished their nonsense \nAnd tipped them the twist of a reel and a jig. \nAch mavrone, how the girls got all mad at me \nDanced til youd think the ceiling would fall. \nFor I spent three weeks at Brooks Academy \nLearning new steps for Lanigans Ball. \nThree long weeks I spent up in Dublin, \nThree long weeks to learn nothing at all,\n Three long weeks I spent up in Dublin, \nLearning new steps for Lanigans Ball. \nShe stepped out and I stepped in again, \nI stepped out and she stepped in again, \nShe stepped out and I stepped in again, \nLearning new steps for Lanigans Ball. \nBoys were all merry and the girls they were hearty \nAnd danced all around in couples and groups, \nTil an accident happened, young Terrance McCarthy \nPut his right leg through miss Finnertys hoops. \nPoor creature fainted and cried Meelia murther, \nCalled for her brothers and gathered them all. \nCarmody swore that hed go no further \nTil he had satisfaction at Lanigans Ball. \nIn the midst of the row miss Kerrigan fainted, \nHer cheeks at the same time as red as a rose. \nSome of the lads declared she was painted, \nShe took a small drop too much, I suppose. \nHer sweetheart, Ned Morgan, so powerful and able, \nWhen he saw his fair colleen stretched out by the wall, \nTore the left leg from under the table \nAnd smashed all the Chaneys at Lanigans Ball. \nBoys, oh boys, twas then there were runctions. \nMyself got a lick from big Phelim McHugh. \nI soon replied to his introduction \nAnd kicked up a terrible hullabaloo. \nOld Casey, the piper, was near being strangled. \nThey squeezed up his pipes, bellows, chanters and all. \nThe girls, in their ribbons, they got all entangled \nAnd that put an end to Lanigans Ball."
#将所有数据转为小写,并且用\n分割
corpus = data.lower().split("\n")

tokenizer.fit_on_texts(corpus)
#+1是考虑陌生词汇
total_words = len(tokenizer.word_index)+1
#打印单词-索引对
print(tokenizer.word_index)
print(total_words)
print(corpus)

{‘and’: 1, ‘the’: 2, ‘a’: 3, ‘in’: 4, ‘all’: 5, ‘i’: 6, ‘for’: 7, ‘of’: 8, ‘lanigans’: 9, ‘ball’: 10, ‘were’: 11, ‘at’: 12, ‘to’: 13, ‘she’: 14, ‘stepped’: 15, ‘his’: 16, ‘girls’: 17, ‘as’: 18, ‘they’: 19, ‘til’: 20, ‘he’: 21, ‘again’: 22, ‘got’: 23, ‘boys’: 24, ‘round’: 25, ‘that’: 26, ‘her’: 27, ‘there’: 28, ‘three’: 29, ‘weeks’: 30, ‘up’: 31, ‘out’: 32, ‘him’: 33, ‘was’: 34, ‘spent’: 35, ‘learning’: 36, ‘new’: 37, ‘steps’: 38, ‘long’: 39, ‘away’: 40, ‘left’: 41, ‘friends’: 42, ‘relations’: 43, ‘when’: 44, ‘wall’: 45, ‘myself’: 46, ‘nice’: 47, ‘just’: 48, ‘dancing’: 49, ‘merry’: 50, ‘tipped’: 51, ‘me’: 52, ‘soon’: 53, ‘time’: 54, ‘old’: 55, ‘their’: 56, ‘them’: 57, ‘danced’: 58, ‘dublin’: 59, ‘an’: 60, ‘put’: 61, ‘leg’: 62, ‘miss’: 63, ‘fainted’: 64, ‘from’: 65, ‘town’: 66, ‘athy’: 67, ‘one’: 68, ‘jeremy’: 69, ‘lanigan’: 70, ‘battered’: 71, ‘hadnt’: 72, ‘pound’: 73, ‘father’: 74, ‘died’: 75, ‘made’: 76, ‘man’: 77, ‘farm’: 78, ‘ten’: 79, ‘acres’: 80, ‘ground’: 81, ‘gave’: 82, ‘grand’: 83, ‘party’: 84, ‘who’: 85, ‘didnt’: 86, ‘forget’: 87, ‘come’: 88, ‘if’: 89, ‘youll’: 90, ‘but’: 91, ‘listen’: 92, ‘ill’: 93, ‘make’: 94, ‘your’: 95, ‘eyes’: 96, ‘glisten’: 97, ‘rows’: 98, ‘ructions’: 99, ‘be’: 100, ‘sure’: 101, ‘free’: 102, ‘invitation’: 103, ‘might’: 104, ‘ask’: 105, ‘minute’: 106, ‘both’: 107, ‘bees’: 108, ‘cask’: 109, ‘judy’: 110, ‘odaly’: 111, ‘little’: 112, ‘milliner’: 113, ‘wink’: 114, ‘give’: 115, ‘call’: 116, ‘arrived’: 117, ‘with’: 118, ‘peggy’: 119, ‘mcgilligan’: 120, ‘lashings’: 121, ‘punch’: 122, ‘wine’: 123, ‘ladies’: 124, ‘potatoes’: 125, ‘cakes’: 126, ‘bacon’: 127, ‘tea’: 128, ‘nolans’: 129, ‘dolans’: 130, ‘ogradys’: 131, ‘courting’: 132, ‘songs’: 133, ‘went’: 134, ‘plenty’: 135, ‘water’: 136, ‘harp’: 137, ‘once’: 138, ‘sounded’: 139, ‘taras’: 140, ‘hall’: 141, ‘sweet’: 142, ‘nelly’: 143, ‘gray’: 144, ‘rat’: 145, ‘catchers’: 146, ‘daughter’: 147, ‘singing’: 148, ‘together’: 149, ‘doing’: 150, ‘kinds’: 151, ‘nonsensical’: 152, ‘polkas’: 153, ‘room’: 154, ‘whirligig’: 155, ‘julia’: 156, ‘we’: 157, ‘banished’: 158, ‘nonsense’: 159, ‘twist’: 160, ‘reel’: 161, ‘jig’: 162, ‘ach’: 163, ‘mavrone’: 164, ‘how’: 165, ‘mad’: 166, ‘youd’: 167, ‘think’: 168, ‘ceiling’: 169, ‘would’: 170, ‘fall’: 171, ‘brooks’: 172, ‘academy’: 173, ‘learn’: 174, ‘nothing’: 175, ‘hearty’: 176, ‘around’: 177, ‘couples’: 178, ‘groups’: 179, ‘accident’: 180, ‘happened’: 181, ‘young’: 182, ‘terrance’: 183, ‘mccarthy’: 184, ‘right’: 185, ‘through’: 186, ‘finnertys’: 187, ‘hoops’: 188, ‘poor’: 189, ‘creature’: 190, ‘cried’: 191, ‘meelia’: 192, ‘murther’: 193, ‘called’: 194, ‘brothers’: 195, ‘gathered’: 196, ‘carmody’: 197, ‘swore’: 198, ‘hed’: 199, ‘go’: 200, ‘no’: 201, ‘further’: 202, ‘had’: 203, ‘satisfaction’: 204, ‘midst’: 205, ‘row’: 206, ‘kerrigan’: 207, ‘cheeks’: 208, ‘same’: 209, ‘red’: 210, ‘rose’: 211, ‘some’: 212, ‘lads’: 213, ‘declared’: 214, ‘painted’: 215, ‘took’: 216, ‘small’: 217, ‘drop’: 218, ‘too’: 219, ‘much’: 220, ‘suppose’: 221, ‘sweetheart’: 222, ‘ned’: 223, ‘morgan’: 224, ‘so’: 225, ‘powerful’: 226, ‘able’: 227, ‘saw’: 228, ‘fair’: 229, ‘colleen’: 230, ‘stretched’: 231, ‘by’: 232, ‘tore’: 233, ‘under’: 234, ‘table’: 235, ‘smashed’: 236, ‘chaneys’: 237, ‘oh’: 238, ‘twas’: 239, ‘then’: 240, ‘runctions’: 241, ‘lick’: 242, ‘big’: 243, ‘phelim’: 244, ‘mchugh’: 245, ‘replied’: 246, ‘introduction’: 247, ‘kicked’: 248, ‘terrible’: 249, ‘hullabaloo’: 250, ‘casey’: 251, ‘piper’: 252, ‘near’: 253, ‘being’: 254, ‘strangled’: 255, ‘squeezed’: 256, ‘pipes’: 257, ‘bellows’: 258, ‘chanters’: 259, ‘ribbons’: 260, ‘entangled’: 261, ‘end’: 262}
263
['in the town of athy o

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值