HMM and CRF for Sequence Labelling in NLP

Nlp tutorial · PySpark.in

HMM and CRF for Sequence Labelling in NLP 

Hidden Markov Model (HMM) and Conditional Random Field (CRF) are important classical machine learning algorithms used for sequence labeling tasks in Natural Language Processing (NLP). These algorithms are specifically designed to process sequential data where the prediction of one word depends on neighboring words in the sequence. 

Sequence labeling is one of the fundamental tasks in NLP and is widely applied in: 

  • Part-of-Speech (POS) Tagging  

  • Named Entity Recognition (NER)  

  • Chunking  

  • Information Extraction  

  • Speech Recognition  

Both HMM and CRF are extensively used before the rise of deep learning models and still remain important for understanding the foundations of NLP. 

What is Sequence Labeling? 

Sequence labeling is the process of assigning labels or tags to each element in a sequence of words. 

For example: 

Sentence 

Tags 

John lives in Delhi 

NNP VBZ IN NNP 

Another example in Named Entity Recognition: 

Word 

Entity Tag 

John 

PERSON 

Delhi 

LOCATION 

The objective is to predict labels for an entire sequence rather than for individual words independently. 

Hidden Markov Model (HMM) 

Hidden Markov Model is a probabilistic graphical model used for modeling sequential data. 

HMM assumes that: 

  1. The current state depends only on the previous state.  

  1. Each observed word depends only on the current hidden state.  

HMM is based on probability theory and Markov chains. 

Components of HMM 

An HMM consists of the following components. 

Component 

Description 

Hidden States 

Actual tags/states not directly visible 

Observations 

Words in the sentence 

Transition Probability 

Probability of moving from one state to another 

Emission Probability 

Probability of observing a word from a state 

Initial Probability 

Starting state probability 

 

 

Markov Assumption 

The Markov assumption states that: 

The current state depends only on the previous state. Mathematically: This simplifies sequence prediction problems. 

 

Transition Probability in HMM 

Transition probability represents the probability of moving from one state to another. 

Example: 

 

Probability that a noun appears after a determiner. 

Emission Probability in HMM 

Emission probability represents the probability of observing a word given a hidden state. 

Example: 

 

This means:Probability that the word “Delhi” is generated from the tag “Location”. 

 

Workflow of CRF in NLP 

Step 1: Text Collection 

Input sentence: 

"John works at Microsoft" 

Step 2: Feature Extraction 

Extract features such as: 

  • Current word  

  • Previous word  

  • Next word  

  • Capitalization  

  • Prefix/Suffix 

Step 3: Sequence Modeling 

CRF evaluates the complete sequence jointly. 

Step 4: Tag Prediction 

Example output: 

Word 

Tag 

John 

PERSON 

Microsoft 

ORGANIZATION 

 

Practical Implementation 

``` 

import sklearn_crfsuite 

from sklearn_crfsuite import metrics 

  

# Sample training data 

  

train_data = [ 

    ( 

        [('John', 'NNP'), ('works', 'VBZ'), ('at', 'IN'), ('Google', 'NNP')] 

    ) 

] 

  

# Feature extraction function 

  

def word2features(sentence, i): 

  

    word = sentence[i][0] 

  

    features = { 

        'word.lower()': word.lower(), 

        'is_upper': word.isupper(), 

        'is_title': word.istitle(), 

        'suffix': word[-3:] 

    } 

  

    return features 

  

# Prepare dataset 

  

X_train = [ 

    [word2features(s, i) for i in range(len(s))] 

    for s in train_data 

] 

  

y_train = [ 

    [label for token, label in s] 

    for s in train_data 

] 

  

# Create CRF model 

  

crf = sklearn_crfsuite.CRF() 

  

# Train model 

  

crf.fit(X_train, y_train) 

  

# Test sentence 

  

test_sentence = [('Microsoft', ''), ('hires', ''), ('engineers', '')] 

  

X_test = [[word2features(test_sentence, i) 

           for i in range(len(test_sentence))]] 

  

# Prediction 

  

prediction = crf.predict(X_test) 

  

print(prediction) 

``` 

 

 

 

Conclusion 

Hidden Markov Model (HMM) and Conditional Random Field (CRF) are foundational sequence labeling algorithms in Natural Language Processing. HMM provides a probabilistic framework for modeling sequential data, while CRF improves prediction quality by considering contextual dependencies and multiple features simultaneously. Although modern deep learning techniques have surpassed them in performance, HMM and CRF remain essential for understanding the evolution and principles of sequence modeling in NLP. 

 

 

More Nlp tutorials

All tutorials · Try the free PySpark compiler · Practice challenges