通常のテキスト ファイルを使用して、キャリッジ リターン ("\n") で区切られた削除する単語を含む別のファイル (ストップワード) で識別される単語を削除しようとしています。
現在、両方のファイルをリストに変換して、各リストの要素を比較できるようにしています。この関数を動作させましたが、ストップワード ファイルで指定したすべての単語が削除されません。どんな助けでも大歓迎です。
def elimstops(file_str): #takes as input a string for the stopwords file location
stop_f = open(file_str, 'r')
stopw = stop_f.read()
stopw = stopw.split('\n')
text_file = open('sample.txt') #Opens the file whose stop words will be eliminated
prime = text_file.read()
prime = prime.split(' ') #Splits the string into a list separated by a space
tot_str = "" #total string
i = 0
while i < (len(stopw)):
if stopw[i] in prime:
prime.remove(stopw[i]) #removes the stopword from the text
else:
pass
i += 1
# Creates a new string from the compilation of list elements
# with the stop words removed
for v in prime:
tot_str = tot_str + str(v) + " "
return tot_str