说我有需要用字典值填充的模板:
我的模板是这样的:
类似于上面的情况,如果:
注意,它仅返回水果而不是卧室,因为卧室不在地点的值中。
我的模板是这样的:
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'}
注意,它仅返回水果而不是卧室,因为卧室不在地点的值中。