[英]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
我收到最后一個錯誤:
這最后一部分的目標是,如果在刪除停用詞后,單詞仍然留在句子中,那么句子應該作為字符串返回,否則返回 null。我將非常感謝任何建議。
else None
^
IndentationError: expected an indented block
我猜您想使用三元運算符。
它的格式是x if condition else y
this 在同一行並且沒有:
在 if else 之后。
所以你最后的退貨聲明應該是:
return " ".join(filtered_sentence) if len(filtered_sentence)>0 else None
您的整個代碼沒有正確縮進
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.