官术网_书友最值得收藏!

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:

  1. 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()
  1. 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.
  1. 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.

主站蜘蛛池模板: 合作市| 呼伦贝尔市| 玉溪市| 乌海市| 寿宁县| 泰和县| 新丰县| 财经| 贵州省| 松桃| 耒阳市| 噶尔县| 金湖县| 财经| 沾益县| 崇礼县| 安新县| 固始县| 板桥市| 竹溪县| 大方县| 浦县| 盐池县| 收藏| 舞钢市| 文登市| 舞钢市| 伊金霍洛旗| 内黄县| 芷江| 宝坻区| 靖远县| 萨迦县| 铜梁县| 罗平县| 通许县| 吉安县| 老河口市| 奇台县| 调兵山市| 香河县|