根据条件用列表中的元素格式化字符串
Formatting a string with elements in a list based on conditions
所以我创建了一系列语法,供我创建的 class 中的方法使用。每个列表可以是 n 个元素长,因此通过列表索引放置每个单词是直接的 wordlist[1:]
,但是我需要使用 |
运算符并且不能使用显式字符串索引来完成(至少我认同)。这是我到目前为止写的:
noun_types = ['port', 'harbor', 'harbour']
target_pronouns = ['rotterdam', 'moscow']
grammer1 = (
F"""
S -> Det N P NP
P -> P
NP -> '{target_pronouns[0]}' | '{target_pronouns[1]}'
Det -> 'the' | 'a'
P -> 'of'
N -> '{noun_types[0]}' | '{noun_types[1]}' | '{noun_types[2]}'
""")
理想情况下,我希望能够传递一个包含 n 个代词和名词的列表,并使用没有显式字符串索引的每个元素来格式化字符串,所以像这样:
noun_types = ['port', 'harbor', 'harbour']
target_pronouns = ['rotterdam', 'moscow']
grammer1 = (
F"""
S -> Det N P NP
P -> P
NP -> '{target_pronouns[range(0, len(target_pronouns))]}'
Det -> 'the' | 'a'
P -> 'of'
N -> '{noun_types[range(0, len(target_pronouns))]}'
""")
但是,我不确定如何实现 |
运算符,更不用说在进行字符串格式化时的任何条件格式化了。语法格式基于在此上下文中使用的 nltk 语法构造函数:
from nltk.parse.generate import generate
from nltk import CFG
grammar = CFG.fromstring(grammer1)
for sentence in generate(grammar, n = 10, depth = 5):
words = ' '.join(sentence)
这个问题有点令人困惑,所以我很乐意尝试澄清任何困惑!
所以我认为有一种 hacky 方法可以做到这一点:事先使用 '
字符转义字符串,然后使用 " | ".join()
.[=18= 将它们插入 f 字符串]
在输入列表的每个字符串前后添加 '
:
noun_types = [f"'{noun}'" for noun in noun_types]
target_pronouns = [f"'{pronoun}'" for pronoun in target_pronouns]
现在您可以使用 " | ".join()
将它们放入 f 字符串中。无论输入列表的大小如何,这都可以工作,不需要索引。
print(f"""
NP -> {' | '.join(target_pronouns)}
N -> {' | '.join(noun_types)}
""")
输出:
NP -> 'rotterdam' | 'moscow'
N -> 'port' | 'harbor' | 'harbour'
另一个解决方案,如果事情变得更复杂,可能是进入 Jinja templating 尽管现在似乎足以破解它并避免额外的库。
所以我创建了一系列语法,供我创建的 class 中的方法使用。每个列表可以是 n 个元素长,因此通过列表索引放置每个单词是直接的 wordlist[1:]
,但是我需要使用 |
运算符并且不能使用显式字符串索引来完成(至少我认同)。这是我到目前为止写的:
noun_types = ['port', 'harbor', 'harbour']
target_pronouns = ['rotterdam', 'moscow']
grammer1 = (
F"""
S -> Det N P NP
P -> P
NP -> '{target_pronouns[0]}' | '{target_pronouns[1]}'
Det -> 'the' | 'a'
P -> 'of'
N -> '{noun_types[0]}' | '{noun_types[1]}' | '{noun_types[2]}'
""")
理想情况下,我希望能够传递一个包含 n 个代词和名词的列表,并使用没有显式字符串索引的每个元素来格式化字符串,所以像这样:
noun_types = ['port', 'harbor', 'harbour']
target_pronouns = ['rotterdam', 'moscow']
grammer1 = (
F"""
S -> Det N P NP
P -> P
NP -> '{target_pronouns[range(0, len(target_pronouns))]}'
Det -> 'the' | 'a'
P -> 'of'
N -> '{noun_types[range(0, len(target_pronouns))]}'
""")
但是,我不确定如何实现 |
运算符,更不用说在进行字符串格式化时的任何条件格式化了。语法格式基于在此上下文中使用的 nltk 语法构造函数:
from nltk.parse.generate import generate
from nltk import CFG
grammar = CFG.fromstring(grammer1)
for sentence in generate(grammar, n = 10, depth = 5):
words = ' '.join(sentence)
这个问题有点令人困惑,所以我很乐意尝试澄清任何困惑!
所以我认为有一种 hacky 方法可以做到这一点:事先使用 '
字符转义字符串,然后使用 " | ".join()
.[=18= 将它们插入 f 字符串]
在输入列表的每个字符串前后添加 '
:
noun_types = [f"'{noun}'" for noun in noun_types]
target_pronouns = [f"'{pronoun}'" for pronoun in target_pronouns]
现在您可以使用 " | ".join()
将它们放入 f 字符串中。无论输入列表的大小如何,这都可以工作,不需要索引。
print(f"""
NP -> {' | '.join(target_pronouns)}
N -> {' | '.join(noun_types)}
""")
输出:
NP -> 'rotterdam' | 'moscow'
N -> 'port' | 'harbor' | 'harbour'
另一个解决方案,如果事情变得更复杂,可能是进入 Jinja templating 尽管现在似乎足以破解它并避免额外的库。