资源简介

用RNN做了一个自动写诗的程序,包含已经训练好的model,可以直接运行,训练数据也包含在里面

资源截图

代码片段和文件信息

from six.moves.urllib.request import urlopen
from six.moves.urllib.error import HTTPError
from six.moves.queue import Queue
import re
from threading import Thread
import sys

def worker(taskpoems):
    while True:
        js = task.get()
        fetch_poems(jspoems)
        task.task_done()

def write_poems(pathpoems):
    with open(path‘w‘encoding=“utf-8“) as f:
        n = 0
        while True:
            poem = poems.get()
            if not poem:
                break
            f.write(poem)
            f.write(‘\n‘)
            poems.task_done()
            n += 1
            sys.stdout.write(‘\r‘)
            sys.stdout.write(‘{} poems have been writen to the file‘.format(n))
            sys.stdout.flush()

def fetch_poems(jspoems):
    ns = 1
    while True:
        poem = get_poem(jsns)
        if not poem:
            break
        poems.put(poem)
        ns += 1

def get_poem(js=1ns=1):
    try:
        raw_data = urlopen(‘http://www16.zzu.edu.cn/qtss/zzjpoem1.dll/viewoneshi?js={:03d}&ns={:03d}‘.format(jsns)).read().decode(‘gb2312‘‘ignore‘)
    except HTTPError as error:
        print(‘HTTP Error {}:{}‘.format(error.codeerror.reason))
        return None
    match = re.search(r‘color=“#FFFFBF“>(.*?)‘raw_datare.M)
    if match:
        return match.group(1).replace(‘ ‘‘‘).replace(‘
‘‘‘)
    return None

def main():
    volumes = 900
    num_worker_threads = 25
    task = Queue()
    poems = Queue()
    for i in range(num_worker_threads):
        t = Thread(target=workerargs=(taskpoems))
        t.daemon = True
        t.start()
    write_thread = Thread(target=write_poemsargs=(‘./data/poems.txt‘poems))
    write_thread.start()
    for js in range(1volumes+1):
        task.put(js)
    task.join()
    poems.join()
    poems.put(None)
    write_thread.join()

if __name__ == “__main__“:
    main()

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     目录           0  2018-05-30 11:18  AncientChinesePoemRNN-鍐欒瘲\
     文件          50  2017-03-22 22:46  AncientChinesePoemRNN-鍐欒瘲\.gitignore
     目录           0  2018-07-03 15:40  __MACOSX\
     目录           0  2018-07-03 15:40  __MACOSX\AncientChinesePoemRNN-鍐欒瘲\
     文件         212  2017-03-22 22:46  __MACOSX\AncientChinesePoemRNN-鍐欒瘲\._.gitignore
     目录           0  2018-05-30 11:39  AncientChinesePoemRNN-鍐欒瘲\.idea\
     文件         533  2018-05-29 15:15  AncientChinesePoemRNN-鍐欒瘲\.idea\AncientChinesePoemRNN-master.iml
     目录           0  2018-05-29 15:15  AncientChinesePoemRNN-鍐欒瘲\.idea\inspectionProfiles\
     文件         226  2018-05-29 15:15  AncientChinesePoemRNN-鍐欒瘲\.idea\misc.xml
     文件         308  2018-05-29 15:14  AncientChinesePoemRNN-鍐欒瘲\.idea\modules.xml
     文件       26023  2018-05-30 11:39  AncientChinesePoemRNN-鍐欒瘲\.idea\workspace.xml
     文件        1872  2018-05-30 11:18  AncientChinesePoemRNN-鍐欒瘲\crawl_data.py
     目录           0  2018-05-30 11:19  AncientChinesePoemRNN-鍐欒瘲\data\
     文件    14141437  2018-05-30 11:19  AncientChinesePoemRNN-鍐欒瘲\data\data.npy
     文件     9143472  2017-03-22 22:46  AncientChinesePoemRNN-鍐欒瘲\data\poems.txt
     目录           0  2018-07-03 15:40  __MACOSX\AncientChinesePoemRNN-鍐欒瘲\data\
     文件         212  2017-03-22 22:46  __MACOSX\AncientChinesePoemRNN-鍐欒瘲\data\._poems.txt
     文件      423714  2018-05-29 15:33  AncientChinesePoemRNN-鍐欒瘲\data\shijing.txt
     文件       23753  2018-05-30 11:19  AncientChinesePoemRNN-鍐欒瘲\data\vocab.pkl
     文件         212  2018-05-30 11:19  __MACOSX\AncientChinesePoemRNN-鍐欒瘲\._data
     文件        4316  2017-03-22 22:46  AncientChinesePoemRNN-鍐欒瘲\model.py
     文件         212  2017-03-22 22:46  __MACOSX\AncientChinesePoemRNN-鍐欒瘲\._model.py
     文件        4310  2018-05-29 15:17  AncientChinesePoemRNN-鍐欒瘲\model.pyc
     文件         878  2017-03-22 22:46  AncientChinesePoemRNN-鍐欒瘲\README.md
     文件         212  2017-03-22 22:46  __MACOSX\AncientChinesePoemRNN-鍐欒瘲\._README.md
     文件        1488  2018-05-29 16:01  AncientChinesePoemRNN-鍐欒瘲\sample.py
     目录           0  2018-05-30 11:36  AncientChinesePoemRNN-鍐欒瘲\save\
     文件          14  2017-03-22 22:46  AncientChinesePoemRNN-鍐欒瘲\save\.gitignore
     目录           0  2018-07-03 15:40  __MACOSX\AncientChinesePoemRNN-鍐欒瘲\save\
     文件         212  2017-03-22 22:46  __MACOSX\AncientChinesePoemRNN-鍐欒瘲\save\._.gitignore
     文件       67397  2018-05-30 11:19  AncientChinesePoemRNN-鍐欒瘲\save\chars_vocab.pkl
............此处省略29个文件信息

评论

共有 条评论