古诗词自动生成
Last Update:
Page View: loading...
文学理论课上讲过的诗词自动生成对现代文学的影响,是个有意思的主题。
算法的大致思路大概是这样:
1.随机生成若干个满足约束条件(格律,押韵等)的填词方案。
2.选取其中较为优秀的一些结果作为父代,然后利用遗传算法中的交叉和变异操作,从父代生成子代,即,在已有的填词方案的基础上生成新的填词方案。
3.不断进行评判与迭代,直到跳出循环,生成一首完整的作品。
实现步骤大概是:
准备数据:收集和准备古诗词的词汇和句型数据。
随机生成填词方案:随机生成满足格律和押韵要求的初始诗词方案。
评判优秀方案:定义一个评估函数来评判诗词的优劣。
遗传算法操作:实现交叉和变异操作来生成新的诗词方案。
关键词生成:根据输入的关键词生成相关的诗词。
我使用GPT-4模型对相关算法进行了基于Python语言的程序生成。大概有几个注意的点:
1.加载和保存诗句结构:我们添加了 load_structures 和 save_structures 方法,用于加载和保存诗句结构到 JSON 文件。
2.添加新诗句结构:add_poem_structure 方法允许用户将新诗句结构添加到库中。
3.示例用法:展示了如何添加一个新的诗句结构,并生成包含关键词的古诗词。
import random
import json
import os
class PoemGenerator:
def init(self, keywords):
self.keywords = keywords
self.words = self.load_words()
self.rhymes = self.load_rhymes()
self.structures = self.load_structures()
def load_words(self):
# 加载词汇表 (示例词汇表)
return ['春', '江', '花', '月', '夜', '山', '水', '风', '雨', '心', '梦', '愁', '酒', '情', '思']
def load_rhymes(self):
# 加载押韵表 (示例押韵表)
return {'a': ['花', '家', '沙', '霞', '茶'],
'i': ['水', '美', '飞', '泪', '灰'],
'an': ['山', '安', '寒', '欢', '滩'],
'en': ['人', '春', '云', '门', '魂']}
def load_structures(self):
# 加载诗句结构 (如果存在用户自定义结构则加载)
if os.path.exists("structures.json"):
with open("structures.json", "r", encoding="utf-8") as file:
return json.load(file)
# 示例结构
return [
"春江花月夜",
"山水风雨情",
"风花雪月夜",
"青山绿水情",
"心随流水去",
"梦断花影间"
]
def save_structures(self, structures):
# 保存诗句结构到文件
with open("structures.json", "w", encoding="utf-8") as file:
json.dump(structures, file, ensure_ascii=False, indent=4)
def add_poem_structure(self, structure):
# 添加新的诗句结构到库中
structures = self.load_structures()
if structure not in structures:
structures.append(structure)
self.save_structures(structures)
print(f"成功添加新诗句结构: {structure}")
else:
print(f"诗句结构已存在: {structure}")
def generate_initial_population(self, size):
population = []
for _ in range(size):
poem = self.generate_random_poem()
population.append(poem)
return population
def generate_random_poem(self):
structure = random.choice(self.structures)
poem = ""
for char in structure:
if char == ' ':
poem += ' '
else:
poem += random.choice(self.words)
return poem
def evaluate(self, poem):
# 简单评估函数:根据关键词出现次数和押韵评分
score = 0
for keyword in self.keywords:
score += poem.count(keyword)
last_word = poem[-1]
for rhyme in self.rhymes.values():
if last_word in rhyme:
score += 1
break
return score
def crossover(self, parent1, parent2):
# 简单交叉操作:交换中间部分
cut = len(parent1) // 2
child1 = parent1[:cut] + parent2[cut:]
child2 = parent2[:cut] + parent1[cut:]
return child1, child2
def mutate(self, poem):
# 简单变异操作:随机替换一个词
index = random.randint(0, len(poem) - 1)
poem = list(poem)
poem[index] = random.choice(self.words)
return ''.join(poem)
def generate_poem(self, generations=100, population_size=10):
population = self.generate_initial_population(population_size)
for _ in range(generations):
population = sorted(population, key=self.evaluate, reverse=True)
next_generation = population[:2] # 保留最优秀的两个
while len(next_generation) < population_size:
parent1, parent2 = random.sample(population[:5], 2)
child1, child2 = self.crossover(parent1, parent2)
if random.random() < 0.1: # 10% 变异率
child1 = self.mutate(child1)
if random.random() < 0.1:
child2 = self.mutate(child2)
next_generation.extend([child1, child2])
population = next_generation
best_poem = max(population, key=self.evaluate)
return best_poem
使用示例
keywords = [‘春’, ‘花’]
generator = PoemGenerator(keywords)
添加新诗句结构
new_structure = “春风又绿江南岸”
generator.add_poem_structure(new_structure)
生成古诗词
poem = generator.generate_poem()
print(f”生成的古诗词: {poem}”)
运行代码时,可以添加新的诗句结构,然后生成包含特定关键词的诗词。
通过这种方式,可以不断扩展诗句库,生成更丰富多样的古诗词。
问题是:此程序基本只能定义唯一的关键词,而不能实现任意关键词输入后,都能输出相应的诗词的结果。
算法的优化还有待进一步探讨。