Python NLTK | nltk.WhitespaceTokenizer
在nltk.tokenize.WhitespaceTokenizer()
方法的帮助下,我们可以使用tokenize.WhitespaceTokenizer()
方法从没有空格、换行和制表符的单词或句子的字符串中提取标记。
Syntax : tokenize.WhitespaceTokenizer()
Return : Return the tokens from a string
示例 #1:
在这个例子中,我们可以看到通过使用tokenize.WhitespaceTokenizer()
方法,我们能够从单词流中提取标记。
# import WhitespaceTokenizer() method from nltk
from nltk.tokenize import WhitespaceTokenizer
# Create a reference variable for Class WhitespaceTokenizer
tk = WhitespaceTokenizer()
# Create a string input
gfg = "GeeksforGeeks \nis\t for geeks"
# Use tokenize method
geek = tk.tokenize(gfg)
print(geek)
输出 :
[‘GeeksforGeeks’, ‘is’, ‘for’, ‘geeks’]
示例 #2:
# import WhitespaceTokenizer() method from nltk
from nltk.tokenize import WhitespaceTokenizer
# Create a reference variable for Class WhitespaceTokenizer
tk = WhitespaceTokenizer()
# Create a string input
gfg = "The price\t of burger \nin BurgerKing is Rs.36.\n"
# Use tokenize method
geek = tk.tokenize(gfg)
print(geek)
输出 :
[‘The’, ‘price’, ‘of’, ‘burger’, ‘in’, ‘BurgerKing’, ‘is’, ‘Rs.36.’]