开发者社区> 问答> 正文

如何从键值列表中的句子中搜索关键字,并获得具有相对引用的句子的匹配结果?

我想从具有数据的键值列表样式的句子中搜索关键字,并返回与句子引用匹配的句子。我一直在研究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

展开
收起
is大龙 2020-03-21 11:18:50 501 0
1 条回答
写回答
取消 提交回答
  • 将文章和引号添加到“ 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

    2020-03-21 11:19:13
    赞同 展开评论 打赏
问答分类:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载