【中文分詞】二階隱馬爾可夫模型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