我有一个句子存储在文本文件中。我有一个短语Json File。我想从句子中匹配该短语并获取其键。
短语JSON
[
{
"1": "['Restaurants','Restaurant','Eat','Food']"
},
{
"2": "['Hotel','Hotels']"
},
{
"3": "['Mall','Malls']"
},
{
"4": "['Garden','Gardens','Park','Parks']"
},
{
"5": "['Cafe','Cafes']"
}
]
主要代码
import json
while True:
with open('JSON\phrases.json') as p, open('JSON\delhi_pla.json') as d, open('store.txt') as text:
phrases = json.load(p)
places = json.load(d)
text_file = text.readlines()
keys = ""
bag = ""
for items in phrases:
keys = items.keys()
for item1 in items.values():
bag = item1.lower()
# for yet in item1:
# print(yet)
# print(yet)
# break
for sentence in text_file:
if any(word in sentence.lower() for word in bag):
# if sentence.lower() in item1.lower():
# print(item1.lower())
for iy in keys:
print(iy)
break
print("Got it")
print("-----------------------------------------------------------------------------------------")
with open('store.txt', 'r+') as text:
text.truncate(0)
break
假设该文本文件的句子为“伦敦的公园”或“钦奈的酒店”。因此,此程序应查找与JSON值匹配的单词并返回其特定键。就像,上面匹配“公园”和“酒店” 因此它应该给出输出:“ 4”或“ 5”。 预先感谢。