2023-03-27 14:59:40 +08:00
|
|
|
// sherpa-onnx/csrc/offline-recognizer-impl.h
|
|
|
|
|
//
|
|
|
|
|
// Copyright (c) 2023 Xiaomi Corporation
|
|
|
|
|
|
|
|
|
|
#ifndef SHERPA_ONNX_CSRC_OFFLINE_RECOGNIZER_IMPL_H_
|
|
|
|
|
#define SHERPA_ONNX_CSRC_OFFLINE_RECOGNIZER_IMPL_H_
|
|
|
|
|
|
|
|
|
|
#include <memory>
|
2023-09-14 19:33:17 +08:00
|
|
|
#include <string>
|
2023-06-16 14:26:36 +08:00
|
|
|
#include <vector>
|
2023-03-27 14:59:40 +08:00
|
|
|
|
2024-06-17 14:28:53 +08:00
|
|
|
#include "kaldifst/csrc/text-normalizer.h"
|
2025-04-27 15:31:11 +08:00
|
|
|
#include "sherpa-onnx/csrc/homophone-replacer.h"
|
2023-06-16 14:26:36 +08:00
|
|
|
#include "sherpa-onnx/csrc/macros.h"
|
2023-03-27 14:59:40 +08:00
|
|
|
#include "sherpa-onnx/csrc/offline-recognizer.h"
|
|
|
|
|
#include "sherpa-onnx/csrc/offline-stream.h"
|
|
|
|
|
|
|
|
|
|
namespace sherpa_onnx {
|
|
|
|
|
|
|
|
|
|
class OfflineRecognizerImpl {
|
|
|
|
|
public:
|
2024-06-17 14:28:53 +08:00
|
|
|
explicit OfflineRecognizerImpl(const OfflineRecognizerConfig &config);
|
|
|
|
|
|
2023-03-27 14:59:40 +08:00
|
|
|
static std::unique_ptr<OfflineRecognizerImpl> Create(
|
|
|
|
|
const OfflineRecognizerConfig &config);
|
|
|
|
|
|
2024-11-26 16:38:35 +08:00
|
|
|
template <typename Manager>
|
|
|
|
|
OfflineRecognizerImpl(Manager *mgr, const OfflineRecognizerConfig &config);
|
2024-06-17 14:28:53 +08:00
|
|
|
|
2024-11-26 16:38:35 +08:00
|
|
|
template <typename Manager>
|
2023-09-12 15:40:16 +08:00
|
|
|
static std::unique_ptr<OfflineRecognizerImpl> Create(
|
2024-11-26 16:38:35 +08:00
|
|
|
Manager *mgr, const OfflineRecognizerConfig &config);
|
2023-09-12 15:40:16 +08:00
|
|
|
|
2023-03-27 14:59:40 +08:00
|
|
|
virtual ~OfflineRecognizerImpl() = default;
|
|
|
|
|
|
2023-06-16 14:26:36 +08:00
|
|
|
virtual std::unique_ptr<OfflineStream> CreateStream(
|
2023-09-14 19:33:17 +08:00
|
|
|
const std::string &hotwords) const {
|
2023-06-16 14:26:36 +08:00
|
|
|
SHERPA_ONNX_LOGE("Only transducer models support contextual biasing.");
|
|
|
|
|
exit(-1);
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-27 14:59:40 +08:00
|
|
|
virtual std::unique_ptr<OfflineStream> CreateStream() const = 0;
|
|
|
|
|
|
|
|
|
|
virtual void DecodeStreams(OfflineStream **ss, int32_t n) const = 0;
|
2024-06-17 14:28:53 +08:00
|
|
|
|
2024-07-13 07:30:47 -07:00
|
|
|
virtual void SetConfig(const OfflineRecognizerConfig &config);
|
|
|
|
|
|
|
|
|
|
virtual OfflineRecognizerConfig GetConfig() const = 0;
|
|
|
|
|
|
2024-06-17 14:28:53 +08:00
|
|
|
std::string ApplyInverseTextNormalization(std::string text) const;
|
|
|
|
|
|
2025-04-27 15:31:11 +08:00
|
|
|
std::string ApplyHomophoneReplacer(std::string text) const;
|
|
|
|
|
|
2024-06-17 14:28:53 +08:00
|
|
|
private:
|
|
|
|
|
OfflineRecognizerConfig config_;
|
|
|
|
|
// for inverse text normalization. Used only if
|
|
|
|
|
// config.rule_fsts is not empty or
|
|
|
|
|
// config.rule_fars is not empty
|
|
|
|
|
std::vector<std::unique_ptr<kaldifst::TextNormalizer>> itn_list_;
|
2025-04-27 15:31:11 +08:00
|
|
|
std::unique_ptr<HomophoneReplacer> hr_;
|
2023-03-27 14:59:40 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
} // namespace sherpa_onnx
|
|
|
|
|
|
|
|
|
|
#endif // SHERPA_ONNX_CSRC_OFFLINE_RECOGNIZER_IMPL_H_
|