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

蜻蜓

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

在添加文章和引号以item_list使用元组时,因为这样可以更轻松地找到匹配项:

item_list.append((article, quote))

现在到checkSentence函数:

因为现在我们正在使用元组,所以我们可以同时保存文章和相应的句子。然后,您只需搜索中的关键字sentence,如果匹配,则将article都添加sentencematches列表中。之后,您只需返回他的结果列表即可。

这是最终代码(无数据):

#    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)

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

Swift:如何在句子中搜索关键字

如何从多个句子中返回列表中的关键字?

在Elasticsearch中对关键字查询的句子

Excel如何找出句子中的关键字

如何在不同的 Pandas 数据框中搜索关键字并在父 DF 中更新或创建具有匹配关键字的新列

Excel-基于在具有多个单词的列中搜索关键字而返回行结果的列表

使用关键字在Python中打印句子

如何在Swift中的句子中的特定关键字之前获取一个单词?

从具有精确单词匹配的句子列表中获取句子:Python

如何在自动建议结果列表中突出显示搜索关键字 React JS

如何使用Python匹配列表中具有单个句子的每个单词

如何在实体列表中搜索关键字?

有没有一种更有效的方法来搜索句子中的关键字而无需在编译前就知道该关键字?

如何检查句子中是否存在关键字集?

如何找到所有结果与mysql中匹配的任何关键字

如何在href中获得具有相同关键字的元素

在数组中搜索关键字,仅使用NodeJS在MongoDB中获得匹配的元素

Javascript搜索并显示具有匹配关键字的div

在列中搜索列表中的关键字,并将所有匹配项返回到其他列

在数据框的每一行中进行搜索时,如何从关键字列表中获取匹配的关键字?

如何使用列表中具有字典关键字的元素访问字典值?

性能-如何在与R中给定句子匹配的单词列表中获得这些单词

如何匹配Lua中的句子

搜索表中的所有列并仅显示在elasticsearch中匹配关键字的那些列

在列表中搜索关键字并创建新列表

将鼠标悬停在关键字上会使句子/段落中的某些单词发光

TortoiseHg:在关键字搜索结果中不显示祖先

更改给定句子的关键字的颜色

从具有匹配模式的文档中删除某些句子