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

Time:11-25

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

CodePudding user response:

I guess you want to use the ternary operator.

The format for it is x if condition else y this is on the same line and without the : after the if else.

So your last return statement should be :

return " ".join(filtered_sentence) if len(filtered_sentence)>0 else None

CodePudding user response:

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

  • Related