Skip to content

Commit

Permalink
Add sentence WER for NLP input (#35)
Browse files Browse the repository at this point in the history
  • Loading branch information
nishchalb authored Sep 22, 2022
1 parent 57d9962 commit 467e2ea
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/fstalign.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -678,9 +678,10 @@ void HandleWer(FstLoader *refLoader, FstLoader *hypLoader, SynonymEngine *engine
RecordSpeakerSwitchWer(stitches, alignerOptions.speaker_switch_context_size);
}

// Calculate and record per-speaker WER
// Calculate and record supplementary WER
RecordSpeakerWer(stitches);
RecordTagWer(stitches);
RecordSentenceWer(stitches);

if (!output_nlp.empty()) {
ofstream nlp_ostream(output_nlp);
Expand Down
28 changes: 28 additions & 0 deletions src/wer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,34 @@ void RecordWer(spWERA topAlignment) {
}
}

void RecordSentenceWer(vector<shared_ptr<Stitching>> stitches) {
std::set<std::string> eos_punc{".", "?", "!"};
vector<WerResult> sentence_wers;
WerResult curr_wer = {0, 0, 0, 0, 0};
for (auto &stitch : stitches) {
curr_wer.deletions += stitch->comment.rfind("del", 0) == 0;
curr_wer.insertions += stitch->comment.rfind("ins", 0) == 0;
curr_wer.substitutions += stitch->comment.rfind("sub", 0) == 0;
curr_wer.numWordsInReference += stitch->comment.rfind("ins", 0) != 0;

// Check if we hit EOS
if (eos_punc.find(stitch->nlpRow.punctuation) != eos_punc.end()) {
sentence_wers.push_back(curr_wer);
curr_wer = {0, 0, 0, 0, 0};
}
}
// Add last one if its empty case
if (curr_wer.numWordsInReference > 0) {
sentence_wers.push_back(curr_wer);
}

// Add to log
for (int i=0; i < sentence_wers.size(); i++) {
RecordWerResult(jsonLogger::JsonLogger::getLogger().root["wer"]["sentenceWer"][i], sentence_wers[i]);
}
}


void RecordSpeakerWer(vector<shared_ptr<Stitching>> stitches) {
// Note: stitches must have already been aligned to NLP rows
// Logic for segment boundaries copied from speaker switch WER code
Expand Down
1 change: 1 addition & 0 deletions src/wer.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ void RecordWerResult(Json::Value &json, WerResult wr);
void RecordWer(spWERA topAlignment);
void RecordSpeakerWer(vector<shared_ptr<Stitching>> stitches);
void RecordSpeakerSwitchWer(vector<shared_ptr<Stitching>> stitches, int speaker_switch_context_size);
void RecordSentenceWer(vector<shared_ptr<Stitching>> stitches);
void RecordTagWer(vector<shared_ptr<Stitching>> stitches);
void RecordCaseWer(vector<shared_ptr<Stitching>> aligned_stitches);

Expand Down

0 comments on commit 467e2ea

Please sign in to comment.