用字典填充 Python 字符串格式

3
说我有需要用字典值填充的模板:
我的模板是这样的:
templates = [
   "I have four {fruit} in {place}", 
   "I have four {fruit} and {grain} in {place}",
   ...
]

有如下这样的字典:

my_dict = {'fruit': ['apple', 'banana', 'mango'], 
           'place': ['kitchen', 'living room'],
           'grain' : ['wheat', 'rice']
          }

假设我有这样一句话:

sentence = "I have four apple in kitchen" 

给定这个句子、模板和词典,我想知道这个句子匹配了哪个模板,并返回它匹配的值,如下所示:

{'fruit': 'apple', 'place': 'kitchen'}

类似于上面的情况,如果:
Input: "I have four apple and wheat in kitchen"
Output: {'fruit': 'apple', 'grain': 'wheat', 'place': 'kitchen'}

如果您能处理以下内容,那就太好了:

Input: "I have four apple in bedroom" 
Output: {'fruit': 'apple'}

注意,它仅返回水果而不是卧室,因为卧室不在地点的值中。

1
{btsdaf} - cs95
{btsdaf} - user3613909
1个回答

6
将格式化字符串转换为正则表达式:
import re

words = {k: '(?P<{}>{})'.format(k, '|'.join(map(re.escape, v))) for k, v in my_dict.items()}
patterns = [re.compile(template.format(**words)) for template in templates]

这会生成形如我在(?P<place>kitchen|living room)有四个(?P<fruit>apple|banana|mango)的模式。匹配这些模式会得到您期望的输出:
for pattern in patterns:
    match = pattern.match(sentence)
    if match:
        matched_words = match.groupdict()

这是一种非常快速的O(N)方法来精确匹配句子:
>>> import re
>>> templates = [
...    "I have four {fruit} in {place}",
...    "I have four {fruit} and {grain} in {place}",
... ]
>>> my_dict = {'fruit': ['apple', 'banana', 'mango'],
...            'place': ['kitchen', 'living room'],
...            'grain' : ['wheat', 'rice']
...           }
>>> def find_matches(sentence):
...     for pattern in patterns:
...         match = pattern.match(sentence)
...         if match:
...             return match.groupdict()
...
>>> find_matches("I have four apple in kitchen")
{'fruit': 'apple', 'place': 'kitchen'}
>>> find_matches("I have four apple and wheat in kitchen")
{'fruit': 'apple', 'grain': 'wheat', 'place': 'kitchen'}

如果您需要模板与部分句子匹配,请将可选部分包含在(?...)组中:

"I have four {fruit} in (?{place})"

或者在单词列表中添加\w+(除了有效单词以外),然后在匹配后验证groupdict()结果与my_dict是否相符。对于“在卧室中”的情况,\w+将匹配bedroom部分,但对于place,它不会在my_dict列表中找到,例如。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接