【发布时间】:2017-02-07 22:13:46
【问题描述】:
有很多关于删除标点符号的方法,但我似乎找不到任何保留它的方法。
如果我这样做:
from nltk import word_tokenize
test_str = "Some Co Inc. Other Co L.P."
word_tokenize(test_str)
Out[1]: ['Some', 'Co', 'Inc.', 'Other', 'Co', 'L.P', '.']
最后一个“.”被推入自己的令牌中。但是,如果末尾有另一个单词,则最后一个“。”被保留:
from nltk import word_tokenize
test_str = "Some Co Inc. Other Co L.P. Another Co"
word_tokenize(test_str)
Out[1]: ['Some', 'Co', 'Inc.', 'Other', 'Co', 'L.P.', 'Another', 'Co']
我希望这始终作为第二种情况执行。现在,我正在做:
from nltk import word_tokenize
test_str = "Some Co Inc. Other Co L.P."
word_tokenize(test_str + " |||")
因为我对扔掉“|||”很有信心在任何给定时间,但不知道我可能想要保留的其他标点符号可能会被删除。有没有更好的方法来做到这一点?
【问题讨论】: