我想从具有数据的键值列表样式的句子中搜索关键字,并返回与句子引用匹配的句子。我一直在研究checkSentence()。我知道怎么写才能只得到报价结果:
def checkSentence(quote_list, searchItems):
result_sentence = [all([searchingWord in searchingSentence for searchingWord in searchItems]) for searchingSentence in quote_list]
return [quote_list[i] for i in range(0, len(result_sentence)) if result_sentence[i]]
checkResult = checkSentence(quote_list, searchItems)
quoteResult_list = []
for quote in checkResult:
quoteResult_list.append(quote)
print(len(quoteResult_list))
print(quoteResult_list)
现在,我想通过引用(“文章”)获取句子(数据中的“内容”)。就像是“世界由甜蜜组成。”:“世界”。
应该有两个for循环,第一层是句子搜索,第二个for循环应该获取句子的“文章”。我不知道为什么它不起作用?看来错误是在item_list [“ quote”]和item_list [“ article”]上?非常感谢!
代码如下:
import json
import os
# data part
data = {
"title": "Vulnerable",
"items": [
{
"article": "The World",
"content": [
"The world is made of sweet.",
"The sweet tastes so good.",
]
},
{
"article": "The Disaster",
"content": [
"That is the sweet wrapping with poison.",
"Is that true? Are you kidding?",
]
},
{
"article": "The Truth",
"content": [
"Trust me. That is not sweet!",
"You see? That is why!",
]
}
]
}
# keywords for searching
searchItems = ["sweet", "is"]
# deal with data to list
item_list = []
quote_list = []
article_list = []
for item in data["items"]:
article = item["article"]
for quote in item["content"]:
item_list.append({article, quote})
quote_list.append(quote)
article_list.append(article)
# check if sentences include keywords
def checkSentence(item_list, searchItems):
for sentence in item_list["quote"]:
result_sentence = [all([searchingWord in searchingSentence for searchingWord in searchItems]) for searchingSentence in sentence]
sententceResult = [item_list[i] for i in range(0, len(result_sentence)) if result_sentence[i]]
for article in item_list["article"]:
return_article = [all([searchingWord in searchingSentence for searchingWord in searchItems]) for searchingSentence in article]
quoteResult = [item_list[i] for i in range(0, len(return_article)) if return_article[i]]
return sententceResult, quoteResult
# make the searching result as list item
checkResult = checkSentence(item_list, searchItems)
quoteResult_list = []
for quote in checkResult:
quoteResult_list.append(quote)
print(quoteResult_list)
问题来源:stackoverflow
将文章和引号添加到“ item_list”时,请使用元组,因为这样可以更轻松地找到匹配项:
item_list.append((article,quote))
现在到checkSentence
函数:
因为现在我们正在使用元组,所以我们可以同时保存文章和相应的句子。然后,您只需要在“句子”中搜索关键字,如果匹配,则将“文章”和“句子”都添加到“匹配”列表中。之后,您只需返回结果列表即可。
这是最终代码(无数据):
# keywords for searching
searchItems = ["sweet", "is"]
# deal with data to list
item_list = []
quote_list = []
article_list = []
for item in data["items"]:
article = item["article"]
for quote in item["content"]:
# use tuples to make later use easier
item_list.append((article, quote))
quote_list.append(quote)
article_list.append(article)
# check if sentences include keywords
def checkSentence(item_list, searchItems):
matches = []
# unpack the tuples and iterate over the list
for article, sentence in item_list:
# check for matching key words in sentence
if all([searchingWord in sentence for searchingWord in searchItems]):
# add both article and sentence to the matches, if key words are present
matches.append((article, sentence))
return matches
# make the searching result as list item
checkResult = checkSentence(item_list, searchItems)
print(checkResult)
回答来源:stackoverflow
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。