Skip to content

Latest commit

 

History

3 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Banner

Statistical NLP Toolkit

Classical probabilistic NLP models implemented from scratch — N-gram LM · HMM POS Tagger · Viterbi · Word Segmentation

Python Accuracy Dataset License


Overview

This project implements classical statistical NLP models from scratch, without relying on high-level NLP libraries. The goal is to build a concrete understanding of probabilistic sequence modeling at the algorithm level.

English | 中文


Models

Model Description Key technique
N-gram LM Character/word-level language model Add-k smoothing
HMM Tagger Part-of-speech tagging Bigram transitions + MLE emission
Viterbi Exact decoding for HMM Dynamic programming, log-space
Segmentation Chinese/unsegmented word segmentation DP with bigram cost

Result

Evaluated on Universal Dependencies English EWT:

Metric Score
POS Tagging Accuracy 88.26%

Installation

python3 -m venv venv
source venv/bin/activate
pip install -r requirements.txt
pip install -e .

Usage

Run the HMM POS tagger:

python examples/run_hmm.py

Project Structure

statistical-nlp/
├── statnlp/
│   ├── ngram.py               # N-gram language model with add-k smoothing
│   ├── hmm.py                 # HMM: transition + emission + Viterbi
│   ├── segmentation_fast.py   # DP word segmentation (optimised)
│   └── segmentation_iterative.py
├── examples/
│   └── run_hmm.py
├── requirements.txt
└── setup.py

Technical Details

HMM Architecture

Transition:  P(tag_t | tag_{t-1})   — bigram tag LM with add-k smoothing
Emission:    P(word | tag)           — MLE, log-space to prevent underflow
Decoding:    Viterbi algorithm       — DP over (time × tag) trellis

Viterbi Decoding

The decoder fills a (T × N_tags) trellis with log-probabilities and backtracks to recover the optimal tag sequence. Log-space computation prevents numerical underflow on long sequences.


Future Improvements

  • Unknown word handling (morphological features / suffix heuristics)
  • Trigram HMM for higher-order context
  • Confusion matrix analysis
  • Neural baseline comparison (BiLSTM-CRF)

中文说明

本项目从零实现了经典统计 NLP 模型,目标是深入理解概率序列建模原理,而不依赖现成框架。

已实现模型

模型 说明 核心技术
N-gram 语言模型 字/词级别语言模型 加性平滑(add-k)
HMM 词性标注器 序列标注 Bigram 转移 + MLE 发射
Viterbi 解码 HMM 精确解码 动态规划 + 对数空间
词语切分 基于 DP 的分词 Bigram 代价最小化

实验结果

在 Universal Dependencies English EWT 数据集上,词性标注准确率达到 88.26%

技术细节

  • 转移概率:基于 bigram 的标签语言模型,add-k 平滑
  • 发射概率:极大似然估计,对数空间计算防止数值下溢
  • Viterbi(时间步 × 标签数) 网格动态规划,回溯最优路径

可扩展方向

  • 未登录词处理(词缀特征等)
  • Trigram HMM
  • 混淆矩阵分析
  • 神经网络基线对比(BiLSTM-CRF)

About

No description, website, or topics provided.

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages