借助nltk.tokenize.word_tokenize()
方法,我們可以使用以下方法從字符串提取令牌:tokenize.word_tokenize()
方法。它實際上從單個單詞返回音節。一個單詞可以包含一個或兩個音節。
用法:tokenize.word_tokenize()
返回: Return the list of syllables of words.
範例1:
在這個例子中,我們可以通過使用tokenize.word_tokenize()
方法,我們能夠從單詞或句子流中提取音節。
# import SyllableTokenizer() method from nltk
from nltk import word_tokenize
# Create a reference variable for Class word_tokenize
tk = SyllableTokenizer()
# Create a string input
gfg = "Antidisestablishmentarianism"
# Use tokenize method
geek = tk.tokenize(gfg)
print(geek)
輸出:
[‘An’, ‘ti’, ‘dis’, ‘es’, ‘ta’, ‘blish’, ‘men’, ‘ta’, ‘ria’, ‘nism’]
範例2:
# import SyllableTokenizer() method from nltk
from nltk.tokenize import word_tokenize
# Create a reference variable for Class word_tokenize
tk = SyllableTokenizer()
# Create a string input
gfg = "Gametophyte"
# Use tokenize method
geek = tk.tokenize(gfg)
print(geek)
輸出:
[‘Ga’, ‘me’, ‘to’, ‘phy’, ‘te’]
相關用法
- 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.tokenize.mwe()用法及代碼示例
- Python nltk.WhitespaceTokenizer用法及代碼示例
- Python nltk.TweetTokenizer()用法及代碼示例
- Python NLTK tokenize.regexp()用法及代碼示例
- Python NLTK tokenize.WordPunctTokenizer()用法及代碼示例
注:本文由純淨天空篩選整理自Jitender_1998大神的英文原創作品 Python NLTK | nltk.tokenizer.word_tokenize()。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。