In this solution we are going to find the Complete sentence that contains your keyword that you are looking for. With the help of python we can achieve that. In python there is specific library called spacy that helped to run this code. In that library Matcher method that lets you find words and phrases using rules describing their token attributes. In this solution kit, I am sharing the code snippet and library that I use to match the pattern in Python which can be executed directly in the IDE.
Preview of the output that you will get on running this code from your IDE
In this solution we have used Matcher function of SpaCy Library.
import spacy from spacy.matcher import PhraseMatcher text = """I like tomtom and I cannot lie. In computer science, artificial intelligence (AI), sometimes called machine intelligence, is intelligence demonstrated by machines, unlike the natural intelligence displayed by humans and animals. Leading AI textbooks define the field as the study of "intelligent agents": any device that perceives its environment and takes actions that maximize its chance of successfully achieving its goals. Colloquially, the term "artificial intelligence" is often used to describe machines (or computers) that mimic "cognitive" functions that humans associate with the human mind, such as "learning" and "problem solving". """ nlp = spacy.load("en_core_web_sm") phrase_matcher = PhraseMatcher(nlp.vocab) phrases = ['machine learning', 'artificial intelligence'] patterns = [nlp(text) for text in phrases] phrase_matcher.add('AI', None, *patterns) doc = nlp(text) for sent in doc.sents: for match_id, start, end in phrase_matcher(nlp(sent.text)): if nlp.vocab.strings[match_id] in ["AI"]: print(sent.text) In computer science, artificial intelligence (AI), sometimes called machine intelligence, is intelligence demonstrated by machines, unlike the natural intelligence displayed by humans and animals. Colloquially, the term "artificial intelligence" is often used to describe machines (or computers)
I hope you found this useful. I have added the link to dependent libraries, version information in the following sections.
I found this code snippet by searching for "How to extract sentence with key phrases in SpaCy" in kandi. You can try any such use case!
In this snippet we are using a Language model (en_core_web_sm)
Check the user's spacy version using pip show spacy command in users terminal.
I tested this solution in the following versions. Be mindful of changes when working with other versions.
Using this solution, we can collect the complete sentence that user need with the help of function in spacy . This process also facilities an easy to use, hassle free method to create a hands-on working version of code which would help us collect the sentence or keywords the user needs in python.
Open Weaver – Develop Applications Faster with Open Source