- Python - 文字處理
- Python - 文字處理簡介
- Python - 文字處理環境
- Python - 字串不可變性
- Python - 對行排序
- Python - 段落重新格式化
- Python - 統計段落中的詞語
- Python - 二進位制ASCII轉換
- Python - 字串作為檔案
- Python - 反向讀取檔案
- Python - 過濾重複單詞
- Python - 從文字中提取電子郵件
- Python - 從文字中提取URL
- Python - 美化列印
- Python - 文字處理狀態機
- Python - 首字母大寫和翻譯
- Python - 分詞
- Python - 刪除停用詞
- Python - 同義詞和反義詞
- Python - 文字翻譯
- Python - 替換單詞
- Python - 拼寫檢查
- Python - WordNet 介面
- Python - 語料庫訪問
- Python - 詞性標註
- Python - 組塊和組塊間隙
- Python - 組塊分類
- Python - 文字分類
- Python - 二元語法
- Python - 處理PDF
- Python - 處理Word文件
- Python - 讀取RSS feed
- Python - 情感分析
- Python - 搜尋和匹配
- Python - 文字處理
- Python - 文字換行
- Python - 頻率分佈
- Python - 文字摘要
- Python - 詞幹提取演算法
- Python - 受約束搜尋
Python - 分詞
在 Python 中,分詞基本上是指將較大的文字內容拆分成較小的行、單詞,甚至為非英語語言建立單詞。nltk 模組本身內建了各種分詞函式,可以在程式中使用,如下所示。
行分詞
在下面的示例中,我們使用 sent_tokenize 函式將給定的文字分成不同的行。
import nltk sentence_data = "The First sentence is about Python. The Second: about Django. You can learn Python,Django and Data Ananlysis here. " nltk_tokens = nltk.sent_tokenize(sentence_data) print (nltk_tokens)
執行上述程式後,我們將得到以下輸出:
['The First sentence is about Python.', 'The Second: about Django.', 'You can learn Python,Django and Data Ananlysis here.']
非英語分詞
在下面的示例中,我們對德語文字進行分詞。
import nltk
german_tokenizer = nltk.data.load('tokenizers/punkt/german.pickle')
german_tokens=german_tokenizer.tokenize('Wie geht es Ihnen? Gut, danke.')
print(german_tokens)
執行上述程式後,我們將得到以下輸出:
['Wie geht es Ihnen?', 'Gut, danke.']
單詞分詞
我們使用 nltk 中提供的 word_tokenize 函式對單詞進行分詞。
import nltk word_data = "It originated from the idea that there are readers who prefer learning new skills from the comforts of their drawing rooms" nltk_tokens = nltk.word_tokenize(word_data) print (nltk_tokens)
執行上述程式後,我們將得到以下輸出:
['It', 'originated', 'from', 'the', 'idea', 'that', 'there', 'are', 'readers', 'who', 'prefer', 'learning', 'new', 'skills', 'from', 'the', 'comforts', 'of', 'their', 'drawing', 'rooms']
廣告