Previous Up Next
Pierre L.
Pierre L.

II  Plugins

II.1  Technical note

II.1.1  The tagger

II.1.1.0  Introduction

“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.

II.1.1.1  Implementation

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. the construction of the database was complex and sometimes obscure to allow multiple tagging models. But ultimately the same database as in II. and II. 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 ai,j of transitioning from state i to state j. Say we observe outputs y1,…, yT. The most likely state sequence x1,…,xT 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 ) ·maxx ∈S ( a_x,k ·V_t-1,x) .

Here Vt,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 Vt,k if t > 1, or k if t=1. Then: x_T=argmaxx ∈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 := {o1,o2,…,oN}, the state space S := {s1,s2,…,sK}, a sequence of observations Y := {y1,y2,…,yT}, transition matrix A of size K × K such that Aij stores the transition probability of transiting from state si to state sj, emission matrix B of size K× N such that Bij stores the probability of observing oj from state si, an array of initial probabilities π of size K such that πi stores the probability that x1 == si. We saya path X := {x1,x2,…,xT} is a sequence of states that generate the observations Y = {y1,y2,…, yT}.

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

X :=     


with xj=si that generates Y={y1,y2,…, yj}. Each element of T2, T2[i,j], stores xj−1 of the most likely path so far

X     =     


for ∀ j, 2≤ jT.

We fill entries of two tables T1[i,j],T2[i,j] by increasing order of K· j+i. T_1[i,j]=maxk(T_1[k,j-1]·A_ki·B_iy_j)


The observation space O={o1,o2,…,oN}, the state space S={s1,s2,…,sK}, a sequence of observations Y={y1,y2,…, yT} such that yt==i if the observation at time t is oi, transition matrix A of size K · K such that Aij stores the transition probability of transiting from state si to state sj, emission matrix B of size K · N such that Bij stores the probability of observing oj from state si, an array of initial probabilities π of size K such that πi stores the probability that x1 == si.


The most likely hidden state sequence

X =       {x1,x2,…,xT} .
A01function VITERBI( O, S,π,Y,A,B ) : X
A02for each state s[i] do
A03T[1][i,1] <- π[i] · B[i][y1]
A04T[2][i,1] <- 0
A05end for
A06for i <- 2,3,...,T do
A07for each state s[j] do
A08T[1][j,i] <- maxk(T1[k,i−1]· Akj· Bjyi)
A09T[2][j,i] <- argmaxk(T1[k,i−1]· Akj· Bjyi)
A10end for
A11end for
A12z[T] <- argmaxk(T1[k,T])
A13x[T] <- s[z[T]]
A14for i <- T,T-1,...,2 do
A15z[i-1] <- T[2][z[i],i]
A16x[i-1] <- s[z[i-1]]
A17end for
A18return X
A19end 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,
    for y in V[0].keys(): print "%.5s: " % y,
    for t in range(len(V)): print "%.7s" % ("%f" % V[t][y]),
    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 :

poules- NFP -
du- PREPDU -
couvent- NMS V3P -
couvent- NMS V3P - .

The set of states would be

S := 


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

la papessa l'appeso il diavolo le stelle
Apache/2.4.41 (Unix) PHP/7.4.2 SVN/1.13.0 Hevea

Previous Up Next