Skip to content

Instantly share code, notes, and snippets.

@blumonkey
Created August 25, 2015 15:59
Show Gist options
  • 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')]
"""
@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