Fix style issues (#1458)

This commit is contained in:
Fangjun Kuang
2024-10-24 11:15:08 +08:00
committed by GitHub
parent ceb69ebd94
commit b3e05f6dc4
55 changed files with 155 additions and 117 deletions

View File

@@ -16,22 +16,22 @@ namespace sherpa_onnx::cxx {
// ============================================================================
// Streaming ASR
// ============================================================================
struct SHERPA_ONNX_API OnlineTransducerModelConfig {
struct OnlineTransducerModelConfig {
std::string encoder;
std::string decoder;
std::string joiner;
};
struct SHERPA_ONNX_API OnlineParaformerModelConfig {
struct OnlineParaformerModelConfig {
std::string encoder;
std::string decoder;
};
struct SHERPA_ONNX_API OnlineZipformer2CtcModelConfig {
struct OnlineZipformer2CtcModelConfig {
std::string model;
};
struct SHERPA_ONNX_API OnlineModelConfig {
struct OnlineModelConfig {
OnlineTransducerModelConfig transducer;
OnlineParaformerModelConfig paraformer;
OnlineZipformer2CtcModelConfig zipformer2_ctc;
@@ -45,17 +45,17 @@ struct SHERPA_ONNX_API OnlineModelConfig {
std::string tokens_buf;
};
struct SHERPA_ONNX_API FeatureConfig {
struct FeatureConfig {
int32_t sample_rate = 16000;
int32_t feature_dim = 80;
};
struct SHERPA_ONNX_API OnlineCtcFstDecoderConfig {
struct OnlineCtcFstDecoderConfig {
std::string graph;
int32_t max_active = 3000;
};
struct SHERPA_ONNX_API OnlineRecognizerConfig {
struct OnlineRecognizerConfig {
FeatureConfig feat_config;
OnlineModelConfig model_config;
@@ -83,14 +83,14 @@ struct SHERPA_ONNX_API OnlineRecognizerConfig {
std::string hotwords_buf;
};
struct SHERPA_ONNX_API OnlineRecognizerResult {
struct OnlineRecognizerResult {
std::string text;
std::vector<std::string> tokens;
std::vector<float> timestamps;
std::string json;
};
struct SHERPA_ONNX_API Wave {
struct Wave {
std::vector<float> samples;
int32_t sample_rate;
};
@@ -118,6 +118,8 @@ class SHERPA_ONNX_API MoveOnly {
Destroy();
p_ = other.Release();
return *this;
}
const T *Get() const { return p_; }

View File

@@ -18,6 +18,7 @@ set(sources
endpoint.cc
features.cc
file-utils.cc
fst-utils.cc
hypothesis.cc
keyword-spotter-impl.cc
keyword-spotter.cc

View File

@@ -0,0 +1,53 @@
// sherpa-onnx/csrc/fst-utils.cc
//
// Copyright (c) 2024 Xiaomi Corporation
#include "sherpa-onnx/csrc/fst-utils.h"
#include "sherpa-onnx/csrc/macros.h"
namespace sherpa_onnx {
// This function is copied from kaldi.
//
// @param filename Path to a StdVectorFst or StdConstFst graph
// @return The caller should free the returned pointer using `delete` to
// avoid memory leak.
fst::Fst<fst::StdArc> *ReadGraph(const std::string &filename) {
// read decoding network FST
std::ifstream is(filename, std::ios::binary);
if (!is.good()) {
SHERPA_ONNX_LOGE("Could not open decoding-graph FST %s", filename.c_str());
}
fst::FstHeader hdr;
if (!hdr.Read(is, "<unknown>")) {
SHERPA_ONNX_LOGE("Reading FST: error reading FST header.");
}
if (hdr.ArcType() != fst::StdArc::Type()) {
SHERPA_ONNX_LOGE("FST with arc type %s not supported",
hdr.ArcType().c_str());
}
fst::FstReadOptions ropts("<unspecified>", &hdr);
fst::Fst<fst::StdArc> *decode_fst = nullptr;
if (hdr.FstType() == "vector") {
decode_fst = fst::VectorFst<fst::StdArc>::Read(is, ropts);
} else if (hdr.FstType() == "const") {
decode_fst = fst::ConstFst<fst::StdArc>::Read(is, ropts);
} else {
SHERPA_ONNX_LOGE("Reading FST: unsupported FST type: %s",
hdr.FstType().c_str());
}
if (decode_fst == nullptr) { // fst code will warn.
SHERPA_ONNX_LOGE("Error reading FST (after reading header).");
return nullptr;
} else {
return decode_fst;
}
}
} // namespace sherpa_onnx

View File

@@ -0,0 +1,18 @@
// sherpa-onnx/csrc/fst-utils.h
//
// Copyright (c) 2024 Xiaomi Corporation
#ifndef SHERPA_ONNX_CSRC_FST_UTILS_H_
#define SHERPA_ONNX_CSRC_FST_UTILS_H_
#include <string>
#include "fst/fstlib.h"
namespace sherpa_onnx {
fst::Fst<fst::StdArc> *ReadGraph(const std::string &filename);
}
#endif // SHERPA_ONNX_CSRC_FST_UTILS_H_

View File

@@ -10,18 +10,12 @@
#include "cppjieba/Jieba.hpp"
#include "sherpa-onnx/csrc/file-utils.h"
#include "sherpa-onnx/csrc/lexicon.h"
#include "sherpa-onnx/csrc/macros.h"
#include "sherpa-onnx/csrc/text-utils.h"
namespace sherpa_onnx {
// implemented in ./lexicon.cc
std::unordered_map<std::string, int32_t> ReadTokens(std::istream &is);
std::vector<int32_t> ConvertTokensToIds(
const std::unordered_map<std::string, int32_t> &token2id,
const std::vector<std::string> &tokens);
class JiebaLexicon::Impl {
public:
Impl(const std::string &lexicon, const std::string &tokens,

View File

@@ -6,6 +6,7 @@
#define SHERPA_ONNX_CSRC_LEXICON_H_
#include <cstdint>
#include <istream>
#include <memory>
#include <string>
#include <unordered_map>
@@ -66,6 +67,12 @@ class Lexicon : public OfflineTtsFrontend {
bool debug_ = false;
};
std::unordered_map<std::string, int32_t> ReadTokens(std::istream &is);
std::vector<int32_t> ConvertTokensToIds(
const std::unordered_map<std::string, int32_t> &token2id,
const std::vector<std::string> &tokens);
} // namespace sherpa_onnx
#endif // SHERPA_ONNX_CSRC_LEXICON_H_

View File

@@ -10,18 +10,12 @@
#include "cppjieba/Jieba.hpp"
#include "sherpa-onnx/csrc/file-utils.h"
#include "sherpa-onnx/csrc/lexicon.h"
#include "sherpa-onnx/csrc/macros.h"
#include "sherpa-onnx/csrc/text-utils.h"
namespace sherpa_onnx {
// implemented in ./lexicon.cc
std::unordered_map<std::string, int32_t> ReadTokens(std::istream &is);
std::vector<int32_t> ConvertTokensToIds(
const std::unordered_map<std::string, int32_t> &token2id,
const std::vector<std::string> &tokens);
class MeloTtsLexicon::Impl {
public:
Impl(const std::string &lexicon, const std::string &tokens,

View File

@@ -11,52 +11,11 @@
#include "kaldi-decoder/csrc/decodable-ctc.h"
#include "kaldi-decoder/csrc/eigen.h"
#include "kaldi-decoder/csrc/faster-decoder.h"
#include "sherpa-onnx/csrc/fst-utils.h"
#include "sherpa-onnx/csrc/macros.h"
namespace sherpa_onnx {
// This function is copied from kaldi.
//
// @param filename Path to a StdVectorFst or StdConstFst graph
// @return The caller should free the returned pointer using `delete` to
// avoid memory leak.
fst::Fst<fst::StdArc> *ReadGraph(const std::string &filename) {
// read decoding network FST
std::ifstream is(filename, std::ios::binary);
if (!is.good()) {
SHERPA_ONNX_LOGE("Could not open decoding-graph FST %s", filename.c_str());
}
fst::FstHeader hdr;
if (!hdr.Read(is, "<unknown>")) {
SHERPA_ONNX_LOGE("Reading FST: error reading FST header.");
}
if (hdr.ArcType() != fst::StdArc::Type()) {
SHERPA_ONNX_LOGE("FST with arc type %s not supported",
hdr.ArcType().c_str());
}
fst::FstReadOptions ropts("<unspecified>", &hdr);
fst::Fst<fst::StdArc> *decode_fst = nullptr;
if (hdr.FstType() == "vector") {
decode_fst = fst::VectorFst<fst::StdArc>::Read(is, ropts);
} else if (hdr.FstType() == "const") {
decode_fst = fst::ConstFst<fst::StdArc>::Read(is, ropts);
} else {
SHERPA_ONNX_LOGE("Reading FST: unsupported FST type: %s",
hdr.FstType().c_str());
}
if (decode_fst == nullptr) { // fst code will warn.
SHERPA_ONNX_LOGE("Error reading FST (after reading header).");
return nullptr;
} else {
return decode_fst;
}
}
/**
* @param decoder
* @param p Pointer to a 2-d array of shape (num_frames, vocab_size)

View File

@@ -5,6 +5,7 @@
#include "sherpa-onnx/csrc/offline-speaker-diarization-result.h"
#include <algorithm>
#include <array>
#include <sstream>
#include <string>
#include <unordered_set>
@@ -48,11 +49,13 @@ OfflineSpeakerDiarizationSegment::Merge(
}
std::string OfflineSpeakerDiarizationSegment::ToString() const {
char s[128];
snprintf(s, sizeof(s), "%.3f -- %.3f speaker_%02d", start_, end_, speaker_);
std::array<char, 128> s{};
snprintf(s.data(), s.size(), "%.3f -- %.3f speaker_%02d", start_, end_,
speaker_);
std::ostringstream os;
os << s;
os << s.data();
if (!text_.empty()) {
os << " " << text_;

View File

@@ -5,6 +5,7 @@
#include "sherpa-onnx/csrc/offline-speaker-diarization.h"
#include <string>
#include <utility>
#include "sherpa-onnx/csrc/offline-speaker-diarization-impl.h"
@@ -94,7 +95,7 @@ OfflineSpeakerDiarizationResult OfflineSpeakerDiarization::Process(
const float *audio, int32_t n,
OfflineSpeakerDiarizationProgressCallback callback /*= nullptr*/,
void *callback_arg /*= nullptr*/) const {
return impl_->Process(audio, n, callback, callback_arg);
return impl_->Process(audio, n, std::move(callback), callback_arg);
}
} // namespace sherpa_onnx

View File

@@ -13,14 +13,12 @@
#include "fst/fstlib.h"
#include "kaldi-decoder/csrc/decodable-ctc.h"
#include "kaldifst/csrc/fstext-utils.h"
#include "sherpa-onnx/csrc/fst-utils.h"
#include "sherpa-onnx/csrc/macros.h"
#include "sherpa-onnx/csrc/online-stream.h"
namespace sherpa_onnx {
// defined in ./offline-ctc-fst-decoder.cc
fst::Fst<fst::StdArc> *ReadGraph(const std::string &filename);
OnlineCtcFstDecoder::OnlineCtcFstDecoder(
const OnlineCtcFstDecoderConfig &config, int32_t blank_id)
: config_(config), fst_(ReadGraph(config.graph)), blank_id_(blank_id) {

View File

@@ -19,6 +19,8 @@
namespace sherpa_onnx {
namespace {
/// Helper for `OnlineRecognizerResult::AsJsonString()`
template <typename T>
std::string VecToString(const std::vector<T> &vec, int32_t precision = 6) {
@@ -51,6 +53,8 @@ std::string VecToString<std::string>(const std::vector<std::string> &vec,
return oss.str();
}
} // namespace
std::string OnlineRecognizerResult::AsJsonString() const {
std::ostringstream os;
os << "{ ";