pythonpython-3.xif-statementnlptopic-modeling

How can I fix this "IndentationError: expected an indented block"?


def remove_stopwords(text,nlp,custom_stop_words=None,remove_small_tokens=True,min_len=2):
    if custom_stop_words:
       nlp.Defaults.stop_words |= custom_stop_words
    filtered_sentence =[] 
    doc = nlp (text)
    for token in doc:
    
        if token.is_stop == False: 
        
           if remove_small_tokens:
              if len(token.text)>min_len:
                 filtered_sentence.append(token.text)
          else:
              filtered_sentence.append(token.text) 
              return " ".join(filtered_sentence) 
          if len(filtered_sentence)>0
          else None

I am getting the error for the last else:

The goal of this last part is, if after the stopword removal, words are still left in the sentence, then the sentence should be returned as a string else return null. I'd be so thankful for any advice.

else None
^
IndentationError: expected an indented block

Solution

  • Your entire code is not properly indented

    def remove_stopwords(text,nlp,custom_stop_words=None,remove_small_tokens=True,min_len=2):
        if custom_stop_words:
            nlp.Defaults.stop_words |= custom_stop_words
    
        filtered_sentence =[] 
        doc = nlp (text)
        for token in doc:
            
            if token.is_stop == False: 
                
                if remove_small_tokens:
                    if len(token.text)>min_len:
                        filtered_sentence.append(token.text)
                else:
                    filtered_sentence.append(token.text)
                    
        if len(filtered_sentence) > 0:           
            return " ".join(filtered_sentence) 
        else:
            return None