1

Silly, but I am somehow not able to access the stem functions in NLTK.

I call

import nltk
nltk.stem.porter.step1ab()

but I get told that object has no step1ab attribute. I guess I am somehow not calling it correctly, but I'm unsure how to actually get to it.

penguin
  • 856
  • 3
  • 14
  • 30

1 Answers1

8

step1ab() is a method of the class PorterStemmer within the nltk.stem.porter module. So you can call it like this:

myPorterStemmer = nltk.stem.porter.PorterStemmer()
...
myPorterStemmer.step1ab()

However, it's not really designed to be called directly. One would usually call myPorterStemmer.stem(word), which would then delegate to step1ab() to do part of the work.

If you really want to use step1ab in isolation though, you would have to set a bunch of variables and you'd get something like this:

>>> word = "countries"
>>> myStemmer = nltk.stem.porter.PorterStemmer()
>>> myStemmer.b = word
>>> myStemmer.k = len(word) - 1
>>> myStemmer.k0 = 0
>>> myStemmer.step1ab()
>>> myStemmer.b[myStemmer.k0:myStemmer.k+1]
'countri'
Junuxx
  • 14,011
  • 5
  • 41
  • 71
  • Thanks- worked a treat! One more thing if you happen to know the answer- when I then try to call `myPorterStemmer.step1ab()` I get a TypeError: `TypeError: step1ab() takes exactly 1 argument (2 given)`. So it's similar to [this](http://stackoverflow.com/questions/4909585/interesting-takes-exactly-1-argument-2-given-python-error) but I can't actually figure how to make it work. Sorry- still a python n00b! – penguin Mar 05 '13 at 09:51
  • @penguin: I updated the answer to show how you can call `step1ab`. However, I think you might actually just want to call `myStemmer.stem(word)` instead, as that is much easier and does all the steps in one go. – Junuxx Mar 05 '13 at 10:13