我正在使用下面的“最快”方法从字符串中删除标点符号:
text = file_open.translate(str.maketrans("", "", string.punctuation))
Run Code Online (Sandbox Code Playgroud)
但是,它从标记中删除了所有标点符号,包括撇号,例如shouldn't将其变为shouldnt。
问题是我使用 NLTK 库作为停用词,而标准停用词不包含此类不带撇号的示例,而是包含如果我使用 NLTK 标记生成器来分割文本时 NLTK 会生成的标记。例如,shouldnt包含的停用词是shouldn, shouldn't, 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"
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5146 次 |
| 最近记录: |