?>

“Les poules du couvent couvent” ! This french sentence is universally known by anyone who was once interested in TTS mechanisms. The problem is that the two last “couvent” at the end of the sentence are homographs but should not be pronounced the same way. The first one is the noun “couvent” and the second one the conjugation of the verb “couver” at third person of the plural.

Tagging might allow to disambiguate such situations. In fact tagging, that we may also call labeling consists in putting a label on each word, corresponding to its nature (or possibly to its function in the sentence). In fact “couvent” can receive the two labels “NMS” (nom mascualin singulier) “V3P” (verbe troisième personne pluriel.) Hence if the end of the sentence is tagged ( ( “couvent” “NMS” ) ( “couvent” “V3P” ) ) pronunciation will be correct.

The tagging process is achieved by a Viterbi algorithm (abbreviated in VA in the sequal.) (see the corresponding Wikipedia page for more explanation.

In fact there was already a tagger in the old FranFest
version ^{1}

The code mentioned above was directly derived from the
tagger implemnted in lliaphon ^{2}

The latter was itself derived from the lia_phon
code ^{3}

The aim of the algorithm is two determine, (guess, calculate,
compute) the most probable sequence of labels for a given
sequence of words i.e. a sentence. In VA language is the
observation (i.e. a data) and the label sequence the most
likely hidden sequence that leads to this observation. The
initial probabilities are given by a database
^{4}. In
II.1.1.1.3 the
construction of the database was complex and sometimes
obscure to allow multiple tagging models. But ultimately the
same database as in
II.1.1.1.1 and
II.1.1.1.2 was
used. This database contains a set of possible labels with
their frequences for a large amount of french words.

The transition probabilities are calculated via a trigram
model ^{5}.

Here are the formulas extracted from the
Wikipedia
article:
[Algorithm]
Suppose we are given a Hidden Markov Model (HMM) with state
space S, initial probabilities π_{i} of being in state
i and transition probabilities a_{i,j} of transitioning
from state i to state j. Say we observe outputs
y_{1},…, y_{T}. The most likely state sequence
x_{1},…,x_{T} that produces the observations is given by
the recurrence relations:
V_1,k=P( y_1 | k ) ·π_k

V_t,k=P( y_t | k ) ·max_{x ∈S} ( a_x,k ·V_t-1,x) .

Here V_{t,k} is the probability of the most probable
state sequence responsible for the first t observations
that has k as its final state. The Viterbi path can be
retrieved by saving back pointers that remember which state
x was used in the second equation. Let
Ptr(k,t) be the function that returns the value
of x used to compute V_{t,k} if t > 1, or k if
t=1. Then:
x_T=argmax_{x ∈S}(V_T,x)

x_t-1=Ptr(x_t,t) .

Here we’re using the standard definition of arg max. The
complexity of this algorithm is
O(T×|S|^{2}).
[Pseudocode]
Given the observation space O :=
{o_{1},o_{2},…,o_{N}}, the state space S :=
{s_{1},s_{2},…,s_{K}}, a sequence of observations Y
:= {y_{1},y_{2},…,y_{T}}, transition matrix A of
size K × K such that A_{ij} stores the transition
probability of transiting from state s_{i} to state s_{j},
emission matrix B of size K× N such that B_{ij}
stores the probability of observing o_{j} from state s_{i},
an array of initial probabilities π of size K such
that π_{i} stores the probability that x_{1} == s_{i}. We
saya path X := {x_{1},x_{2},…,x_{T}} is a sequence
of states that generate the observations Y =
{y_{1},y_{2},…, y_{T}}.

In this dynamic programming problem, we construct two
2-dimensional tables T_{1}, T_{2} of size K× T.
Each element of T_{1}, T_{1}[i,j], stores the probability
of the most likely path so far

X := | ⎧ ⎨ ⎩ | x_{1},x_{2}, ldots,x_{j} | ⎫ ⎬ ⎭ |

with
x_{j}=s_{i} that generates Y={y_{1},y_{2},…,
y_{j}}. Each element of T_{2}, T_{2}[i,j], stores
x_{j−1} of the most likely path so far

X = | ⎧ ⎨ ⎩ | x_{1},x_{2},…,x_{j−1},x_{j} | ⎫ ⎬ ⎭ |

for ∀ j, 2≤ j ≤ T.

We fill entries of two tables T_{1}[i,j],T_{2}[i,j] by
increasing order of K· j+i.
T_1[i,j]=max_{k}(T_1[k,j-1]·A_ki·B_iy_j)

T_2[i,j]=argmax_{k}(T_1[k,j-1]·A_ki·B_iy_j)

The observation space O={o_{1},o_{2},…,o_{N}}, the
state space S={s_{1},s_{2},…,s_{K}}, a sequence of
observations Y={y_{1},y_{2},…, y_{T}} such that
y_{t}==i if the observation at time t is o_{i},
transition matrix A of size K · K such that
A_{ij} stores the transition probability of transiting
from state s_{i} to state s_{j}, emission matrix B of
size K · N such that B_{ij} stores the
probability of observing o_{j} from state s_{i}, an array
of initial probabilities π of size K such that
π_{i} stores the probability that x_{1} == s_{i}.

The most likely hidden state sequence

X =
{x_{1},x_{2},…,x_{T}} . |

A_{01} | function VITERBI( O, S,π,Y,A,B ) : X |

A_{02} | for each state s[i] do |

A_{03} | T[1][i,1] <- π[i] · B[i][y_{1}] |

A_{04} | T[2][i,1] <- 0 |

A_{05} | end for |

A_{06} | for i <- 2,3,...,T do |

A_{07} | for each state s[j] do |

A_{08} | T[1][j,i] <- max_{k}(T_{1}[k,i−1]· A_{kj}·
B_{jyi}) |

A_{09} | T[2][j,i] <- argmax_{k}(T_{1}[k,i−1]·
A_{kj}· B_{jyi}) |

A_{10} | end for |

A_{11} | end for |

A_{12} | z[T] <- argmax_{k}(T_{1}[k,T]) |

A_{13} | x[T] <- s[z[T]] |

A_{14} | for i <- T,T-1,...,2 do |

A_{15} | z[i-1] <- T[2][z[i],i] |

A_{16} | x[i-1] <- s[z[i-1]] |

A_{17} | end for |

A_{18} | return X |

A_{19} | end function |

[Example:] Consider a primitive clinic in a village. People in the village have a very nice property that they are either healthy or have a fever. They can only tell if they have a fever by asking a doctor in the clinic. The wise doctor makes a diagnosis of fever by asking patients how they feel. Villagers only answer that they feel normal, dizzy, or cold.

Suppose a patient comes to the clinic each day and tells the doctor how she feels. The doctor believes that the health condition of this patient operates as a discrete Markov chain. There are two states, "Healthy" and "Fever", but the doctor cannot observe them directly, that is, they are hidden from him. On each day, there is a certain chance that the patient will tell the doctor he has one of the following feelings, depending on his health condition: "normal", "cold", or "dizzy". Those are the observations. The entire system is that of a hidden Markov model (HMM).

The doctor knows the villager’s general health condition, and what symptoms patients complain of with or without fever on average. In other words, the parameters of the HMM are known. They can be represented as follows in the Python programming language:

states = (’Healthy’, ’Fever’) observations = (’normal’, ’cold’, ’dizzy’) start_probability = {’Healthy’: 0.6, ’Fever’: 0.4} transition_probability = { ’Healthy’ : {’Healthy’: 0.7, ’Fever’: 0.3}, ’Fever’ : {’Healthy’: 0.4, ’Fever’: 0.6}, } emission_probability = { ’Healthy’ : {’normal’: 0.5, ’cold’: 0.4, ’dizzy’: 0.1}, ’Fever’ : {’normal’: 0.1, ’cold’: 0.3, ’dizzy’: 0.6}, }

In this piece of code, start_probability represents the doctor’s belief about which state the HMM is in when the patient first visits (all he knows is that the patient tends to be healthy). The particular probability distribution used here is not the equilibrium one, which is (given the transition probabilities) approximately ’Healthy’: 0.57, ’Fever’: 0.43. The transition_probability represents the change of the health condition in the underlying Markov chain. In this example, there is only a 30have a fever if he is healthy today. The emission_probability represents how likely the patient is to feel on each day. If he is healthy, there is a 50chance that he feels normal; if he has a fever, there is a 60feels dizzy.

Graphical representation of the given HMM

The patient visits three days in a row and the doctor discovers that on the first day he feels normal, on the second day he feels cold, on the third day he feels dizzy. The doctor has a question: what is the most likely sequence of health condition of the patient would explain these observations? This is answered by the Viterbi algorithm.

# Helps visualize the steps of Viterbi. def print_dptable(V): print " ", for i in range(len(V)): print "%7d" % i, print for y in V[0].keys(): print "%.5s: " % y, for t in range(len(V)): print "%.7s" % ("%f" % V[t][y]), print def viterbi(obs, states, start_p, trans_p, emit_p): V = [{}] path = {} # Initialize base cases (t == 0) for y in states: V[0][y] = start_p[y] * emit_p[y][obs[0]] path[y] = [y] # Run Viterbi for t > 0 for t in range(1,len(obs)): V.append({}) newpath = {} for y in states: (prob, state) = max([(V[t-1][y0] * trans_p[y0][y] * emit_p[y][obs[t]], y0) for y0 in states]) V[t][y] = prob newpath[y] = path[state] + [y] # Don’t need to remember the old paths path = newpath print_dptable(V) (prob, state) = max([(V[len(obs) - 1][y], y) for y in states]) return (prob, path[state])

The function viterbi takes the following arguments: obs is the sequence of observations, e.g. [’normal’, ’cold’, ’dizzy’]; states is the set of hidden states; start_p is the start probability; trans_p are the transition probabilities; and emit_p are the emission probabilities. For simplicity of code, we assume that the observation sequence obs is non-empty and that trans_p[i][j] and emit_p[i][j] is defined for all states i,j.

In the running example, the forward/Viterbi algorithm is used as follows:

def example(): return viterbi(observations, states, start_probability, transition_probability, emission_probability) print example()

This reveals that the observations [’normal’, ’cold’, ’dizzy’] were most likely generated by states [’Healthy’, ’Healthy’, ’Fever’]. In other words, given the observed activities, the patient was most likely to have been healthy both on the first day when he felt normal as well as on the second day when he felt cold, and then he contracted a fever the third day.

The operation of Viterbi’s algorithm can be visualized by means of a trellis diagram. The Viterbi path is essentially the shortest path through this trellis. [The “poules”] Let us came back to our famous sentence “Les poules du couvent couvenrt” (see II.1.1.0.) Notice that in the case of tagging, the set o of states is composed by subsets for each observation. Anyway we could consider a set composed by the union of all these subsets. But in this perspective, many emission probabilities would be null. More precisely here are the labels that might be affected to each word :

les | - DETFP DETMP PPOBJFP PPOBJMP - |

poules | - NFP - |

du | - PREPDU - |

couvent | - NMS V3P - |

couvent | - NMS V3P - . |

The set of states would be

S := | ⎧ ⎨ ⎩ | DETFP; DETMP; PPOBJFP; PPOBJMP; NFP; PREPDU; NMS; V3P | ⎫ ⎬ ⎭ |

even if word “du” for instance may only have label “PREPDU”. As far as I understand code in II.1.1.1.3, no optimization was done and there were big matrices with many null entries. In II.1.1.1.2 and II.1.1.1.1, a field in a structure kept track of label number.

Apache/2.4.41 (Unix) PHP/7.4.2 SVN/1.13.0 |