简体   繁体   中英

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM