昨天晚上,见到室友发了一条非常文艺的诗句说说,我就想,我这文采是不行了,但是机器应该可以。我今天就做了个非常简单的诗歌编辑脚本。过于简陋,仅供参考。
前言
中国古诗词,是中华文化的特色,也是高级语言的一种表达。今天我就壮着胆子表现一下。
提示:以下是本篇文章正文内容,下面案例可供参考
一、制作古诗或者藏头诗?
我先说一下我的思路:
1.从网络上爬取古诗词
2.对古诗词进行处理,只保留5言或7言的句子
3.通过随机组合成诗句
二、使用步骤
1.引入库
我们所用到的库是经常使用的:
import requests
from lxml import etree
import time
import random
2.爬取古诗词网
之所以爬取两个url是因为,处理后可用的诗词句太少了,所以多方面获取,这个诗词句文件内容越多越好(这样爬取会有干扰需自己手动处理):
def get_shici():
"""
获取诗词网的数据
"""
for i in range(1, 21):
# 获取url
url = "https://so.gushiwen.cn/shiwen/default_0AA{}.aspx".format(i)
# 获取网页数据
html = requests.get(url).text
# text格式转为HTML格式
html = etree.HTML(html)
# 通过xpath定位需要的信息 (返回的是一个列表)
shi_poem = html.xpath(
'//div[@class="main3"]/div[@class="left"]/div[@class="sons"]/div[@class="cont"]/div[@class="contson"]/text()')
# 遍历数据
for url_data in shi_poem:
# 转为字符串并去除空格
strs = "".join(url_data)
# 判断是否有数据
if strs.isascii() == 0:
# 保存数据
with open("gushi.txt", "a", encoding="utf-8") as f:
f.write(strs.strip() + "\n")
print("正在下载第{}页的古诗".format(i))
# 延时2秒
time.sleep(2)
for i in range(1, 6):
# 获取url
url = "https://so.gushiwen.cn/mingjus/default.aspx?page={}".format(i)
# 获取网页数据
html = requests.get(url).text
# text格式转为HTML格式
html = etree.HTML(html)
# 通过xpath定位需要的信息 (返回的是一个列表)
shi_poem = html.xpath(
'//*[@id="html"]/body/div[2]/div[1]/div[2]/div/a[1]/text()')
# 遍历数据
for url_data in shi_poem:
# 转为字符串并去除空格
strs = "".join(url_data)
# 判断是否有数据
if strs.isascii() == 0:
# 保存数据
with open("gushi.txt", "a", encoding="utf-8") as f:
f.write(strs.strip() + "\n")
print("正在下载第{}页的古诗".format(i))
# 延时2秒
time.sleep(2)
print("下载完成!!!")
3.读取文件并处理文件
知识涉及太少,也没有很好的处理方法,就只有最原始的硬解
# 读取文件
with open("gushi.txt", "r", encoding="utf-8") as f:
poems = f.read()
# 对文件处理 暂时没有想到更好的方法
# 把所有的 。 ? ! 转换为 ,号
poems = poems.replace("。", ",")
poems = poems.replace("?", ",")
poems = poems.replace("!", ",")
# 以 , 号分割
lines = poems.split(",")
# 去除长度不是5个字或7个字的字符串
lines = [i.strip() for i in lines if len(i.strip()) == 5 or len(i.strip()) == 7]
# 先转为集合去重 再换回来
lines = list(set(lines))
4.诗句创作
还是知识涉及太少,怎么也没有想到诗句的对仗方法,所以这程序是没有押韵效果的,个人觉得有点鸡肋。
def get_wuyanshi():
"""
5言诗句创建
"""
# 定义一个列表用来存储诗句
lie = []
# 随机产生4句或者8句的诗句
cheng = 4 * random.randint(1, 2)
# 循环添加
while len(lie) < cheng:
# 随机选取一条诗句
juzi = lines[random.randint(0, len(lines) - 1)]
# 判断长度是否为5个字
if len(juzi) == 5:
# 满足就添加
lie.append(juzi)
# 遍历数据结果
for i in lie:
print(i)
def get_qiyanshi():
"""
5言诗句创建
"""
# 定义一个列表用来存储诗句
lie = []
# 随机产生4句或者8句的诗句
cheng = 4 * random.randint(1, 2)
# 循环添加
while len(lie) < cheng:
# 随机选取一条诗句
juzi = lines[random.randint(0, len(lines) - 1)]
# 判断长度是否为7个字
if len(juzi) == 7:
# 满足就添加
lie.append(juzi)
# 遍历数据结果
for i in lie:
print(i)
def get_cangtoushi(strs):
"""
依据藏头词进行造诗
:param strs: 藏头词
"""
# 随机产生5言或者7言诗
chang = random.randint(2, 3) * 2 + 1
# 循环遍历藏头词
for i in strs:
# 用来存储创造出来的诗句
lie = []
# 遍历匹配符合要求的诗句
for j in lines:
# 诗句开头符合对应的藏头词 并且统一格式5言或7言
if j[0] == i and len(j) == chang:
lie.append(j)
# 判断是否有符合要求的诗句
if len(lie) > 0:
# 多个符合要求的随机选泽一个
print(lie[random.randint(0, len(lie) - 1)])
else:
print("没有合适的句子匹配。")
def get_cangweishi(strs):
"""
依据藏尾词进行造诗
:param strs: 藏尾词
"""
# 随机产生5言或者7言诗
chang = random.randint(2, 3) * 2 + 1
# 循环遍历藏头词
for i in strs:
# 用来存储创造出来的诗句
lie = []
# 遍历匹配符合要求的诗句
for j in lines:
# 诗句结尾符合对应的藏尾词 并且统一格式5言或7言
if j[-1] == i and len(j) == chang:
lie.append(j)
# 判断是否有符合要求的诗句
if len(lie) > 0:
# 多个符合要求的随机选泽一个
print(lie[random.randint(0, len(lie) - 1)])
else:
print("没有合适的句子匹配。")
5.完整代码
不多说啥了,咱的这技术过于丢人,也只能厚着脸皮发出来了。见谅见谅。
import requests
from lxml import etree
import time
import random
def get_shici():
"""
获取诗词网的数据
"""
for i in range(1, 21):
# 获取url
url = "https://so.gushiwen.cn/shiwen/default_0AA{}.aspx".format(i)
# 获取网页数据
html = requests.get(url).text
# text格式转为HTML格式
html = etree.HTML(html)
# 通过xpath定位需要的信息 (返回的是一个列表)
shi_poem = html.xpath(
'//div[@class="main3"]/div[@class="left"]/div[@class="sons"]/div[@class="cont"]/div[@class="contson"]/text()')
# 遍历数据
for url_data in shi_poem:
# 转为字符串并去除空格
strs = "".join(url_data)
# 判断是否有数据
if strs.isascii() == 0:
# 保存数据
with open("gushi.txt", "a", encoding="utf-8") as f:
f.write(strs.strip() + "\n")
print("正在下载第{}页的古诗".format(i))
# 延时2秒
time.sleep(2)
for i in range(1, 6):
# 获取url
url = "https://so.gushiwen.cn/mingjus/default.aspx?page={}".format(i)
# 获取网页数据
html = requests.get(url).text
# text格式转为HTML格式
html = etree.HTML(html)
# 通过xpath定位需要的信息 (返回的是一个列表)
shi_poem = html.xpath(
'//*[@id="html"]/body/div[2]/div[1]/div[2]/div/a[1]/text()')
# 遍历数据
for url_data in shi_poem:
# 转为字符串并去除空格
strs = "".join(url_data)
# 判断是否有数据
if strs.isascii() == 0:
# 保存数据
with open("gushi.txt", "a", encoding="utf-8") as f:
f.write(strs.strip() + "\n")
print("正在下载第{}页的古诗".format(i))
# 延时2秒
time.sleep(2)
print("下载完成!!!")
# 读取文件
with open("gushi.txt", "r", encoding="utf-8") as f:
poems = f.read()
# 对文件处理 暂时没有想到更好的方法
# 把所有的 。 ? ! 转换为 ,号
poems = poems.replace("。", ",")
poems = poems.replace("?", ",")
poems = poems.replace("!", ",")
# 以 , 号分割
lines = poems.split(",")
# 去除长度不是5个字或7个字的字符串
lines = [i.strip() for i in lines if len(i.strip()) == 5 or len(i.strip()) == 7]
# 先转为集合去重 再换回来
lines = list(set(lines))
def get_wuyanshi():
"""
5言诗句创建
"""
# 定义一个列表用来存储诗句
lie = []
# 随机产生4句或者8句的诗句
cheng = 4 * random.randint(1, 2)
# 循环添加
while len(lie) < cheng:
# 随机选取一条诗句
juzi = lines[random.randint(0, len(lines) - 1)]
# 判断长度是否为5个字
if len(juzi) == 5:
# 满足就添加
lie.append(juzi)
# 遍历数据结果
for i in lie:
print(i)
def get_qiyanshi():
"""
5言诗句创建
"""
# 定义一个列表用来存储诗句
lie = []
# 随机产生4句或者8句的诗句
cheng = 4 * random.randint(1, 2)
# 循环添加
while len(lie) < cheng:
# 随机选取一条诗句
juzi = lines[random.randint(0, len(lines) - 1)]
# 判断长度是否为7个字
if len(juzi) == 7:
# 满足就添加
lie.append(juzi)
# 遍历数据结果
for i in lie:
print(i)
def get_cangtoushi(strs):
"""
依据藏头词进行造诗
:param strs: 藏头词
"""
# 随机产生5言或者7言诗
chang = random.randint(2, 3) * 2 + 1
# 循环遍历藏头词
for i in strs:
# 用来存储创造出来的诗句
lie = []
# 遍历匹配符合要求的诗句
for j in lines:
# 诗句开头符合对应的藏头词 并且统一格式5言或7言
if j[0] == i and len(j) == chang:
lie.append(j)
# 判断是否有符合要求的诗句
if len(lie) > 0:
# 多个符合要求的随机选泽一个
print(lie[random.randint(0, len(lie) - 1)])
else:
print("没有合适的句子匹配。")
def get_cangweishi(strs):
"""
依据藏尾词进行造诗
:param strs: 藏尾词
"""
# 随机产生5言或者7言诗
chang = random.randint(2, 3) * 2 + 1
# 循环遍历藏头词
for i in strs:
# 用来存储创造出来的诗句
lie = []
# 遍历匹配符合要求的诗句
for j in lines:
# 诗句结尾符合对应的藏尾词 并且统一格式5言或7言
if j[-1] == i and len(j) == chang:
lie.append(j)
# 判断是否有符合要求的诗句
if len(lie) > 0:
# 多个符合要求的随机选泽一个
print(lie[random.randint(0, len(lie) - 1)])
else:
print("没有合适的句子匹配。")
while True:
print("本软件可以实现:1、自动生成5言诗句 2、自动生成7言诗句 3、藏头诗 4、藏尾诗 5、退出")
number = input("请输入你要实现的功能(输入数字):")
if number not in ["1", "2", "3", "4", "5"]:
print("您的输入不符合要求!!!")
elif number == "1":
get_wuyanshi()
elif number == "2":
get_qiyanshi()
elif number == "3":
uesr_lien = input("请输入您的藏头词:")
get_cangtoushi(uesr_lien)
elif number == "4":
uesr_lien = input("请输入您的藏尾词:")
get_cangweishi(uesr_lien)
else:
break
欢迎大家指点错误,或者优化。
总结
不断地学习,不断地变强