File size: 6,351 Bytes
3483aba
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
"""Which letters spell which phoneme.

Every phoneme of a pronunciation is given a run of zero to four letters of the
word, in order, so that the runs spell the word: "thought" TH AO T becomes
th|ough|t and "box" B AA K S becomes b|o|x|(nothing). The pronunciation rules
need this to tell an unstressed vowel written "i" from one written "a".

The table of how likely each (phoneme, letters) pair is comes from CMUdict
alone, by expectation-maximisation over all its entries (``train``). The same
table aligns dictionary entries when the lexicon is built and the output of the
unknown-word model at run time (``Aligner.align``).
"""

from collections import defaultdict
import math

MAX_RUN = 4
_LENGTH_PRIOR = (0.02, 1.0, 0.3, 0.05, 0.01)      # starting belief by number of letters
_UNSEEN = -14.0


def base(symbol):
    """ARPABET symbol without its stress digit."""
    return symbol.rstrip("012")


def _expected_counts(word, phones, probability):
    letters, count = len(word), len(phones)
    forward = [[0.0] * (letters + 1) for _ in range(count + 1)]
    forward[0][0] = 1.0
    for j in range(1, count + 1):
        phone, previous, row = phones[j - 1], forward[j - 1], forward[j]
        for i in range(letters + 1):
            total = 0.0
            for k in range(min(MAX_RUN, i) + 1):
                if previous[i - k]:
                    total += previous[i - k] * probability(phone, word[i - k:i])
            row[i] = total
    evidence = forward[count][letters]
    if evidence <= 0.0:
        return None
    backward = [[0.0] * (letters + 1) for _ in range(count + 1)]
    backward[count][letters] = 1.0
    for j in range(count - 1, -1, -1):
        phone, following, row = phones[j], backward[j + 1], backward[j]
        for i in range(letters + 1):
            total = 0.0
            for k in range(min(MAX_RUN, letters - i) + 1):
                if following[i + k]:
                    total += following[i + k] * probability(phone, word[i:i + k])
            row[i] = total
    counts = {}
    for j in range(count):
        phone = phones[j]
        for i in range(letters + 1):
            if not forward[j][i]:
                continue
            for k in range(min(MAX_RUN, letters - i) + 1):
                if backward[j + 1][i + k]:
                    run = word[i:i + k]
                    weight = forward[j][i] * probability(phone, run) * backward[j + 1][i + k] / evidence
                    if weight > 1e-12:
                        counts[(phone, run)] = counts.get((phone, run), 0.0) + weight
    return counts


def _expectation(job):
    entries, table = job
    if table is None:
        probability = lambda phone, run: _LENGTH_PRIOR[len(run)]
    else:
        probability = lambda phone, run: table.get((phone, run), 0.0) + 1e-9 * _LENGTH_PRIOR[len(run)]
    totals, failed = defaultdict(float), 0
    for word, phones in entries:
        counts = _expected_counts(word, phones, probability)
        if counts is None:
            failed += 1
            continue
        for key, value in counts.items():
            totals[key] += value
    return dict(totals), failed


def train(entries, iterations=8, workers=1, log=None):
    """Estimate P(letters | phoneme) from ``(word, [phoneme, ...])`` pairs
    (phonemes without stress). Returns ``{phoneme: {letters: log probability}}``."""
    entries = [(word, [base(p) for p in phones]) for word, phones in entries
               if phones and len(word) <= MAX_RUN * len(phones)]
    table = None
    pool = None
    if workers > 1:
        import multiprocessing
        pool = multiprocessing.get_context("fork").Pool(workers)
    try:
        for iteration in range(iterations):
            shards = [entries[i::workers] for i in range(workers)]
            jobs = [(shard, table) for shard in shards]
            results = pool.map(_expectation, jobs) if pool else [_expectation(job) for job in jobs]
            totals, failed = defaultdict(float), 0
            for counts, misses in results:
                failed += misses
                for key, value in counts.items():
                    totals[key] += value
            mass = defaultdict(float)
            for (phone, _), value in totals.items():
                mass[phone] += value
            table = {key: value / mass[key[0]] for key, value in totals.items()}
            if log:
                log(f"alignment iteration {iteration + 1}/{iterations}: {len(table)} pairs, {failed} entries without a path")
    finally:
        if pool:
            pool.close()
    nested = defaultdict(dict)
    for (phone, run), value in table.items():
        if value >= 1e-6:
            nested[phone][run] = round(math.log(value), 3)
    return {phone: dict(sorted(runs.items())) for phone, runs in sorted(nested.items())}


class Aligner:
    def __init__(self, table):
        self.table = table

    def score(self, phone, run):
        known = self.table.get(phone)
        value = known.get(run) if known else None
        return value if value is not None else _UNSEEN - 2.0 * abs(len(run) - 1)

    def align(self, word, phones):
        """Split ``word`` into one run of letters per phoneme. ``phones`` may
        carry stress digits. Returns ``None`` when the word is too long for the
        pronunciation (an abbreviation such as "dept")."""
        phones = [base(p) for p in phones]
        letters, count = len(word), len(phones)
        if not count or letters > MAX_RUN * count:
            return None
        low = -1e18
        best = [[low] * (letters + 1) for _ in range(count + 1)]
        step = [[0] * (letters + 1) for _ in range(count + 1)]
        best[0][0] = 0.0
        for j in range(1, count + 1):
            phone, previous, row, trace = phones[j - 1], best[j - 1], best[j], step[j]
            for i in range(letters + 1):
                for k in range(min(MAX_RUN, i) + 1):
                    if previous[i - k] > low:
                        value = previous[i - k] + self.score(phone, word[i - k:i])
                        if value > row[i]:
                            row[i], trace[i] = value, k
        if best[count][letters] <= low:
            return None
        runs, i = [], letters
        for j in range(count, 0, -1):
            k = step[j][i]
            runs.append(word[i - k:i])
            i -= k
        return runs[::-1]