Phrase Extraction in a string in Python

In this tutorial, you are going to learn about Phrase Extraction in a string in Python.

What is Phrase Extraction?

Here we can take one string and select the phrase which we want to extract from a string.

We achieve Extraction in a string using Python here. There are different types to achieve phrase extraction in a string one of the method is using Python and the other is Machine Learning.

string = 'codespeedy is the best website for reading purpose'
print("The string is : " + str(string)) 
x = 3
operation = [initial for initial, ele in enumerate(string) if ele == ' '] 
result = string[operation[x - 1]: operation[-(x - 2)]].strip() 
print(" after phrase extraction the output is : " + str(result)) 


Now we are ready to run and see the output:

Phrase Extraction Output

Program Explanation

First of all, assign one message to String which we to extract the phrases of String.

Next, print that message as it is in String.

Initialize one variable x with the number which we want.

For Phrase Extraction, we have to do some operations. Initialize the Operation with the list which takes variable which we assigned before and it can find the element in String by using enumerate which takes the fixed values and made an if condition with element equals to ‘ ‘.

Next, the result made a string operation which takes variable along with strip() function.

Strip() function is used to extract the phrases from the argument which is passed.

Print the result which is remained in the result.


Leave a Reply

Your email address will not be published. Required fields are marked *