Removing Stopwords from Text in Python

  • Share this:

Code introduction


This function removes stopwords from the given text based on the specified language.


Technology Stack : lingua

Code Type : Text processing

Code Difficulty : Intermediate


                
                    
import random
from lingua import stopwords

def remove_stopwords_from_text(text, language='english'):
    """
    This function removes stopwords from the given text based on the specified language.
    """
    stop_words = stopwords.words(language)
    words = text.split()
    filtered_words = [word for word in words if word.lower() not in stop_words]
    return ' '.join(filtered_words)                
              
Tags: