2016-09-09 6 views
2

저는 파이썬에 대해 아주 익숙합니다. NLTK를 사용하여 파일의 불용어를 제거하려고합니다. 내 텍스트가 짹짹 (@user) 인 경우 코드가 작동하지만 구두점이 구분됩니다. "@ user"를 얻습니다. 나중에 단어 빈도를해야 할 것입니다. 제대로 작동하려면 멘션과 해시 태그가 필요합니다. 내 코드 :Python - 구두점을 구분하는 NLTK

from nltk.corpus import stopwords 
from nltk.tokenize import word_tokenize 
import codecs 
arquivo = open('newfile.txt', encoding="utf8") 
linha = arquivo.readline() 
while linha: 
    stop_word = set(stopwords.words("portuguese")) 
    word_tokens = word_tokenize(linha) 
    filtered_sentence = [w for w in word_tokens if not w in stop_word] 
    filtered_sentence = [] 
    for w in word_tokens: 
     if w not in stop_word: 
      filtered_sentence.append(w) 
    fp = codecs.open("stopwords.txt", "a", "utf-8") 
    for words in (filtered_sentence): 
     fp.write(words + " ") 
    fp.write("\n") 
    linha= arquivo.readline() 

편집 이 그것을 할 수있는 가장 좋은 방법입니다,하지만 난 그것을 이런 식으로 고정 확실하지 : word_tokenize 대신

for words in (filtered_sentence): 
     fp.write(words) 
     if words not in string.punctuation: 
      fp.write(" ") 
    fp.write("\n") 

답변

3

을, 당신은 Twitter-aware tokenizer 제공을 사용할 수 있습니다 nltk :

from nltk.tokenize import TweetTokenizer 

... 
tknzr = TweetTokenizer() 
... 
word_tokens = tknzr.tokenize(linha) 
+0

이 방법이 더 좋습니다. 대단히 감사합니다. – urukh