Add non-streaming ASR (#92)

This commit is contained in:
Fangjun Kuang
2023-03-26 08:53:42 +08:00
committed by GitHub
parent 6f92bc7362
commit 5572246253
48 changed files with 1526 additions and 150 deletions

View File

@@ -0,0 +1,87 @@
// sherpa-onnx/csrc/offline-recognizer.h
//
// Copyright (c) 2023 Xiaomi Corporation
#ifndef SHERPA_ONNX_CSRC_OFFLINE_RECOGNIZER_H_
#define SHERPA_ONNX_CSRC_OFFLINE_RECOGNIZER_H_
#include <memory>
#include <string>
#include <vector>
#include "sherpa-onnx/csrc/offline-stream.h"
#include "sherpa-onnx/csrc/offline-transducer-model-config.h"
#include "sherpa-onnx/csrc/parse-options.h"
namespace sherpa_onnx {
struct OfflineRecognitionResult {
// Recognition results.
// For English, it consists of space separated words.
// For Chinese, it consists of Chinese words without spaces.
std::string text;
// Decoded results at the token level.
// For instance, for BPE-based models it consists of a list of BPE tokens.
std::vector<std::string> tokens;
/// timestamps.size() == tokens.size()
/// timestamps[i] records the time in seconds when tokens[i] is decoded.
std::vector<float> timestamps;
};
struct OfflineRecognizerConfig {
OfflineFeatureExtractorConfig feat_config;
OfflineTransducerModelConfig model_config;
std::string decoding_method = "greedy_search";
// only greedy_search is implemented
// TODO(fangjun): Implement modified_beam_search
OfflineRecognizerConfig() = default;
OfflineRecognizerConfig(const OfflineFeatureExtractorConfig &feat_config,
const OfflineTransducerModelConfig &model_config,
const std::string &decoding_method)
: feat_config(feat_config),
model_config(model_config),
decoding_method(decoding_method) {}
void Register(ParseOptions *po);
bool Validate() const;
std::string ToString() const;
};
class OfflineRecognizer {
public:
~OfflineRecognizer();
explicit OfflineRecognizer(const OfflineRecognizerConfig &config);
/// Create a stream for decoding.
std::unique_ptr<OfflineStream> CreateStream() const;
/** Decode a single stream
*
* @param s The stream to decode.
*/
void DecodeStream(OfflineStream *s) const {
OfflineStream *ss[1] = {s};
DecodeStreams(ss, 1);
}
/** Decode a list of streams.
*
* @param ss Pointer to an array of streams.
* @param n Size of the input array.
*/
void DecodeStreams(OfflineStream **ss, int32_t n) const;
private:
class Impl;
std::unique_ptr<Impl> impl_;
};
} // namespace sherpa_onnx
#endif // SHERPA_ONNX_CSRC_OFFLINE_RECOGNIZER_H_