當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Python nltk.tokenize.SpaceTokenizer()用法及代碼示例


借助nltk.tokenize.SpaceTokenizer()方法,我們可以使用以下方法根據單詞之間的空間從字符串中提取標記tokenize.SpaceTokenizer()方法。

用法: tokenize.SpaceTokenizer()
返回:Return the tokens of words.

範例1:
在這個例子中,我們可以通過使用tokenize.SpaceTokenizer()方法,我們能夠從流中提取標記到單詞之間有空格的單詞。


# import SpaceTokenizer() method from nltk 
from nltk.tokenize import SpaceTokenizer 
     
# Create a reference variable for Class SpaceTokenizer 
tk = SpaceTokenizer() 
     
# Create a string input 
gfg = "Geeksfor Geeks.. .$$&* \nis\t for geeks"
     
# Use tokenize method 
geek = tk.tokenize(gfg) 
     
print(geek)

輸出:

[‘Geeksfor’, ‘Geeks..’, ‘.$$&*’, ‘\nis\t’, ‘for’, ‘geeks’]

範例2:

# import SpaceTokenizer() method from nltk 
from nltk.tokenize import SpaceTokenizer 
     
# Create a reference variable for Class SpaceTokenizer 
tk = SpaceTokenizer() 
     
# 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\t’, ‘of’, ‘burger’, ‘\nin’, ‘BurgerKing’, ‘is’, ‘Rs.36.\n’]



相關用法


注:本文由純淨天空篩選整理自Jitender_1998大神的英文原創作品 Python NLTK | nltk.tokenize.SpaceTokenizer()。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。