Skip to content

Instantly share code, notes, and snippets.

@Logic2apply
Last active November 19, 2021 07:44
Show Gist options
  • Save Logic2apply/3a769d253f15e4880bdd0a44c04207e0 to your computer and use it in GitHub Desktop.
Save Logic2apply/3a769d253f15e4880bdd0a44c04207e0 to your computer and use it in GitHub Desktop.
You are given few sentences as a list (Python list of sentences). Take a query string as an input from the user. You have to pull out the sentences matching this query inputted by the user in decreasing order of relevance after converting every word in the query and the sentence to lowercase. Most relevant sentence is the one with the maximum nu…

Search The Sentence

You are given few sentences as a list (Python list of sentences). Take a query string as an input from the user. You have to pull out the sentences matching this query inputted by the user in decreasing order of relevance after converting every word in the query and the sentence to lowercase. Most relevant sentence is the one with the maximum number of matching words with the query. Sentences = [“This is good”, “python is good”, “python is not python snake”]

Input:

Please input your query string: "Python is"

Output:

3 results found:

  1. python is not python snake
  2. python is good
  3. This is good
'''
You are given few sentences as a list (Python list of sentences). Take a query string as an input from the user. You have to pull out the sentences matching this query inputted by the user in decreasing order of relevance after converting every word in the query and the sentence to lowercase. Most relevant sentence is the one with the maximum number of matching words with the query.
Sentences = [“This is good”, “python is good”, “python is not python snake”]
Input:
Please input your query string
“Python is”
Output:
3 results found:
1. python is not python snake
2. python is good
3. This is good
'''
def mathingWords(sentence1, sentence2):
words1 = sentence1.strip().split(" ")
words2 = sentence2.strip().split(" ")
score = 0
for word1 in words1:
for word2 in words2:
# print(f"Matching {word1} with {word2}")
if word1.lower() == word2.lower():
score += 1
return score
if __name__ == "__main__":
# mathingWords("This is good", "python is good")
sentences = ["python is a good", "this is snake",
"harry is a good boy", "Subscribe to code with harry"]
query = input("Please enter the query string\n")
scores = [mathingWords(query, sentence) for sentence in sentences]
# print(scores)
sortedSentScore = [sentScore for sentScore in sorted(
zip(scores, sentences), reverse=True) if sentScore[0] !=0 ]
# print(sortedSentScore)
print(f"{len(sortedSentScore)} results found!")
for score, item in sortedSentScore:
print(f" \"{item}\": with a score of {score}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment