借助nltk.tokenize.WhitespaceTokenizer()
方法,我們可以通過使用以下命令從單詞或句子的字符串中提取標記,而無需空格,換行和製表符tokenize.WhitespaceTokenizer()
方法。
用法:tokenize.WhitespaceTokenizer()
返回: 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.’]
相關用法
- Python nltk.tokenize.SExprTokenizer()用法及代碼示例
- Python nltk.tokenize.LineTokenizer用法及代碼示例
- Python NLTK nltk.tokenize.ConditionalFreqDist()用法及代碼示例
- Python nltk.tokenize.SpaceTokenizer()用法及代碼示例
- Python nltk.tokenize.TabTokenizer()用法及代碼示例
- Python nltk.tokenize.StanfordTokenizer()用法及代碼示例
- Python nltk.tokenizer.word_tokenize()用法及代碼示例
- Python nltk.tokenize.mwe()用法及代碼示例
- Python nltk.TweetTokenizer()用法及代碼示例
- Python NLTK tokenize.regexp()用法及代碼示例
- Python NLTK tokenize.WordPunctTokenizer()用法及代碼示例
注:本文由純淨天空篩選整理自Jitender_1998大神的英文原創作品 Python NLTK | nltk.WhitespaceTokenizer。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。