Python编写打油诗
利用Python的random库结合创建的不同词性的单词列表,编写一个诗歌生成器程序。
首先创建5个不同词性的单词列表。如下:
(1)名词、动词、形容词、介词、副词列表如下
noun = [
"fossil",
"horse",
"aardvark",
"judge",
"chef",
"mango",
"extrovert",
"gorilla",
]
verb = [
"kicks",
"jingles",
"bounces",
"slurps",
"meows",
"explodes",
"curdles",
]
adjective = [
"furry",
"balding",
"incredulous",
"fragrant",
"exuberant",
"glistening",
]
preposition = [
"against",
"after",
"into",
"beneath",
"upon",
"for",
"in",
"like",
"over",
"within",
]
adverb = [
"curiously",
"extravagantly",
"tantalizingly",
"furiously",
"sensuously",
]
(2)规则:
随机从每类单词中分别选出对应个数的单词:
- 3个名词
- 3个动词
- 3个形容词
- 2个介词
- 1个副词
可以借助random模块中的choice()函数,它接收一个列表为参数,随机返回一个列表中的元素。定义一个挑选词汇的函数获取词汇的函数。参数为列表,词的个数。
诗歌的格式如下:
'''
adj代表形容词(adjective),prep代表介词(preposition)
'''
{A/An} {adj1} {noun1}
{A/An} {adj1} {noun1} {verb1} {prep1} the {adj2} {noun2}
{adverb1}, the {noun1} {verb2}
the {noun2} {verb3} {prep2} a/an {adj3} {noun3}
以下是源码:
import random
noun = [
"fossil",
"horse",
"aardvark",
"judge",
"chef",
"mango",
"extrovert",
"gorilla",
]
verb = [
"kicks",
"jingles",
"bounces",
"slurps",
"meows",
"explodes",
"curdles",
]
adjective = [
"furry",
"balding",
"incredulous",
"fragrant",
"exuberant",
"glistening",
]
preposition = [
"against",
"after",
"into",
"beneath",
"upon",
"for",
"in",
"like",
"over",
"within",
]
adverb = [
"curiously",
"extravagantly",
"tantalizingly",
"furiously",
"sensuously",
]
# 定义一个随机挑选词汇的函数
def random_get(wordlst, n):
if n > 1:
random_words = []
while 1:
word = random.choice(wordlst)
if word in random_words:
continue
random_words.append(word)
if len(random_words) == n:
return random_words
else:
return random.choice(wordlst) # 单个选词是副词
# 元音字母,用以判断形容词前是an还是a
vowel = ["a", "e", "i", "o", "u"]
# 获取3个形容词
adjective_lst = random_get(adjective, 3)
# 获取3个名词
noun_lst = random_get(noun, 3)
# 获取3个动词
verb_lst = random_get(verb, 3)
# 获取2个介词
preposition_lst = random_get(preposition, 2)
# 获取1个副词
adverb = random_get(adverb, 1)
# 诗歌题目
title = f'{adjective_lst[0]} {noun_lst[0]}'
# 诗歌部分主体
poem = f'{verb_lst[0]} {preposition_lst[0]} the {adjective_lst[1]} {noun_lst[1]}\n{adverb}, the {noun_lst[0]} {verb_lst[1]}\nthe {noun_lst[1]} {verb_lst[2]} {preposition_lst[1]} '
# 判断形容词首字母是不是元音开头,以选择An或A
if adjective_lst[0][0].lower() in vowel:
title = 'An ' + title
if adjective_lst[2][0].lower() in vowel:
poem = title +'\n\n'+ title + poem + f'an {adjective_lst[2]} {noun_lst[2]}'
else:
poem = title +'\n\n'+ title + poem + f'a {adjective_lst[2]} {noun_lst[2]}'
else:
title = 'A ' + title
if adjective_lst[2][0].lower() in vowel:
poem = title + '\n\n' + title + poem + f'an {adjective_lst[2]} {noun_lst[2]}'
else:
poem = title + '\n\n' + title + poem + f'a {adjective_lst[2]} {noun_lst[2]}'
print(poem)
这个程序生成的打油诗如下:
A fragrant judge
A fragrant judgemeows beneath the exuberant chef
extravagantly, the judge jingles
the chef bounces after a balding mango
An incredulous extrovert
An incredulous extrovertmeows in the glistening aardvark
extravagantly, the extrovert slurps
the aardvark jingles for a fragrant judge
欢迎关注、点赞、转发!!!