简体   繁体   English

我该如何解决这个“IndentationError: expected an indented block”?

[英]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.这最后一部分的目标是,如果在删除停用词后,单词仍然留在句子中,那么句子应该作为字符串返回,否则返回 null。我将非常感谢任何建议。

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.它的格式是x if condition else y this 在同一行并且没有:在 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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