python - 如何从 NLP 的标点符号中去除字符串(撇号除外)

标签 python nlp nltk

我正在使用以下“最快”的方法从字符串中删除标点符号:

text = file_open.translate(str.maketrans("", "", string.punctuation))

但是,它会从标记中删除所有标点符号,包括撇号,例如 shouldn't 将其转换为 shouldnt

问题是我正在使用 NLTK 库作为停用词,而标准停用词不包含此类不带撇号的示例,而是包含如果我使用 NLTK 分词器分割文本时 NLTK 会生成的标记。例如,对于 shouldnt,包含的停用词是 shouldn、shouldnt、t

我可以添加额外的停用词或从 NLTK 停用词中删除撇号。但这两种解决方案在某种程度上似乎都不“正确”,因为我认为在进行标点符号清理时应该保留撇号。

有没有办法在快速标点符号清理时保留撇号?

最佳答案

>>> from string import punctuation
>>> type(punctuation)
<class 'str'>
>>> my_punctuation = punctuation.replace("'", "")
>>> my_punctuation
'!"#$%&()*+,-./:;<=>?@[\\]^_`{|}~'
>>> "It's right, isn't it?".translate(str.maketrans("", "", my_punctuation))
"It's right isn't it"

关于python - 如何从 NLP 的标点符号中去除字符串(撇号除外),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59877761/

相关文章:

Python - 执行分数的字符串代码

python - 如何访问 Django 模板中的枚举类型

java - StanleyNLP - TokensRegexNERAnnotator.readEntries 处的 ArrayIndexOutOfBoundsException(TokensRegexNERAnnotator.java :696))

python - 使用 Python 中的 Wordnet 同义词集处理意大利语

python -/tmp/python.cache_root 中的多个文件

python - 查找定义范围内的值的子范围

python - 如何向 NLTK 中的停用词添加更多语言?

python - babelize_shell() 在 NLTK 包中不起作用

python - 来自gensim的单词共现矩阵

python - NLTK Brill-Tagger 在 2.5 小时后退出,退出代码为 137。出了什么问题?