概念生成的生成语法。
prestoplot的Python项目详细描述
砰!即时情节!
prestoplot是一个用于生成想法、名称和其他愚蠢行为的工具 否则你应该写作的时候。
最好用PrestoPlot Oracles repository中的神谕。
安装
PYPI提供了Prestoplot:
pip install prestoplot
用法
可以使用prestocli脚本调用prestoplot:
presto --help
直接咨询的“oracle”必须包含Begin:节:
$ cat names.yaml Begin: - "{Name}" Name: - George - Martha $ presto run names.yaml George
生成语法
现在的主要特性是一个生成语法,它使用一个简单的基于yaml的 语言和Python f-string syntax创建“oracles”以生成创意。
学习语法的最好方法是看例子。我们会考虑 YAML for generating a Pirate story,开头如下:
include: - setup Begin: - "{PiratesOracle}"
我们需要Begin:节来直接咨询Oracle。 它包含随机生成器可以从中选择的字符串列表。 在本例中,我们有一个f-string模板,它调用PiratesOracle。我们 请在下面查找:
PiratesOracle: - | {Setup} - {Letters.One} - {Letters.Two} - {Letters.Three} - {Letters.Four} - | {Setup} - {CutlassDagger.One} - {CutlassDagger.Two} - {CutlassDagger.Three} - {CutlassDagger.Four}
我们看到另一个字符串列表。|后跟缩进新行表示 将缩进级别后面的内容视为文本字符串,而不是 山药:
{Setup} - {Letters.One} - {Letters.Two} - {Letters.Three} - {Letters.Four}
所以这是一个带有标记样式列表的字符串,而不是yaml列表,all 因为|。
所以这里我们看到Setup被调用,然后Letters被调用了四次。 Letters定义如下:
Letters: - mode: pick - "Betrayal and treachery!" - "Captured {Nationality} charts, carefully copied, and used by the Royal Navy." - "Dolphins, seen frolicking in the bow-wake of a ship, perhaps leading it toward its goal." - "Flotsam and jetsam, washed ashore after a sea-battle." - "Fo’c’sle gossip blaming the ship’s misfortunes on a crewman who killed an albatross." - "Forged documents, implying that their bearer speaks for the Crown." - "Hidden reefs, which at low tide endanger any ship that passes over them."
我们还有一个清单,包含盗版主题元素。mode: pick 告诉生成器从其中随机选取,然后删除该选项 从对未来选择的考虑。正常模式是reuse,它允许 列出要由生成器重新使用的项。另一个模式markov告诉 生成器从列表中生成马尔可夫链,如these name lists。
回到PiratesOracle,我们看到Letters被调用了四次, 每次使用新的key。键的值仅对 读者。工作时,每个新密钥都充当随机生成器的新种子 在那一节里。例如,如果{Letters.One}选择了元素 "Captured {Nationality} charts, carefully copied, and used by the Royal Navy.",值One提供用于拾取Nationality的种子, 说,English。稍后,如果{Letters.Two}遇到另一个元素 包含{Nationality}的键Two将为 第二次选择国籍。
当我们检查include节时,情节会变厚,其中包括 setup.yaml文件next door。此文件包含更多文件。接下来我们将检查characters.yaml。
在characters.yaml里面,我们发现了这组迷人的诗节:
Sex: - male - female He: - > {'She' if Sex[key] == 'female' else 'He'} his: - > {'her' if Sex[key] == 'female' else 'his'} His: - > {'Her' if Sex[key] == 'female' else 'His'} hero: - "{'heroine' if Sex[key] == 'female' else 'hero'}"
使用这组工具,我们可以编写以下字符串:
That {hero.protag}! {He.protag} sure loves {his.protag} mom.
它的长短取决于主人公的性别 将呈现其中之一:
That heroine! She sure loves her mom.
或:
That hero! He sure loves his mom.
在这里我们可以看到,在f-string语法中,我们可以使用pythonic表达式, 变量key包含来自外部作用域的键:{He.protag} 将值"protag"分配给key。{Sex[key]}将可靠地生成 相同键的相同结果(假设初始种子相同)。
其他的都是yaml语法和python f-string表达式。
关于
我写了prestoplot来支持我的想法生成和名字生成 受纸浆启发的科幻太空歌剧系列,Salvage of Empire:
When his brother-in-law threatens to reveal his terrible secret, Director Kolteo Ais must sacrifice everything he has worked for to save the Galactic Empire—and his marriage—from utter ruin.