我试图将一个段落分成单词.我手头有可爱的nltk.tokenize.word_tokenize(发送),但是帮助(word_tokenize)说,"这个标记器设计用于一次一个句子."
有没有人知道如果你在段落上使用它会发生什么,即最多5个句子呢?我自己尝试了一些简短的段落,似乎有效,但这几乎不是确凿的证据.
nltk.tokenize.word_tokenize(text)是一个瘦的包装函数,它调用TreebankWordTokenizer类tokenize的一个实例的方法,它显然使用简单的正则表达式来解析一个句子.
该类的文档声明:
此标记器假定文本已被分段为句子.任何句号 - 除了字符串末尾的句号 - 都被假定为它们所附加的单词的一部分(例如缩写等),并且不单独标记.
底层tokenize方法本身非常简单:
def tokenize(self, text):
for regexp in self.CONTRACTIONS2:
text = regexp.sub(r'\1 \2', text)
for regexp in self.CONTRACTIONS3:
text = regexp.sub(r'\1 \2 \3', text)
# Separate most punctuation
text = re.sub(r"([^\w\.\'\-\/,&])", r' \1 ', text)
# Separate commas if they're followed by space.
# (E.g., don't separate 2,500)
text = re.sub(r"(,\s)", r' \1', text)
# Separate single quotes if they're followed by a space.
text = re.sub(r"('\s)", r' \1', text)
# Separate periods that come before newline or end of string.
text = re.sub('\. *(\n|$)', ' . ', text)
return text.split()
Run Code Online (Sandbox Code Playgroud)
基本上,该方法通常做的是将句点标记为单独的标记,如果它落在字符串的末尾:
>>> nltk.tokenize.word_tokenize("Hello, world.")
['Hello', ',', 'world', '.']
Run Code Online (Sandbox Code Playgroud)
落在字符串中的任何句点都被标记为单词的一部分,假设它是缩写:
>>> nltk.tokenize.word_tokenize("Hello, world. How are you?")
['Hello', ',', 'world.', 'How', 'are', 'you', '?']
Run Code Online (Sandbox Code Playgroud)
只要这种行为是可以接受的,你应该没事.
| 归档时间: |
|
| 查看次数: |
3775 次 |
| 最近记录: |