NLTK tokenizer 和 Stanford corenlp tokenizer 无法区分句点 (.) 处没有 space 的 2 个句子

NLTK tokenizer and Stanford corenlp tokenizer cannot distinct 2 sentences without space at period (.)

我的数据集中有 2 个句子:

w1 = 我是 Pusheen cat.I 很可爱。 # 句号
后没有 space w2 = 我是猫 Pusheen。我很可爱。 # 在句点

之后 space

当我使用 NKTL 分词器(word 和 sent)时,nltk 无法区分 cat.I.

这里是分词

>>> nltk.word_tokenize(w1, 'english')
['I', 'am', 'Pusheen', 'the', 'cat.I', 'am', 'so', 'cute']
>>> nltk.word_tokenize(w2, 'english')
['I', 'am', 'Pusheen', 'the', 'cat', '.', 'I', 'am', 'so', 'cute']

并发送标记化

>>> nltk.sent_tokenize(w1, 'english')
['I am Pusheen the cat.I am so cute']
>>> nltk.sent_tokenize(w2, 'english')
['I am Pusheen the cat.', 'I am so cute']

请问如何解决?即:在我的数据集中将 nlkt 检测为 w2,有时单词和标点符号会粘在一起。

更新: 尝试了 Stanford CoreNLP 3.7.0,它们也无法将 'cat.I' 区分为 'cat'、'.'、'I'

meow@meow-server:~/projects/stanfordcorenlp$ java edu.stanford.nlp.process.PTBTokenizer sample.txt
I
am
Pusheen
the
cat.I
am
so
cute
.
PTBTokenizer tokenized 9 tokens at 111.21 tokens per second.

它是故意这样实现的——在它之后没有 space 的句点通常并不表示句子的结束(想想“版本 4.3”、“即"、"A.M." 等)。如果你有一个语料库,其中句号后没有 space 的句子结尾很常见,你必须在将文本发送到 NLTK 之前用正则表达式或类似的东西预处理文本。

一个好的经验法则通常是小写字母后跟句点后跟大写字母通常表示句子结束。在这种情况下,要在句点后插入 space,您可以使用正则表达式,例如

import re
w1 = re.sub(r'([a-z])\.([A-Z])', r'. ', w1)

哪里

  • [a-z] 匹配任何小写字符
  • \. 匹配句号
  • [A-Z] 匹配任何大写字符
  • </code> 是 (括号)</li> 中的 <a href="">reference to the first group</a> <li><code>是对(括号)
  • 中第二组的引用