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

View File

@ -478,14 +478,14 @@ DS_FreeMetadata(Metadata* m)
if (m) {
for (int i = 0; i < m->num_transcripts; ++i) {
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;
delete m;
free((void*)m->transcripts);
free(m);
}
}

View File

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

View File

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

View File

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