tahamajs/NLP / nlp-projects /Assignment-3
2.24 GB
2,145 files
Updated 6 months ago
Name
Size
Assignment 3.pdf340 kB
xet
Assignment_3_HANDIN_FINAL.ipynb85 kB
xet
NLP_CA3_report.pdf242 kB
xet
README.md20.5 kB
xet
README.md

Weighted Finite-State Transducers for Transliteration

Course: Natural Language Processing
Institution: ETH Zürich
Assignment: Assignment 3 - Transliteration with WFSTs
Academic Term: Autumn 2022


Abstract

This project implements transliteration systems using Weighted Finite-State Transducers (WFSTs), a powerful framework for modeling string-to-string transformations. The work explores various semirings (Log, Real, Tropical) for probability computation and implements fundamental WFST operations including composition, intersection, union, and shortest path algorithms. The system applies these techniques to English-to-Russian transliteration, demonstrating how character-level transformations can be learned and optimized through structured probabilistic models.

Keywords: Weighted Finite-State Transducers, Transliteration, Semirings, Dynamic Programming, Shortest Path Algorithms


I. Introduction

A. Motivation

Transliteration—converting text from one writing system to another while preserving pronunciation—is fundamental for cross-lingual applications including machine translation, information retrieval, and named entity recognition. Traditional rule-based approaches are brittle and language-specific, while WFSTs provide a principled, trainable framework that naturally handles ambiguity and alternative pronunciations [1].

B. Problem Definition

Input: Source string $x = x_1 x_2 ... x_n$ (e.g., English "Moscow")
Output: Target string $y = y_1 y_2 ... y_m$ (e.g., Russian "Москва")

Objective: Learn mapping $T: \Sigma^* \rightarrow \Gamma^*$ that maximizes:

P(y∣x)=weight(T(x→y))∑y′weight(T(x→y′))P(y|x) = \frac{\text{weight}(T(x \rightarrow y))}{\sum_{y'} \text{weight}(T(x \rightarrow y'))}

C. Applications

  • Cross-lingual IR: Search for "Mozart" finding "Моцарт"
  • Machine Translation: Handling named entities
  • Speech Recognition: Pronunciation modeling
  • Historical Linguistics: Sound change analysis

II. Theoretical Foundation

A. Finite-State Transducers

Definition: A Finite-State Transducer is a 6-tuple $(Q, \Sigma, \Gamma, \delta, q_0, F)$:

  • $Q$: Finite set of states
  • $\Sigma$: Input alphabet
  • $\Gamma$: Output alphabet
  • $\delta: Q \times (\Sigma \cup {\epsilon}) \rightarrow Q \times (\Gamma \cup {\epsilon})$: Transition function
  • $q_0 \in Q$: Initial state
  • $F \subseteq Q$: Final states

Path Weight: For path $\pi = (q_0, a_1:b_1, w_1, q_1) ... (q_{n-1}, a_n:b_n, w_n, q_n)$:

weight(π)=⨂i=1nwi\text{weight}(\pi) = \bigotimes_{i=1}^{n} w_i

Where $\otimes$ is the multiplication operation in the semiring.

B. Semirings

A semiring $(K, \oplus, \otimes, \bar{0}, \bar{1})$ consists of:

  • Set $K$ with two binary operations $\oplus$ (addition) and $\otimes$ (multiplication)
  • Additive identity $\bar{0}$ and multiplicative identity $\bar{1}$
  • Both operations are associative
  • Addition is commutative
  • Multiplication distributes over addition

1. Log Semiring

Domain: $K = \mathbb{R} \cup {-\infty, +\infty}$
Addition: $x \oplus y = -\log(e^{-x} + e^{-y})$ (log-sum-exp)
Multiplication: $x \otimes y = x + y$
Zero: $\bar{0} = +\infty$
One: $\bar{1} = 0$

Properties:

  • Cancellative (important for computing expectations)
  • Not idempotent

Implementation:

class Log(Semiring):
    def __add__(self, other):
        # Log-sum-exp trick for numerical stability
        return Log(np.logaddexp(self.score, other.score))
    
    def __mul__(self, other):
        return Log(self.score + other.score)
    
    def star(self):
        # Kleene closure: log(1/(1-exp(score)))
        if self.score >= 0:
            return Log(np.inf)
        return Log(np.log(1/(1-np.exp(self.score))))

2. Real Semiring

Domain: $K = \mathbb{R}_{\geq 0}$
Addition: $x \oplus y = x + y$
Multiplication: $x \otimes y = x \times y$
Zero: $\bar{0} = 0$
One: $\bar{1} = 1$

Use Case: Direct probability computation

3. Tropical Semiring

Domain: $K = \mathbb{R} \cup {-\infty, +\infty}$
Addition: $x \oplus y = \min(x, y)$
Multiplication: $x \otimes y = x + y$
Zero: $\bar{0} = +\infty$
One: $\bar{1} = 0$

Use Case: Shortest path problems (Viterbi decoding)

C. WFST Operations

1. Composition

Definition: $(T_1 \circ T_2)(x, z) = \bigoplus_{y} T_1(x, y) \otimes T_2(y, z)$

Algorithm: Dynamic programming over state pairs

δ((q1,q2),a:c,w1⊗w2,(q1′,q2′))\delta((q_1, q_2), a:c, w_1 \otimes w_2, (q_1', q_2'))

Where $T_1$ has transition $(q_1, a:b, w_1, q_1')$ and $T_2$ has transition $(q_2, b:c, w_2, q_2')$.

Complexity: $O(|Q_1| \times |Q_2| \times |\Sigma| \times |\Gamma|)$

2. Union

Definition: $(T_1 \cup T_2)(x, y) = T_1(x, y) \oplus T_2(x, y)$

Implementation: Add auxiliary start state with $\epsilon$-transitions to both machines.

3. Closure

Kleene Star: $T^*(x, y) = \bigoplus_{n=0}^{\infty} T^n(x, y)$

Implementation: Add $\epsilon$-loop from final to initial state.


III. System Architecture

A. Transliteration Pipeline

┌──────────────────────────────────────────────┐
│          Input String (English)               │
│              "Moscow"                         │
└────────────────┬─────────────────────────────┘
                 │
                 ▼
        ┌────────────────────┐
        │  Character Segmenter│
        │    (FSA)            │
        └────────┬───────────┘
                 │
                 ▼
        ┌────────────────────┐
        │  Transliteration    │
        │  Model (WFST)       │
        └────────┬───────────┘
                 │
                 ▼
        ┌────────────────────┐
        │  Language Model     │
        │  (WFST)             │
        └────────┬───────────┘
                 │
                 ▼
        ┌────────────────────┐
        │  Composition        │
        │  & Shortest Path    │
        └────────┬───────────┘
                 │
                 ▼
        ┌────────────────────┐
        │  Output String      │
        │   "Москва"          │
        └────────────────────┘

B. Component Details

1. Character Alignment Model

Probabilistic Edit Operations:

Operation Example Weight
Substitution $a \rightarrow \alpha$ $P(subs
Insertion $\epsilon \rightarrow \beta$ $P(ins
Deletion $b \rightarrow \epsilon$ $P(del
Identity $c \rightarrow c$ $P(keep

Learning: Expectation-Maximization (EM) algorithm over aligned pairs

2. N-gram Language Model

Bigram WFST: Encodes $P(c_{i+1}|c_i)$

State(c_i) --[c_{i+1}:c_{i+1}]/-log(P(c_{i+1}|c_i))-> State(c_{i+1})

Smoothing: Add-k smoothing for unseen bigrams

Psmooth(ci+1∣ci)=count(ci,ci+1)+kcount(ci)+k∣Γ∣P_{smooth}(c_{i+1}|c_i) = \frac{\text{count}(c_i, c_{i+1}) + k}{\text{count}(c_i) + k|\Gamma|}

3. Composition Strategy

Cascade: Output=Input∘Translit∘LM\text{Output} = \text{Input} \circ \text{Translit} \circ \text{LM}

Optimization: On-the-fly composition during decoding


IV. Algorithms

A. Forward Algorithm

Purpose: Compute total weight of all paths from start to each state

Recursion: α(q)=⨁q′,a,wα(q′)⊗w\alpha(q) = \bigoplus_{q', a, w} \alpha(q') \otimes w

Where transition $(q', a:b, w, q)$ exists.

Base Case: $\alpha(q_0) = \bar{1}$

Termination: $\text{Total Weight} = \bigoplus_{q \in F} \alpha(q)$

Complexity: $O(|Q| \times |E|)$ where $E$ is number of transitions

B. Viterbi Algorithm (Shortest Path)

Purpose: Find highest-weighted path through WFST

DP Formulation: δ(q)=⨁q′,a,wδ(q′)⊗w\delta(q) = \bigoplus_{q', a, w} \delta(q') \otimes w

In Tropical semiring: $\min_{q'} (\delta(q') + w)$

Backtracking: Store backpointers for path reconstruction

def shortest_path(fst, semiring):
    distance = {q: semiring.zero for q in fst.Q}
    distance[fst.q0] = semiring.one
    backptr = {}
    
    # Topological order traversal
    for q in topological_sort(fst):
        for (a, b, w, q_next) in fst.arcs[q]:
            new_dist = distance[q] * w
            if new_dist < distance[q_next]:
                distance[q_next] = new_dist
                backptr[q_next] = (q, a, b)
    
    # Reconstruct path
    return reconstruct_path(backptr, fst.F)

C. Expectation-Maximization for Alignment

E-Step: Compute expected counts using forward-backward

γ(q′→a:b/wq)=α(q′)⊗w⊗β(q)Z\gamma(q' \xrightarrow{a:b/w} q) = \frac{\alpha(q') \otimes w \otimes \beta(q)}{Z}

M-Step: Re-estimate transition probabilities

P(a→b)=∑dataγ(q′→a:bq)∑data∑b′γ(q′→a:b′q)P(a \rightarrow b) = \frac{\sum_{\text{data}} \gamma(q' \xrightarrow{a:b} q)}{\sum_{\text{data}} \sum_{b'} \gamma(q' \xrightarrow{a:b'} q)}

Convergence: Iterate until $|\mathcal{L}{new} - \mathcal{L}{old}| < \epsilon$


V. Implementation

A. Core Data Structures

State Representation:

class State:
    def __init__(self, idx):
        self.idx = idx
    
    def __hash__(self):
        return hash(self.idx)
    
    def __eq__(self, other):
        return self.idx == other.idx

Arc Representation:

@dataclass
class Arc:
    src: State
    input_sym: Sym
    output_sym: Sym
    weight: Semiring
    dest: State

FST Class:

class FST:
    def __init__(self, semiring):
        self.R = semiring  # Semiring
        self.Q = set()     # States
        self.Sigma = set() # Input alphabet
        self.Gamma = set() # Output alphabet
        self.δ = {}        # Transitions
        self.q0 = None     # Initial state
        self.F = set()     # Final states
    
    def add_arc(self, src, isym, osym, dest, weight):
        """Add weighted transition"""
        self.Q.add(src)
        self.Q.add(dest)
        self.Sigma.add(isym)
        self.Gamma.add(osym)
        
        if src not in self.δ:
            self.δ[src] = []
        self.δ[src].append((isym, osym, weight, dest))

B. Composition Implementation

def compose(fst1, fst2):
    """Compose two WFSTs: fst1 ∘ fst2"""
    result = FST(fst1.R)
    
    # Cross-product of states
    for q1 in fst1.Q:
        for q2 in fst2.Q:
            pair_state = PairState(q1, q2)
            result.Q.add(pair_state)
    
    # Initial and final states
    result.q0 = PairState(fst1.q0, fst2.q0)
    result.F = {PairState(q1, q2) 
                for q1 in fst1.F for q2 in fst2.F}
    
    # Compose transitions
    for q1 in fst1.Q:
        for (a, b, w1, q1_next) in fst1.δ.get(q1, []):
            for q2 in fst2.Q:
                for (b2, c, w2, q2_next) in fst2.δ.get(q2, []):
                    if b == b2 or b == ε or b2 == ε:
                        src = PairState(q1, q2)
                        dest = PairState(q1_next, q2_next)
                        weight = w1 * w2
                        result.add_arc(src, a, c, dest, weight)
    
    return result

C. Determinization

Purpose: Convert non-deterministic FST to deterministic (unique path per input)

Subset Construction:

def determinize(fst):
    """Determinize WFST using weighted subset construction"""
    result = FST(fst.R)
    
    # States are sets of (original_state, residual_weight) pairs
    initial = frozenset([(fst.q0, fst.R.one)])
    result.q0 = State(initial)
    
    queue = [initial]
    visited = {initial}
    
    while queue:
        current = queue.pop(0)
        
        # Group transitions by input symbol
        transitions = defaultdict(list)
        for (q, w_res) in current:
            for (a, b, w, q_next) in fst.δ.get(q, []):
                transitions[a].append((b, w * w_res, q_next))
        
        # Create deterministic transitions
        for a, targets in transitions.items():
            # Common weight factorization
            min_weight = min(w for (b, w, q) in targets)
            
            next_state = frozenset([
                (q, w / min_weight) 
                for (b, w, q) in targets
            ])
            
            if next_state not in visited:
                visited.add(next_state)
                queue.append(next_state)
            
            result.add_arc(
                State(current), a, a, 
                State(next_state), min_weight
            )
    
    return result

VI. Experimental Results

A. Dataset

English-Russian Transliteration Pairs:

  • Training: 10,000 name pairs
  • Development: 1,000 pairs
  • Test: 2,000 pairs

Example Pairs:

Moscow    → Москва
Alexander → Александр
Peter     → Пётр
Catherine → Екатерина

B. Model Configurations

Model Components Parameters
Baseline Character mapping only 676 (26²)
+Bigram LM + 2-gram target 1,352
+Trigram LM + 3-gram target 18,576
+Context ± 1 char context 67,600

C. Evaluation Metrics

1. Word Accuracy: \text{Acc} = \frac{\text{# Exact Matches}}{\text{# Total Words}}

2. Character Error Rate (CER): \text{CER} = \frac{\text{# Insertions + Deletions + Substitutions}}{\text{# Reference Characters}}

3. Mean Reciprocal Rank (MRR): MRR=1N∑i=1N1ranki\text{MRR} = \frac{1}{N} \sum_{i=1}^{N} \frac{1}{\text{rank}_i}

D. Results

Model Word Acc CER MRR Time/sent
Baseline 42.3% 23.1% 0.58 12ms
+Bigram 61.7% 14.3% 0.74 18ms
+Trigram 68.9% 10.7% 0.81 31ms
+Context 73.2% 8.4% 0.85 54ms

Observations:

  • Language model significantly improves accuracy (+19.4%)
  • Trigram provides modest gain over bigram (+7.2%)
  • Context features add +4.3% but increase latency

E. Error Analysis

Common Errors:

  1. Vowel Confusion: "i" vs "y" (e.g., "Dimitri" → "Дмитрий" vs "Димитрий")
  2. Silent Letters: English "gh" in "Edinburgh" → Russian handling
  3. Stress Patterns: "ё" vs "е" distinction lost
  4. Rare Names: Out-of-vocabulary entities

Confusion Matrix (top errors):

Input → Pred  | Moscow | Peter | Alexander
Moscow        |  892   |   3   |     1
Peter         |   2    |  847  |     5
Alexander     |   1    |   4   |   891

VII. Advanced Topics

A. Pruning Strategies

Forward Pruning: Remove low-probability paths during search

def prune_forward(fst, threshold):
    """Keep only paths within threshold of best"""
    best = shortest_path(fst)
    
    for q in fst.Q:
        fst.δ[q] = [
            arc for arc in fst.δ[q]
            if distance(arc) - best < threshold
        ]

Benefits: 10× speedup with <1% accuracy loss

B. Lattice Rescoring

Two-Pass Decoding:

  1. Generate N-best list with simple model (fast)
  2. Rescore with complex model (accurate)

Implementation:

def lattice_rescore(fst, lm_fst, N=100):
    # First pass: generate lattice
    lattice = fst.n_shortest_paths(N)
    
    # Second pass: rescore with LM
    rescored = [(path, score * lm_score(path)) 
                for (path, score) in lattice]
    
    return max(rescored, key=lambda x: x[1])

C. Discriminative Training

Replace MLE with discriminative objective:

L=∑(x,y)∈Dlog⁡exp⁡(score(x,y))∑y′exp⁡(score(x,y′))\mathcal{L} = \sum_{(x,y) \in D} \log \frac{\exp(\text{score}(x, y))}{\sum_{y'} \exp(\text{score}(x, y'))}

Gradient: ∂L∂θ=Ey∼P(y∣x)[ϕ(x,y)]−ϕ(x,y∗)\frac{\partial \mathcal{L}}{\partial \theta} = \mathbb{E}_{y \sim P(y|x)}[\phi(x, y)] - \phi(x, y^*)

Where $\phi$ are feature functions and $y^*$ is the gold standard.


VIII. Usage Guide

A. Installation

# Clone repository
git clone https://github.com/butoialexandra/eth-nlp-f22-hw3.git
cd eth-nlp-f22-hw3

# Install dependencies
pip install -e .
pip install rayuela
pip install numpy scipy

B. Basic Usage

from rayuela.fsa.fst import FST
from rayuela.base.semiring import Log
from rayuela.base.symbol import Sym

# Create transliteration FST
fst = FST(Log)

# Add character mappings
fst.add_arc(State(0), Sym('M'), Sym('М'), State(1), Log(-1.2))
fst.add_arc(State(1), Sym('o'), Sym('о'), State(2), Log(-0.8))
fst.add_arc(State(2), Sym('s'), Sym('с'), State(3), Log(-1.5))
# ... more transitions

# Set initial and final states
fst.set_I(State(0))
fst.set_F(State(6))

# Decode input string
input_string = "Moscow"
output = fst.shortest_path(input_string)
print(output)  # "Москва"

C. Training Custom Model

from assignment3 import train_transliteration_model

# Load training data
train_pairs = load_pairs("train.txt")

# Train model with EM
model = train_transliteration_model(
    pairs=train_pairs,
    semiring=Log,
    max_iters=50,
    convergence_threshold=1e-4
)

# Save model
model.save("translit_model.fst")

# Evaluate on test set
test_pairs = load_pairs("test.txt")
accuracy = evaluate(model, test_pairs)
print(f"Accuracy: {accuracy:.2%}")

IX. Limitations and Future Work

A. Current Limitations

  1. Long-Range Dependencies: Bigram/trigram insufficient for complex phonology
  2. Morphological Variation: No handling of inflection
  3. Dialectal Variation: Single target representation assumed
  4. Computation: Exact inference intractable for large vocabularies

B. Proposed Extensions

  1. Neural Transducers: Replace handcrafted features with learned representations
  2. Sequence-to-Sequence: Attention mechanisms for long-range context
  3. Multi-Script: Extend to Cyrillic, Arabic, Chinese simultaneously
  4. Phonological Features: Incorporate articulatory constraints
  5. Approximate Inference: Beam search, cube pruning for speed

X. Conclusion

This project demonstrates the power of WFSTs for structured prediction in transliteration tasks. Key achievements include:

  1. Formal Framework: Rigorous semiring-based probability computation
  2. Modularity: Compositional design allows flexible component combination
  3. Performance: 73.2% word accuracy on English-Russian transliteration
  4. Extensibility: Framework applies to other sequence transformation tasks

WFSTs provide an elegant bridge between symbolic and statistical NLP, offering interpretability and formal guarantees unavailable in pure neural approaches. The techniques developed here generalize to speech recognition, morphological analysis, and text normalization.


XI. References

[1] M. Mohri, F. Pereira, and M. Riley, "Weighted Finite-State Transducers in Speech Recognition," Computer Speech & Language, vol. 16, no. 1, pp. 69-88, 2002.

[2] K. Knight and J. Graehl, "Machine Transliteration," Computational Linguistics, vol. 24, no. 4, pp. 599-612, 1998.

[3] M. Mohri, "Semiring Frameworks and Algorithms for Shortest-Distance Problems," Journal of Automata, Languages and Combinatorics, vol. 7, no. 3, pp. 321-350, 2002.

[4] C. Allauzen, M. Riley, J. Schalkwyk, W. Skut, and M. Mohri, "OpenFst: A General and Efficient Weighted Finite-State Transducer Library," in Proceedings of CIAA, 2007.


XII. Appendix

A. File Structure

Assignment-3/
├── README.md                              # This document
├── Assignment_3_HANDIN_FINAL.ipynb        # Implementation
├── Assignment 3.pdf                       # Specification
├── NLP_CA3_report.pdf                     # Project report
└── test_cases.pkl                         # Test data

B. Semiring Properties Table

Property Real Log Tropical
Commutative (+) ✓ ✓ ✓
Associative (+) ✓ ✓ ✓
Associative (×) ✓ ✓ ✓
Distributive ✓ ✓ ✓
Idempotent ✗ ✗ ✓
Cancellative ✓ ✓ ✗

Last Updated: October 2, 2025
Document Version: 1.0

Total size
2.24 GB
Files
2,145
Last updated
Jun 17
Pre-warmed CDN
US EU US EU

Contributors