You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
132 lines
5.5 KiB
132 lines
5.5 KiB
4 years ago
|
# Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved.
|
||
|
#
|
||
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
||
|
# you may not use this file except in compliance with the License.
|
||
|
# You may obtain a copy of the License at
|
||
|
#
|
||
|
# http://www.apache.org/licenses/LICENSE-2.0
|
||
|
#
|
||
|
# Unless required by applicable law or agreed to in writing, software
|
||
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
||
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||
|
# See the License for the specific language governing permissions and
|
||
|
# limitations under the License.
|
||
8 years ago
|
"""Wrapper for various CTC decoders in SWIG."""
|
||
7 years ago
|
import swig_decoders
|
||
|
|
||
|
|
||
|
class Scorer(swig_decoders.Scorer):
|
||
|
"""Wrapper for Scorer.
|
||
|
|
||
|
:param alpha: Parameter associated with language model. Don't use
|
||
|
language model when alpha = 0.
|
||
|
:type alpha: float
|
||
|
:param beta: Parameter associated with word count. Don't use word
|
||
7 years ago
|
count when beta = 0.
|
||
7 years ago
|
:type beta: float
|
||
|
:model_path: Path to load language model.
|
||
4 years ago
|
:type model_path: str
|
||
7 years ago
|
"""
|
||
|
|
||
7 years ago
|
def __init__(self, alpha, beta, model_path, vocabulary):
|
||
|
swig_decoders.Scorer.__init__(self, alpha, beta, model_path, vocabulary)
|
||
8 years ago
|
|
||
|
|
||
7 years ago
|
def ctc_greedy_decoder(probs_seq, vocabulary):
|
||
8 years ago
|
"""Wrapper for ctc best path decoder in swig.
|
||
|
|
||
|
:param probs_seq: 2-D list of probability distributions over each time
|
||
|
step, with each element being a list of normalized
|
||
|
probabilities over vocabulary and blank.
|
||
|
:type probs_seq: 2-D list
|
||
|
:param vocabulary: Vocabulary list.
|
||
|
:type vocabulary: list
|
||
|
:return: Decoding result string.
|
||
4 years ago
|
:rtype: str
|
||
8 years ago
|
"""
|
||
7 years ago
|
result = swig_decoders.ctc_greedy_decoder(probs_seq.tolist(), vocabulary)
|
||
4 years ago
|
return result
|
||
8 years ago
|
|
||
|
|
||
7 years ago
|
def ctc_beam_search_decoder(probs_seq,
|
||
|
vocabulary,
|
||
7 years ago
|
beam_size,
|
||
7 years ago
|
cutoff_prob=1.0,
|
||
7 years ago
|
cutoff_top_n=40,
|
||
7 years ago
|
ext_scoring_func=None):
|
||
|
"""Wrapper for the CTC Beam Search Decoder.
|
||
8 years ago
|
|
||
|
:param probs_seq: 2-D list of probability distributions over each time
|
||
|
step, with each element being a list of normalized
|
||
|
probabilities over vocabulary and blank.
|
||
|
:type probs_seq: 2-D list
|
||
|
:param vocabulary: Vocabulary list.
|
||
|
:type vocabulary: list
|
||
7 years ago
|
:param beam_size: Width for beam search.
|
||
|
:type beam_size: int
|
||
8 years ago
|
:param cutoff_prob: Cutoff probability in pruning,
|
||
|
default 1.0, no pruning.
|
||
|
:type cutoff_prob: float
|
||
7 years ago
|
:param cutoff_top_n: Cutoff number in pruning, only top cutoff_top_n
|
||
7 years ago
|
characters with highest probs in vocabulary will be
|
||
|
used in beam search, default 40.
|
||
7 years ago
|
:type cutoff_top_n: int
|
||
8 years ago
|
:param ext_scoring_func: External scoring function for
|
||
7 years ago
|
partially decoded sentence, e.g. word count
|
||
|
or language model.
|
||
8 years ago
|
:type external_scoring_func: callable
|
||
|
:return: List of tuples of log probability and sentence as decoding
|
||
|
results, in descending order of the probability.
|
||
|
:rtype: list
|
||
|
"""
|
||
7 years ago
|
beam_results = swig_decoders.ctc_beam_search_decoder(
|
||
|
probs_seq.tolist(), vocabulary, beam_size, cutoff_prob, cutoff_top_n,
|
||
|
ext_scoring_func)
|
||
|
beam_results = [(res[0], res[1].decode('utf-8')) for res in beam_results]
|
||
|
return beam_results
|
||
8 years ago
|
|
||
|
|
||
|
def ctc_beam_search_decoder_batch(probs_split,
|
||
|
vocabulary,
|
||
7 years ago
|
beam_size,
|
||
8 years ago
|
num_processes,
|
||
|
cutoff_prob=1.0,
|
||
7 years ago
|
cutoff_top_n=40,
|
||
8 years ago
|
ext_scoring_func=None):
|
||
7 years ago
|
"""Wrapper for the batched CTC beam search decoder.
|
||
8 years ago
|
|
||
7 years ago
|
:param probs_seq: 3-D list with each element as an instance of 2-D list
|
||
|
of probabilities used by ctc_beam_search_decoder().
|
||
|
:type probs_seq: 3-D list
|
||
|
:param vocabulary: Vocabulary list.
|
||
|
:type vocabulary: list
|
||
7 years ago
|
:param beam_size: Width for beam search.
|
||
|
:type beam_size: int
|
||
7 years ago
|
:param num_processes: Number of parallel processes.
|
||
|
:type num_processes: int
|
||
7 years ago
|
:param cutoff_prob: Cutoff probability in vocabulary pruning,
|
||
7 years ago
|
default 1.0, no pruning.
|
||
7 years ago
|
:type cutoff_prob: float
|
||
|
:param cutoff_top_n: Cutoff number in pruning, only top cutoff_top_n
|
||
7 years ago
|
characters with highest probs in vocabulary will be
|
||
|
used in beam search, default 40.
|
||
7 years ago
|
:type cutoff_top_n: int
|
||
7 years ago
|
:param num_processes: Number of parallel processes.
|
||
|
:type num_processes: int
|
||
|
:param ext_scoring_func: External scoring function for
|
||
7 years ago
|
partially decoded sentence, e.g. word count
|
||
|
or language model.
|
||
7 years ago
|
:type external_scoring_function: callable
|
||
|
:return: List of tuples of log probability and sentence as decoding
|
||
|
results, in descending order of the probability.
|
||
|
:rtype: list
|
||
|
"""
|
||
|
probs_split = [probs_seq.tolist() for probs_seq in probs_split]
|
||
8 years ago
|
|
||
7 years ago
|
batch_beam_results = swig_decoders.ctc_beam_search_decoder_batch(
|
||
7 years ago
|
probs_split, vocabulary, beam_size, num_processes, cutoff_prob,
|
||
7 years ago
|
cutoff_top_n, ext_scoring_func)
|
||
4 years ago
|
batch_beam_results = [[(res[0], res[1]) for res in beam_results]
|
||
|
for beam_results in batch_beam_results]
|
||
7 years ago
|
return batch_beam_results
|