|
| 1 | +#include "_edit_distance_osa.hpp" |
| 2 | +#include <algorithm> |
| 3 | +#include <cmath> |
| 4 | + |
| 5 | +std::vector<std::vector<double>> compute_dp_table( |
| 6 | + const std::string& a, |
| 7 | + const std::string& b, |
| 8 | + const std::map<EditopName, double>& cost_map |
| 9 | +) { |
| 10 | + int len_a = a.length(); |
| 11 | + int len_b = b.length(); |
| 12 | + std::vector<std::vector<double>> dp(len_a + 1, std::vector<double>(len_b + 1, 0.0)); |
| 13 | + |
| 14 | + for (int i = 0; i <= len_a; ++i) { |
| 15 | + dp[i][0] = i * cost_map.at(DELETE); |
| 16 | + } |
| 17 | + for (int j = 0; j <= len_b; ++j) { |
| 18 | + dp[0][j] = j * cost_map.at(INSERT); |
| 19 | + } |
| 20 | + |
| 21 | + for (int i = 1; i <= len_a; ++i) { |
| 22 | + for (int j = 1; j <= len_b; ++j) { |
| 23 | + double deletion = dp[i-1][j] + cost_map.at(DELETE); |
| 24 | + double insertion = dp[i][j-1] + cost_map.at(INSERT); |
| 25 | + double substitution_cost = (a[i-1] == b[j-1]) ? 0.0 : cost_map.at(REPLACE); |
| 26 | + double substitution = dp[i-1][j-1] + substitution_cost; |
| 27 | + |
| 28 | + dp[i][j] = std::min({deletion, insertion, substitution}); |
| 29 | + |
| 30 | + if (i > 1 && j > 1 && |
| 31 | + a[i-1] == b[j-2] && a[i-2] == b[j-1]) { |
| 32 | + dp[i][j] = std::min(dp[i][j], |
| 33 | + dp[i-2][j-2] + cost_map.at(TRANSPOSE)); |
| 34 | + } |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + return dp; |
| 39 | +} |
| 40 | + |
| 41 | + |
| 42 | +double cpp_compute_distance( |
| 43 | + const std::string& a, |
| 44 | + const std::string& b, |
| 45 | + const std::map<EditopName, double>& cost_map |
| 46 | +) { |
| 47 | + auto dp = compute_dp_table(a, b, cost_map); |
| 48 | + return dp[a.length()][b.length()]; |
| 49 | +} |
| 50 | + |
| 51 | +std::vector<std::vector<Editop>> backtrack_all_paths( |
| 52 | + const std::string& a, |
| 53 | + const std::string& b, |
| 54 | + const std::map<EditopName, double>& cost_map, |
| 55 | + const std::vector<std::vector<double>>& dp, |
| 56 | + int i, |
| 57 | + int j, |
| 58 | + std::vector<Editop>& current_path |
| 59 | +) { |
| 60 | + if (i == 0 && j == 0) { |
| 61 | + std::vector<Editop> reversed_path = current_path; |
| 62 | + std::reverse(reversed_path.begin(), reversed_path.end()); |
| 63 | + return {reversed_path}; |
| 64 | + } |
| 65 | + |
| 66 | + std::vector<std::vector<Editop>> all_paths; |
| 67 | + double current_cost = dp[i][j]; |
| 68 | + const double tol = 1e-6; |
| 69 | + |
| 70 | + |
| 71 | + if (i > 0 && std::abs((dp[i-1][j] + cost_map.at(DELETE)) - current_cost) < tol) { |
| 72 | + Editop op(DELETE, i-1, i-1, cost_map.at(DELETE), std::string(1, a[i-1])); |
| 73 | + current_path.push_back(op); |
| 74 | + auto paths = backtrack_all_paths(a, b, cost_map, dp, i-1, j, current_path); |
| 75 | + all_paths.insert(all_paths.end(), paths.begin(), paths.end()); |
| 76 | + current_path.pop_back(); |
| 77 | + } |
| 78 | + |
| 79 | + if (j > 0 && std::abs((dp[i][j-1] + cost_map.at(INSERT)) - current_cost) < tol) { |
| 80 | + Editop op(INSERT, i, i, cost_map.at(INSERT), std::string(1, b[j-1])); |
| 81 | + current_path.push_back(op); |
| 82 | + auto paths = backtrack_all_paths(a, b, cost_map, dp, i, j-1, current_path); |
| 83 | + all_paths.insert(all_paths.end(), paths.begin(), paths.end()); |
| 84 | + current_path.pop_back(); |
| 85 | + } |
| 86 | + |
| 87 | + |
| 88 | + if (i > 0 && j > 0) { |
| 89 | + double sub_cost = (a[i-1] == b[j-1]) ? 0.0 : cost_map.at(REPLACE); |
| 90 | + if (std::abs((dp[i-1][j-1] + sub_cost) - current_cost) < tol) { |
| 91 | + std::string out_char = (sub_cost == 0.0) ? std::string(1, a[i-1]) : std::string(1, b[j-1]); |
| 92 | + Editop op(REPLACE, i-1, j-1, sub_cost, out_char); |
| 93 | + current_path.push_back(op); |
| 94 | + auto paths = backtrack_all_paths(a, b, cost_map, dp, i-1, j-1, current_path); |
| 95 | + all_paths.insert(all_paths.end(), paths.begin(), paths.end()); |
| 96 | + current_path.pop_back(); |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + |
| 101 | + if (i > 1 && j > 1 && |
| 102 | + a[i-1] == b[j-2] && a[i-2] == b[j-1] && |
| 103 | + std::abs((dp[i-2][j-2] + cost_map.at(TRANSPOSE)) - current_cost) < tol) { |
| 104 | + std::string transpose_str = std::string(1, b[j-2]) + std::string(1, b[j-1]); |
| 105 | + Editop op(TRANSPOSE, i-2, j-2, cost_map.at(TRANSPOSE), transpose_str); |
| 106 | + current_path.push_back(op); |
| 107 | + auto paths = backtrack_all_paths(a, b, cost_map, dp, i-2, j-2, current_path); |
| 108 | + all_paths.insert(all_paths.end(), paths.begin(), paths.end()); |
| 109 | + current_path.pop_back(); |
| 110 | + } |
| 111 | + |
| 112 | + return all_paths; |
| 113 | +} |
| 114 | + |
| 115 | + |
| 116 | +std::vector<std::vector<Editop>> cpp_compute_all_paths( |
| 117 | + const std::string& a, |
| 118 | + const std::string& b, |
| 119 | + const std::map<EditopName, double>& cost_map |
| 120 | +) { |
| 121 | + auto dp = compute_dp_table(a, b, cost_map); |
| 122 | + std::vector<Editop> current_path; |
| 123 | + return backtrack_all_paths(a, b, cost_map, dp, a.length(), b.length(), current_path); |
| 124 | +} |
| 125 | + |
| 126 | + |
| 127 | +void cpp_print_all_paths( |
| 128 | + const std::string& a, |
| 129 | + const std::string& b, |
| 130 | + const std::map<EditopName, double>& cost_map |
| 131 | +) { |
| 132 | + auto paths = cpp_compute_all_paths(a, b, cost_map); |
| 133 | + double distance = cpp_compute_distance(a, b, cost_map); |
| 134 | + |
| 135 | + std::cout << "OSA Distance from '" << a << "' to '" << b << "': " << distance << std::endl; |
| 136 | + std::cout << "Number of optimal edit sequences: " << paths.size() << std::endl; |
| 137 | + std::cout << std::endl; |
| 138 | + |
| 139 | + for (size_t i = 0; i < paths.size(); ++i) { |
| 140 | + std::cout << "Path " << (i + 1) << ":" << std::endl; |
| 141 | + for (const auto& op : paths[i]) { |
| 142 | + std::cout << " " << op << std::endl; |
| 143 | + } |
| 144 | + std::cout << std::endl; |
| 145 | + } |
| 146 | +} |
| 147 | + |
| 148 | +std::string editop_name_to_string(EditopName name) { |
| 149 | + switch (name) { |
| 150 | + case INSERT: return "INSERT"; |
| 151 | + case DELETE: return "DELETE"; |
| 152 | + case REPLACE: return "REPLACE"; |
| 153 | + case TRANSPOSE: return "TRANSPOSE"; |
| 154 | + default: return "UNKNOWN"; |
| 155 | + } |
| 156 | +} |
| 157 | + |
| 158 | +std::ostream& operator<<(std::ostream& os, const Editop& op) { |
| 159 | + os << "Editop(name=" << editop_name_to_string(op.name) |
| 160 | + << ", src_idx=" << op.src_idx |
| 161 | + << ", dst_idx=" << op.dst_idx |
| 162 | + << ", cost=" << op.cost |
| 163 | + << ", output_string='" << op.output_string << "')"; |
| 164 | + return os; |
| 165 | +} |
0 commit comments