46

Using NLTK and WordNet, how do I convert simple tense verb into its present, past or past participle form?

For example:

I want to write a function which would give me verb in expected form as follows.

v = 'go'
present = present_tense(v)
print present # prints "going"

past = past_tense(v)
print past # prints "went"
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Software Enthusiastic
  • 25,147
  • 16
  • 58
  • 68

4 Answers4

27

With the help of NLTK this can also be done. It can give the base form of the verb. But not the exact tense, but it still can be useful. Try the following code.

from nltk.stem.wordnet import WordNetLemmatizer
words = ['gave','went','going','dating']
for word in words:
    print word+"-->"+WordNetLemmatizer().lemmatize(word,'v')

The output is:

gave-->give
went-->go
going-->go
dating-->date

Have a look at Stack Overflow question NLTK WordNet Lemmatizer: Shouldn't it lemmatize all inflections of a word?.

Community
  • 1
  • 1
Gunjan
  • 2,775
  • 27
  • 30
  • You should be carefull with words which second form is the same as other verb's infinitive. My last unlucky example is "fell". WordNetLemmatizer does not convert it into 'fall' because there is actually a verb 'to fell'. Another example is 'felt'. And there's no way to tell the lemmatizer that the verb is in it's second form. – Dany Mar 03 '16 at 13:25
  • 1
    The answer to my previous comment is to use "_morphy" method. It returns the list of possible base forms. If you know that the word you are trying to lemmatize is not in it's base form you can exclude it from the list you get from '_morphy' and pick one of the rest. – Dany Mar 03 '16 at 14:29
  • Worth noting that NLTK is under the GNU license and cannot be used for commercial purposes without a commercial license – Gibolt Jul 18 '19 at 03:11
  • This is not an answer to the posted question, but to the opposite question: the question was how to get the inflected forms from the base form, and this answer provides a way to get the base form from an inflected form. – Tom Feb 22 '21 at 11:25
23

I think what you're looking for is the NodeBox::Linguistics library. It does exactly that:

print en.verb.present("gave")
>>> give
msbmsb
  • 1,406
  • 9
  • 9
  • Very good, I think I am looking for this one only... Let me try it. – Software Enthusiastic Sep 22 '10 at 11:05
  • it seems it has some bugs in it. for example en.is_verb("download") return true, but en.verb.present("download") will report some error – camino Mar 13 '13 at 21:16
  • 1
    Yes, cannot import en in python 3 – Pyd Nov 01 '17 at 07:41
  • 4
    NodeBox has been replaced by pattern (https://www.clips.uantwerpen.be/pattern). Currently there is no official python 3 support for pattern, although it seems that you might be able to get it working by building the development branch (see: https://github.com/clips/pattern/issues/62) – Agargara Jan 16 '18 at 07:37
  • 2
    I've managed to get it working on Windows/Anaconda/Python 3.6, just had to run "pip install mysqlclient" first and "git clone https://github.com/clips/pattern cd pattern git fetch git checkout development python setup.py install" later. Excellent work Pattern team, will support you at a closest occasion. – Anatoly Alekseev Jan 17 '18 at 11:46
  • How to install it in python 2? – Nathan B Jan 21 '18 at 15:33
20

For Python3:

pip install pattern

then

from pattern.en import conjugate, lemma, lexeme, PRESENT, SG
print (lemma('gave'))
print (lexeme('gave'))
print (conjugate(verb='give',tense=PRESENT,number=SG)) # he / she / it

yields

give 
['give', 'gives', 'giving', 'gave', 'given'] 
gives

thnks to @Agargara for pointing & authors of Pattern for their beautiful work, go support them ;-)

PS. To use most of pattern's functionality in python 3.7+, you might want to use the trick described here

Pikamander2
  • 7,332
  • 3
  • 48
  • 69
Anatoly Alekseev
  • 2,011
  • 24
  • 27
  • 1
    I tried this one. But I got some errors. `Traceback (most recent call last): File "..\venv\lib\site-packages\pattern\text\__init__.py", line 609, in _read raise StopIteration StopIteration ` – Sashini Hettiarachchi Oct 02 '19 at 02:44
  • Sashini sorry, author has probably changed the package. "pip uninstall pattern" and then "pip install pattern -U" will do the trick. I have updated my answer. – Anatoly Alekseev Aug 01 '20 at 13:51
  • This is giving error, ERROR: Command errored out with exit status 1: 'c:\users\prabhat\appdata\local\programs\python\python39\python.exe' 'c:\users\prabhat\appdata\local\programs\python\python39\lib\site-packages\pip\_vendor\pep517\_in_process.py' prepare_metadata_for_build_wheel 'C:\Users\PRABHAT\AppData\Local\Temp\tmp3ucs_d6m' Check the logs for full command output. – Prabhat kumar Oct 08 '20 at 05:20
0

JWI (the WordNet library by MIT) also has a stemmer (WordNetStemmer) which converts different morphological forms of a word like ("written", "writes", "wrote") to their base form. It seems it works only for nouns (like plurals) and verbs though.

Word Stemming in Java with WordNet and JWNL also shows how to do this kind of stemming using JWNL, another Java-based Wordnet library:

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Hoda
  • 27
  • 1