- Python Machine Learning By Example
- Yuxi (Hayden) Liu
- 216字
- 2021-07-02 12:41:40
Stemming and lemmatization
Word stemming is a process of reverting an inflected or derived word to its root form. For instance, machine is the stem of machines, and learning and learned are generated from learn as their stem.
The word lemmatization is a cautious version of stemming. It considers the PoS of a word when conducting stemming. We will discuss these two text preprocessing techniques, stemming and lemmatization, in further detail shortly. For now, let's take a quick look at how they're implemented respectively in NLTK by performing the following steps:
- Import porter as one of the three built-in stemming algorithms (LancasterStemmer and SnowballStemmer are the other two) and initialize the stemmer as follows:
>>> from nltk.stem.porter import PorterStemmer
>>> porter_stemmer = PorterStemmer()
- Stem machines and learning, as shown in the following codes:
>>> porter_stemmer.stem('machines')
'machin'
>>> porter_stemmer.stem('learning')
'learn'
Stemming sometimes involves chopping of letters if necessary, as we can see in machin in the preceding command output.
- Now import a lemmatization algorithm based on the built-in WordNet corpus and initialize a lemmatizer:
>>> from nltk.stem import WordNetLemmatizer
>>> lemmatizer = WordNetLemmatizer()
Similar to stemming, we lemmatize machines, learning:
>>> lemmatizer.lemmatize('machines')
'machine'
>>> lemmatizer.lemmatize('learning')
'learning'
Why is learning unchanged? It turns out that this algorithm only lemmatizes on nouns by default.
推薦閱讀
- Learning Microsoft Azure Storage
- 協作機器人技術及應用
- Cloud Analytics with Microsoft Azure
- 21天學通Java
- 大學計算機應用基礎
- Cloudera Administration Handbook
- 人工智能趣味入門:光環板程序設計
- Learning Azure Cosmos DB
- Windows Server 2003系統安全管理
- Visual C++項目開發案例精粹
- 統計挖掘與機器學習:大數據預測建模和分析技術(原書第3版)
- Photoshop CS5圖像處理入門、進階與提高
- Photoshop CS4數碼照片處理入門、進階與提高
- 樂高創意機器人教程(中級 上冊 10~16歲) (青少年iCAN+創新創意實踐指導叢書)
- 基于元胞自動機的人群疏散系統建模與分析