| 520 | |
| 521 | template <typename T> |
| 522 | int64_t edit_distance(const std::vector<T> & ref, const std::vector<T> & hyp) { |
| 523 | if (ref.empty()) { |
| 524 | return static_cast<int64_t>(hyp.size()); |
| 525 | } |
| 526 | if (hyp.empty()) { |
| 527 | return static_cast<int64_t>(ref.size()); |
| 528 | } |
| 529 | std::vector<int64_t> dp(hyp.size() + 1); |
| 530 | std::iota(dp.begin(), dp.end(), 0); |
| 531 | for (size_t i = 0; i < ref.size(); ++i) { |
| 532 | int64_t prev = dp[0]; |
| 533 | dp[0] = static_cast<int64_t>(i + 1); |
| 534 | for (size_t j = 0; j < hyp.size(); ++j) { |
| 535 | const int64_t temp = dp[j + 1]; |
| 536 | const int64_t cost = ref[i] == hyp[j] ? 0 : 1; |
| 537 | dp[j + 1] = std::min({dp[j + 1] + 1, dp[j] + 1, prev + cost}); |
| 538 | prev = temp; |
| 539 | } |
| 540 | } |
| 541 | return dp.back(); |
| 542 | } |
| 543 | |
| 544 | double asr_error(const std::string & reference, const std::string & hypothesis, const std::string & language) { |
| 545 | if (language == "en") { |