<分区> 分区>
我正在开发一个简单的随机诗歌生成器程序。我有一些模型的词表,例如:名词、形容词、动词等。我这样实现它们:
adjective = open('lexicon/adjective.txt')
noun = open('lexicon/noun.txt')
verb = open('lexicon/verb.txt')
文本文件的格式为:noun.txt
tree
fish
computer
..
我正在使用这个函数从中随机选择一个词:
def select_word(model):
lines = model.read().splitlines()
selectedline = random.choice(lines)
return selectedline
我也有一个这种格式的 pos 标签列表(它们是我自己的语言,我只是给出英文示例以便更好地理解):
went go[simplepast]
gone go[pastperfect]
..
我正在使用此函数获取带有给定单词和邮戳的空白之前的第一个字符串
def find_postag(word,postag):
global selectedword
zemberek = open('parse.txt')
for line in zemberek:
if all(i in line for i in (word,postag)):
selectedword = line.split(" ")[0]
break
return selectedword
我的想法是:我需要创建一个巨大的诗歌模式列表,用于将随机单词放入这些模式中的合适位置。例如,假设我的模式列表中有这句话:
I was like a *noun*
程序应该调用 select_word(noun) 函数(假设它返回了 "tree")并产生如下内容:
I was like a tree
假设在一个单词后有一个 postag 定义(抱歉,我找不到合适的英文示例):
I *verb*"[Past]" a friend of mine.
现在程序应该调用 select_word(verb) 函数来选择一个随机单词,假设它现在返回 "host" 它应该调用 find_postag("host ",[Past]") 它将返回 "hosted" 输出应该是:
I hosted a friend of mine.
我的问题是:我不确定模式的格式。 * 和 " 字符仅用于表示,我不必使用它们。我正在考虑以这种格式构建模式列表:
<pattern number=1 theme=heroic>
*noun* *nnoun* gibi
*nverb*"[Verb]+[Pos]+DH[Past]+m[A1sg]" deneme *adjective*"[Adj]-[Noun]+[A3sg]+Hm[P1sg]+[Nom] :"
</pattern>
<pattern number=2 theme=love>
(some other lines)
</pattern>
我试着这样解析它:
def read_pattern(number,theme):
pattern = open('poempatterns.txt').read().splitlines()
for line in pattern:
if line == "<pattern number="+number+" theme="+theme+">":
while line != "</pattern>":
#DO PARSING
你对模式列表的格式有什么建议,我如何解析单词和单独的邮戳
