【中文分词】二阶隐马尔可夫模型2-HMM


前一篇中介绍了用HMM做中文分词,对于未登录词(out-of-vocabulary, OOV)有良好的识别效果,但是缺点也十分明显——对于词典中的(in-vocabulary, IV)词却未能很好地识别。主要是因为,HMM本质上是一个Bigram的语法模型,未能深层次地考虑上下文(context)。对于此,本文将介绍更为复杂的二阶HMM以及开源实现。

1. 前言

n-gram语法模型

n-gram语法模型用来:在已知前面$n-1$个词$w_1, \cdots, w_$的情况下,预测下一个词出现的概率:

\[ P(w_n | w_1, \cdots, w_{n-1}) \]

常见的n-gram有Unigram(一元)、Bigram(二元)、Trigram(三元),分别表示当前词出现的概率为自身词频、只与前面一个词相关、只与前面两个词相关;对应的计算公式如下:

\begin \text \quad & \hat (w_3) = \frac{f(w_3)} \cr \text \quad & \hat (w_3|w_2) = \frac{f(w_2, w_3)}{f(w_2)} \cr \text \quad &\hat (w_3|w_1,w_2) = \frac{f(w_1, w_2, w_3)}{f(w_1,w_2)} \ \end

其中,$N$为语料中总词数,$f(w_i)$为词$w_i$在语料中出现的次数。

两种CWS模型

中文分词(Chinese word segmentation, CWS)的统计学习模型大致可以分为两类:Word-Based Generative ModelCharacter-Based Discriminative Model [3]. Word-Based Generative Model采用最大联合概率来对最佳分词方案建模,比如,对于句子$c_1^=c_1, \cdots, c_n$,最佳分词$w_1^m=w_1, \cdots, w_m$应满足:

\begin \arg \mathop{\max}\limits_{w_1m} P(w_1m) \end

此模型可以简化为二阶Markov链(second order Markov Chain)——当前词的转移概率只与前两个词相关,即为Trigram语法模型:

\begin P(w_1m) = \prod_P(w_i|w_1^) \approx \prod_P(w_i|w_) \end

Character-Based Discriminative Model采用类似与POS(Part-of-Speech)那一套序列标注的方法来进行分词:

\begin \arg \mathop{\max}\limits_{t_1n} P(t_1n | c_1^n) \label \end

$t_i$表示字符$c_i$对应的B/M/E/S词标注。

HMM分词

根据贝叶斯定理,式\eqref可改写为

\[ \begin{aligned} \arg \mathop{\max}\limits_{t_1^n} P(t_1^n | c_1^n) & = \arg \mathop{\max}\limits_{t_1^n} \frac{P(c_1^n | t_1^n) P(t_1^n)}{P(c_1^n)} \\ & = \arg \mathop{\max}\limits_{t_1^n} P(c_1^n | t_1^n) P(t_1^n)\\ \end{aligned} \]

HMM做了两个基本假设:齐次Markov性假设与观测独立性假设,即

  • 状态(标注)仅与前一状态相关;
\[ P(t_{i}|t_{1}^{i-1}) = P(t_i| t_{i-1}) \]
  • 观测相互独立,即字符相对独立:
\[ P(c_1^n|t_1^n) = \prod_{i=1}^{n} P(c_i|t_1^n) \]
  • 观测值依赖于该时刻的状态,即字符的出现仅依赖于标注:
\[ P(c_i|t_1^n) = P(c_i | t_i) \]

将上述三个等式代入下式:

\[ \begin{aligned} P(c_1^n | t_1^n) P(t_1^n) & = \prod_{i=1}^{n} P(c_i|t_1^n) \times [P(t_n|t_{1}^{n-1}) \cdots P(t_i|t_{1}^{i-1}) \cdots P(t_2|t_1)] \\ & = \prod_{i=1}^{n} [P(c_i|t_i) \times P(t_i|t_{i-1})]\\ \end{aligned} \]

因此,用HMM求解式子\eqref相当于

\begin \arg \mathop{\max}\limits_{t_1n} \prod_ [P(t_i|t_) \times P(c_i|t_i)] \end

二阶HMM的状态转移依赖于其前两个状态,类似地,分词模型如下:

\begin \arg \mathop{\max}\limits_{t_1n} \left[ \prod_ P(t_i|t_,t_) P(c_i|t_i) \right] \times P(t_{n+1}|t_n) \label \end

其中,$t_{-1},t_0,t_{n+1}$分别表示序列的开始标记与结束标记。

2. TnT

论文[2]基于二阶HMM提出TnT (Trigrams'n'Tags) 序列标注方案,对条件概率$P(t_3|t_2, t_1)$采取了如下平滑(smooth)处理:

\[ P(t_3|t_2, t_1)=\lambda_1 \hat{P}(t_3) + \lambda_2 \hat{P}(t_3|t_2) + \lambda_3 \hat{P}(t_3|t_2, t_1) \]

为了求解系数$\lambda$,TnT提出如下算法:

算法中,如果分母为0则置式子的结果为0.

3. Character-Based Generative Model

鉴于两种CWS模型的利弊:

  • Word-Based Generative Model高召回IV、低召回OOV;
  • Character-Based Discriminative Model高召回OOV,低召回IV

论文[3]结合两者提出了Character-Based Generative Model:

\[ \arg \mathop{\max}\limits_{t_1^n} P([c,t]_1^n)= \arg \mathop{\max}\limits_{t_1^n} \prod_{i=1}^n P([c,t]_i | [c,t]_{i-k}^{i-1}) \]

论文[3]中公式6的连乘下标k应为i,猜测应该是作者写错了。

4. 开源实现Snownlp

isnowfy大神在项目Snownlp实现TnT与Character-Based Discriminative Model;并且在博文中给出两者与最大匹配、Word-based Unigram模型的准确率比较,可以看出Generative Model的准确率较高。Snownlp的默认分词方案采用的是CharacterBasedGenerativeModel

from snownlp import SnowNLP

s = SnowNLP('小明硕士毕业于中国科学院计算所,后在日本京都大学深造')
print('/'.join(s.words))
# 小明/硕士/毕业/于/中国/科学院/计算/所/,/后/在/日本/京都/大学/深造
# Jieba HMM: 小明/硕士/毕业于/中国/科学院/计算/所/,/后/在/日/本京/都/大学/深造

通过分析TnTCharacterBasedGenerativeModel源码,发现作者在求解\eqref、Generative Model的最大值都是采用穷举法,导致了较低的分词效率。此外,HanLP的作者hankcs大神给出了TnT算法的Java实现

5. 参考资料

[1] Manning, Christopher D., and Hinrich Schütze. Foundations of statistical natural language processing. Vol. 999. Cambridge: MIT press, 1999. [2] Brants, Thorsten. "TnT: a statistical part-of-speech tagger." Proceedings of the sixth conference on Applied natural language processing. Association for Computational Linguistics, 2000. [3] Wang, Kun, Chengqing Zong, and Keh-Yih Su. "Which is More Suitable for Chinese Word Segmentation, the Generative Model or the Discriminative One?." PACLIC. 2009. [4] isnowfy, 几种中文分词算法的比较 [5] hankcs, 基于HMM2-Trigram字符序列标注的中文分词器Java实现.


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM