Reputation: 593
I would like to search keywords from sentence with the data is in key-value list style, and return the matched sentences with the sentences references. I have been working on the checkSentence(). I know how to write to get the result only for quote:
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)
And now I would like to make it to get the sentences ("content" in the data, let say) with the references (the "article"). It would be something like ["The world is made of sweet.":"The World"].
There should be two for-loops that the first layer is the sentences searching and the second for-loop should be getting the "article" of the sentences. I have no idea why it doesn't work? It looks like the error is at item_list["quote"] and item_list["article"]? Many thanks!
The code is as below:
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)
Upvotes: 0
Views: 68
Reputation: 91
When adding the articles and quotes to item_list
use tuples, as it makes it easier when finding the matches:
item_list.append((article, quote))
Now to the checkSentence
function:
Because now we're using tuples, we can save both the article and corresponding sentence simultaneously. Then you only have to search for the key words in sentence
and, if it matches, add both article
and sentence
to the matches
list. Afterwards you just return the list with he results.
Here's the final code (without the data):
# 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)
Upvotes: 1