Skip to content
DeepForgeby svx
All problems
MediumNLPnlp-210

G2P Rule Application

Problem

Apply ordered grapheme-to-phoneme rewrite rules to word. Each rule is [pattern, replacement] and is applied with str.replace in list order, so later rules see the output of earlier ones.

An empty rule list returns the word unchanged.

Hint

Rules compose in order, so an earlier replacement can feed a later pattern.

Test cases

case 1
in: ["cat",[["c","k"]]]
expected: "kat"
case 2
in: ["phone",[["ph","f"],["o","oh"]]]
expected: "fohne"
case 3
in: ["x",[]]
expected: "x"
case 4
in: ["aa",[["a","b"],["bb","c"]]]
expected: "c"
case 5
in: ["",[["a","b"]]]
expected: ""
Starter codePython
def g2p_rule(word, rules):
    # Your code here
    pass

5 test cases · run your code against them in the editor