波特干擾器

  1. 匯入 PorterStemmer 並初始化

     placeholderCopyfrom nltk.stem import PorterStemmer
     from nltk.tokenize import word_tokenize
     ps = PorterStemmer()
  2. 幹一個單詞列表

     placeholderCopyexample_words = ["python","pythoner","pythoning","pythoned","pythonly"]
    
     for w in example_words:
         print(ps.stem(w))

    結果:

     placeholderCopypython
     python
     python
     python
     pythonli
  3. 在對其進行標記後判斷一個句子。

     placeholderCopynew_text = "It is important to by very pythonly while you are pythoning with python. All pythoners have pythoned poorly at least once."
    
     word_tokens = word_tokenize(new_text)
     for w in word_tokens:
         print(ps.stem(w))   # Passing word tokens into stem method of Porter Stemmer

    結果:

     placeholderCopyIt
     is
     import
     to
     by
     veri
     pythonli
     while
     you
     are
     python
     with
     python
     .
     all
     python
     have
     python
     poorli
     at
     least
     onc
     .