2016-12-29 12 views
2

ファイル内に停止単語を削除したい(文、タブ、その後に単語を含む)。ストップワードは別ファイルにあり、言語はペルシャ語です。以下のコードは動作しますが、問題は、たとえば行内の停止語を削除しますが、他の行では同じ停止語は削除しません。それはほぼすべてのストップワードのために起こった。私はそれが正規化のためかもしれないと思った。だから、私は2つのファイルをハマムモジュールをインポートして正規化しました(ハマムはペルシア語のNLTKのようです)。しかし、問題に変化はありませんでした。体が助けることができますか?ファイル内の特定の単語を削除する

from hazm import* 
punctuation = '!"#$%&\'()*+,-./:;<=>[email protected][\\]^_`{|}~،؟«؛' 

file1 = "stopwords.txt" 
file2 = "test/پر.txt" 


witoutStops = [] 
corpuslines = [] 

def RemStopWords (file1, file2): 
    with open(file1, encoding = "utf-8") as stopfile: 
     normalizer = Normalizer() 
     stopwords = stopfile.read() 
     stopwords = normalizer.normalize(stopwords) 
     with open(file2, encoding = "utf-8") as trainfile: 
      with open ("y.txt", "w", encoding = "utf-8") as newfile: 
       for line in trainfile: 
        tmp = line.strip().split("\t") 
        tmp[0] = normalizer.normalize(tmp[0]) 
        corpuslines.append(tmp) 
        for row in corpuslines: 
         line = "" 
         tokens = row[0].split() 
         for token in tokens: 
          if token not in stopwords: 
           line += token + " " 
        line = line.strip() + "\n" 
        for i in punctuation: # deletes punctuations 
         if i in line: 
          line = line.replace(i, "") 
        newfile.write(line) 
        witoutStops.append (line) 

ストップワードファイル: https://www.dropbox.com/s/irjkjmwkzwnnpnk/stopwords.txt?dl=0

ファイル: https://www.dropbox.com/s/p4m8san3xhr0pdj/%D9%BE%D8%B1.txt?dl=0

+0

[正規表現を使用してストップワードを削除する]の可能な複製(http://stackoverflow.com/questions/41417528/delete-stop-words-using- regular-expression) –

答えて

0

を、私は問題を発見しました。これは、ある言葉では、句読点が単語に付いていて、コードでは句読点ではなく単語の一部としてカウントされるからです。最初に句読点を削除する場合は、この部分に属するコードの3行を "tmp [0] = normalizer.normalize(tmp [0])"行の下に移動し、ストップワードを削除すると、すべてのストップ言葉は省略されます。