当前位置:   article > 正文

LSTM做文本生成(基于word2vec)_keras 小说生成

keras 小说生成

数据:使用丘吉尔的人物传记作为我的学习语料

框架:Keras

  1. import os
  2. import numpy as np
  3. import nltk
  4. from keras.models import Sequential
  5. from keras.layers import Dense
  6. from keras.layers import Dropout
  7. from keras.layers import LSTM
  8. from keras.callbacks import ModelCheckpoint
  9. from keras.utils import np_utils
  10. from gensim.models.word2vec import Word2Vec
  11. #读入文本
  12. raw_text=""
  13. for file in os.listdir("../input/"):
  14. if file.endswith(".txt"):
  15. raw_text+=open("../input/"+file,errors="ignore").read()+\"n\n"
  16. #row_test=open("../input/Winston_Churchil.txt").read()
  17. raw_text=raw_text.lower()
  18. sentensor=nltk.data.load("tokenizers/punkt/english.pickle")
  19. sents=sentensor.tokenize(raw_text)
  20. corpus=[]
  21. for sen in sents:
  22. corpus.append(nltk.word_tokenize(sen))
  23. print(len(corpus))
  24. print(corpus[:3])
  25. #结果
  26. 91007
  27. [['\ufeffthe', 'project', 'gutenberg', 'ebook', 'of', 'great', 'expectations', ',', 'by', 'charles', 'dickens', 'this', 'ebook', 'is', 'for', 'the', 'use', 'of', 'anyone', 'anywhere', 'at', 'no', 'cost', 'and', 'with', 'almost', 'no', 'restrictions', 'whatsoever', '.'], ['you', 'may', 'copy', 'it', ',', 'give', 'it', 'away', 'or', 're-use', 'it', 'under', 'the', 'terms', 'of', 'the', 'project', 'gutenberg', 'license', 'included', 'with', 'this', 'ebook', 'or', 'online', 'at', 'www.gutenberg.org', 'title', ':', 'great', 'expectations', 'author', ':', 'charles', 'dickens', 'posting', 'date', ':', 'august', '20', ',', '2008', '[', 'ebook', '#'
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/一键难忘520/article/detail/836461
推荐阅读
相关标签
  

闽ICP备14008679号