Skip to content

Instantly share code, notes, and snippets.

@blumonkey
Created August 25, 2015 15:59
Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save blumonkey/007955ec2f67119e0909 to your computer and use it in GitHub Desktop.
Save blumonkey/007955ec2f67119e0909 to your computer and use it in GitHub Desktop.
Python Code to train a Hidden Markov Model, using NLTK
__author__ = 'ssbushi'
# Import the toolkit and tags
import nltk
from nltk.corpus import treebank
# Train data - pretagged
train_data = treebank.tagged_sents()[:3000]
print train_data[0]
# Import HMM module
from nltk.tag import hmm
# Setup a trainer with default(None) values
# And train with the data
trainer = hmm.HiddenMarkovModelTrainer()
tagger = trainer.train_supervised(train_data)
print tagger
# Prints the basic data about the tagger
print tagger.tag("Today is a good day .".split())
print tagger.tag("Joe met Joanne in Delhi .".split())
print tagger.tag("Chicago is the birthplace of Ginny".split())
"""
Output in order (Notice some tags are wrong :/):
[('Today', u'NN'), ('is', u'VBZ'), ('a', u'DT'), ('good', u'JJ'), ('day', u'NN'), ('.', u'.')]
[('Joe', u'NNP'), ('met', u'VBD'), ('Joanne', u'NNP'), ('in', u'IN'), ('Delhi', u'NNP'), ('.', u'NNP')]
[('Chicago', u'NNP'), ('is', u'VBZ'), ('the', u'DT'), ('birthplace', u'NNP'), ('of', u'NNP'), ('Ginny', u'NNP')]
"""
@achmedzhanov
Copy link

achmedzhanov commented Jan 22, 2020

I've tried to run

tagger.evaluate(treebank.tagged_sents()[3000:])

result 0.36844377293330455

the code doesn't work properly for latest version of NLTK
you should create tagger so

tagger = nltk.HiddenMarkovModelTagger.train(train_data)

then

tagger.evaluate(treebank.tagged_sents()[3000:])

shows 0.8984243470753291

@krisGTech
Copy link

Yes, tagger = nltk.HiddenMarkovModelTagger.train(train_data) worked for me too.

@blumonkey
Copy link
Author

Thanks for your updates, @achmedzhanov and @krisGTech. This is a very old snippet obviously, but your comments might help others find an short and easy example to get started with HMMs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment