我有python 2.7,這是我的代碼,當我運行它,我得到這個錯誤:'繼續'不正確的循環。繼續不正確的循環
我知道'繼續'應該在循環內部,但我在if
裏面使用它,那我該怎麼辦?
from numpy import zeros
from scipy.linalg import svd
from math import log
from numpy import asarray, sum
#from nltk.corpus import stopwords
from sklearn.metrics.pairwise import cosine_similarity
#from nltk.stem import PorterStemmer
#from nltk.stem.isri import ISRIStemmer
import nltk
#from matplotlib import pyplot as plt
from snowballstemmer import stemmer
titles = [" ذهبت الاخت الى المدرسة","تقع المدرسة في الجبال",
"ذهب الام لزيارة ابنتها في المدرسة ","تحضر الام الكعكة" ]
ar_stemmer = stemmer("arabic")
stopwords = ['ثم','و','حتى','الى','على','في']
ignorechars = ''',:'!'''
class LSA(object):
def __init__(self, stopwords, ignorechars):
self.stopwords = stopwords
self.ignorechars = ignorechars
self.wdict = {}
self.dcount = 0
def parse(self, doc):
#tokens=nltk.word_tokenise(titles)
#words = doc.split();
#ar_stemmer = stemmer("arabic")
for word in titles.split(" "):
# w = w.lower()
#for w in titles.split(" "):
stem = ar_stemmer.stemWord(word)
#st = ISRIStemmer()
#for w in words :
#join = w.decode('Windows-1256')
# w= st.stem(w.decode('utf-8'))
if stem in self.stopwords:
continue
elif stem in self.wdict:
self.wdict[stem].append(self.dcount)
else:
self.wdict[stem] = [self.dcount]
self.dcount += 1
您的縮進嚴重混亂。一致縮進代碼以解決錯誤。 – user2357112
爲了避免上面的問題,你的if語句實際上並不是for循環,所以再縮進一個以便成爲循環的一部分。 – chrishorton
正如你所寫,你的'parse'函數只處理'stem'的最後一個值。那是故意的嗎?如果不是,則將'if'語句縮進到與'stem = ar_stemmer.stemWord(word)'相同的級別。 –