【问题标题】:Python - NLTK separating punctuationPython - NLTK 分隔标点符号
【发布时间】:2016-09-09 18:12:06
【问题描述】:

我对 Python 还是很陌生,我正在尝试使用 NLTK 来删除我的文件的停用词。 该代码正在运行,但是它正在分隔标点符号,如果我的文本是带有提及(@user)的推文,我会得到“@user”。 稍后我需要做一个词频,我需要提及和主题标签才能正常工作。 我的代码:

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()

编辑 不确定这是否是最好的方法,但我是这样修复的:

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

【问题讨论】:

    标签: python nltk


    【解决方案1】:

    您可以使用 nltk 提供的 Twitter-aware tokenizer 代替 word_tokenize

    from nltk.tokenize import TweetTokenizer
    
    ...
    tknzr = TweetTokenizer()
    ...
    word_tokens = tknzr.tokenize(linha)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-03-10
      • 2017-09-21
      • 1970-01-01
      • 1970-01-01
      • 2021-12-18
      • 1970-01-01
      相关资源
      最近更新 更多