Computes the length of the LCS between two seqs. The implementation below uses a DP programming algorithm and runs in O(nm) time where n = len(x) and m = len(y). Source: http://www.algorithmist.com/index.php/Longest_Common_Subsequence Args: x: collection of words y: collection of w
(x, y)
| 261 | |
| 262 | |
| 263 | def _lcs(x, y): |
| 264 | """Computes the length of the LCS between two seqs. |
| 265 | |
| 266 | The implementation below uses a DP programming algorithm and runs |
| 267 | in O(nm) time where n = len(x) and m = len(y). |
| 268 | Source: http://www.algorithmist.com/index.php/Longest_Common_Subsequence |
| 269 | |
| 270 | Args: |
| 271 | x: collection of words |
| 272 | y: collection of words |
| 273 | |
| 274 | Returns: |
| 275 | Table of dictionary of coord and len lcs |
| 276 | """ |
| 277 | n, m = len(x), len(y) |
| 278 | table = dict() |
| 279 | for i in range(n + 1): |
| 280 | for j in range(m + 1): |
| 281 | if i == 0 or j == 0: |
| 282 | table[i, j] = 0 |
| 283 | elif x[i - 1] == y[j - 1]: |
| 284 | table[i, j] = table[i - 1, j - 1] + 1 |
| 285 | else: |
| 286 | table[i, j] = max(table[i - 1, j], table[i, j - 1]) |
| 287 | return table |
| 288 | |
| 289 | |
| 290 | def _ngrams(sequence, order): |
no outgoing calls
no test coverage detected
searching dependent graphs…