Const members in structs

This commit is contained in:
Reuben Morais 2020-03-18 19:11:58 +01:00
parent 2ec34d5a06
commit 1547498e82
6 changed files with 38 additions and 44 deletions

View File

@ -45,7 +45,7 @@ struct meta_word {
}; };
char* char*
CandidateTranscriptToString(CandidateTranscript* transcript) CandidateTranscriptToString(const CandidateTranscript* transcript)
{ {
std::string retval = ""; std::string retval = "";
for (int i = 0; i < transcript->num_tokens; i++) { for (int i = 0; i < transcript->num_tokens; i++) {
@ -56,7 +56,7 @@ CandidateTranscriptToString(CandidateTranscript* transcript)
} }
std::vector<meta_word> std::vector<meta_word>
CandidateTranscriptToWords(CandidateTranscript* transcript) CandidateTranscriptToWords(const CandidateTranscript* transcript)
{ {
std::vector<meta_word> word_list; std::vector<meta_word> word_list;
@ -101,7 +101,7 @@ CandidateTranscriptToWords(CandidateTranscript* transcript)
} }
std::string std::string
CandidateTranscriptToJSON(CandidateTranscript *transcript) CandidateTranscriptToJSON(const CandidateTranscript *transcript)
{ {
std::ostringstream out_string; std::ostringstream out_string;
@ -130,7 +130,7 @@ MetadataToJSON(Metadata* result)
out_string << "{\n"; out_string << "{\n";
for (int j=0; j < result->num_transcripts; ++j) { for (int j=0; j < result->num_transcripts; ++j) {
CandidateTranscript *transcript = &result->transcripts[j]; const CandidateTranscript *transcript = &result->transcripts[j];
if (j == 0) { if (j == 0) {
out_string << CandidateTranscriptToJSON(transcript); out_string << CandidateTranscriptToJSON(transcript);

View File

@ -478,14 +478,14 @@ DS_FreeMetadata(Metadata* m)
if (m) { if (m) {
for (int i = 0; i < m->num_transcripts; ++i) { for (int i = 0; i < m->num_transcripts; ++i) {
for (int j = 0; j < m->transcripts[i].num_tokens; ++j) { for (int j = 0; j < m->transcripts[i].num_tokens; ++j) {
free(m->transcripts[i].tokens[j].text); free((void*)m->transcripts[i].tokens[j].text);
} }
delete[] m->transcripts[i].tokens; free((void*)m->transcripts[i].tokens);
} }
delete[] m->transcripts; free((void*)m->transcripts);
delete m; free(m);
} }
} }

View File

@ -24,13 +24,13 @@ typedef struct StreamingState StreamingState;
*/ */
typedef struct TokenMetadata { typedef struct TokenMetadata {
/** The text corresponding to this token */ /** The text corresponding to this token */
char* text; const char* const text;
/** Position of the token in units of 20ms */ /** Position of the token in units of 20ms */
unsigned int timestep; const unsigned int timestep;
/** Position of the token in seconds */ /** Position of the token in seconds */
float start_time; const float start_time;
} TokenMetadata; } TokenMetadata;
/** /**
@ -39,14 +39,14 @@ typedef struct TokenMetadata {
*/ */
typedef struct CandidateTranscript { typedef struct CandidateTranscript {
/** Array of TokenMetadata objects */ /** Array of TokenMetadata objects */
TokenMetadata* tokens; const TokenMetadata* const tokens;
/** Size of the tokens array */ /** Size of the tokens array */
unsigned int num_tokens; const unsigned int num_tokens;
/** Approximated confidence value for this transcript. This is roughly the /** Approximated confidence value for this transcript. This is roughly the
* sum of the acoustic model logit values for each timestep/character that * sum of the acoustic model logit values for each timestep/character that
* contributed to the creation of this transcript. * contributed to the creation of this transcript.
*/ */
double confidence; const double confidence;
} CandidateTranscript; } CandidateTranscript;
/** /**
@ -54,9 +54,9 @@ typedef struct CandidateTranscript {
*/ */
typedef struct Metadata { typedef struct Metadata {
/** Array of CandidateTranscript objects */ /** Array of CandidateTranscript objects */
CandidateTranscript* transcripts; const CandidateTranscript* const transcripts;
/** Size of the transcripts array */ /** Size of the transcripts array */
unsigned int num_transcripts; const unsigned int num_transcripts;
} Metadata; } Metadata;
enum DeepSpeech_Error_Codes enum DeepSpeech_Error_Codes

View File

@ -85,10 +85,6 @@ using namespace node;
%ignore Metadata::num_transcripts; %ignore Metadata::num_transcripts;
%ignore CandidateTranscript::num_tokens; %ignore CandidateTranscript::num_tokens;
%immutable Metadata::transcripts;
%immutable CandidateTranscripts::tokens;
%immutable TokenMetadata::text;
%nodefaultctor Metadata; %nodefaultctor Metadata;
%nodefaultdtor Metadata; %nodefaultdtor Metadata;
%nodefaultctor CandidateTranscript; %nodefaultctor CandidateTranscript;

View File

@ -41,33 +41,35 @@ ModelState::decode_metadata(const DecoderState& state,
size_t num_results) size_t num_results)
{ {
vector<Output> out = state.decode(num_results); vector<Output> out = state.decode(num_results);
size_t num_returned = out.size(); unsigned int num_returned = out.size();
std::unique_ptr<Metadata> metadata(new Metadata); CandidateTranscript* transcripts = (CandidateTranscript*)malloc(sizeof(CandidateTranscript)*num_returned);
metadata->num_transcripts = num_returned;
std::unique_ptr<CandidateTranscript[]> transcripts(new CandidateTranscript[num_returned]);
for (int i = 0; i < num_returned; ++i) { for (int i = 0; i < num_returned; ++i) {
transcripts[i].num_tokens = out[i].tokens.size(); TokenMetadata* tokens = (TokenMetadata*)malloc(sizeof(TokenMetadata)*out[i].tokens.size());
transcripts[i].confidence = out[i].confidence;
std::unique_ptr<TokenMetadata[]> tokens(new TokenMetadata[transcripts[i].num_tokens]);
// Loop through each token
for (int j = 0; j < out[i].tokens.size(); ++j) { for (int j = 0; j < out[i].tokens.size(); ++j) {
tokens[j].text = strdup(alphabet_.StringFromLabel(out[i].tokens[j]).c_str()); TokenMetadata token {
tokens[j].timestep = out[i].timesteps[j]; strdup(alphabet_.StringFromLabel(out[i].tokens[j]).c_str()), // text
tokens[j].start_time = out[i].timesteps[j] * ((float)audio_win_step_ / sample_rate_); static_cast<unsigned int>(out[i].timesteps[j]), // timestep
out[i].timesteps[j] * ((float)audio_win_step_ / sample_rate_), // start_time
if (tokens[j].start_time < 0) { };
tokens[j].start_time = 0; memcpy(&tokens[j], &token, sizeof(TokenMetadata));
}
} }
transcripts[i].tokens = tokens.release(); CandidateTranscript transcript {
tokens, // tokens
static_cast<unsigned int>(out[i].tokens.size()), // num_tokens
out[i].confidence, // confidence
};
memcpy(&transcripts[i], &transcript, sizeof(CandidateTranscript));
} }
metadata->transcripts = transcripts.release(); Metadata* ret = (Metadata*)malloc(sizeof(Metadata));
return metadata.release(); Metadata metadata {
transcripts, // transcripts
num_returned, // num_transcripts
};
memcpy(ret, &metadata, sizeof(Metadata));
return ret;
} }

View File

@ -108,10 +108,6 @@ static PyObject *parent_reference() {
} }
} }
%immutable Metadata::transcripts;
%immutable CandidateTranscript::tokens;
%immutable TokenMetadata::text;
%nodefaultctor Metadata; %nodefaultctor Metadata;
%nodefaultdtor Metadata; %nodefaultdtor Metadata;
%nodefaultctor CandidateTranscript; %nodefaultctor CandidateTranscript;