Changes In Branch bdic_opt Excluding Merge-Ins
This is equivalent to a diff from 4a19028115 to a8c66433af
2020-09-16
| ||
06:09 | [graphspell] ibdawg: performance improvements [fr] tests [fx] tests check-in: fb9b86e9e8 user: olr tags: trunk, fr, fx, graphspell | |
2020-09-15
| ||
17:04 | [fr] update suggest tests Closed-Leaf check-in: a8c66433af user: olr tags: fr, bdic_opt | |
16:57 | [graphspell][py] suggestion: use Jaro-Winkler check-in: 7db21c89fd user: olr tags: graphspell, bdic_opt | |
2020-09-14
| ||
07:55 | [fr] ajustements check-in: cd8c458e7b user: olr tags: trunk, fr | |
2020-09-11
| ||
19:20 | merge trunk check-in: 43afb8b856 user: olr tags: bdic_opt | |
19:18 | [fr] tests: spellchecker.suggest() check-in: 4a19028115 user: olr tags: trunk, fr | |
14:21 | [fr] faux positif check-in: 86f302f4ef user: olr tags: trunk, fr | |
Modified gc_lang/fr/modules/tests_modules.py from [1156e862bc] to [420f61228e].
︙ | |||
18 19 20 21 22 23 24 | 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | - + | def timeblock (label, hDst=None): "performance counter (contextmanager)" start = time.perf_counter() try: yield finally: end = time.perf_counter() |
︙ | |||
47 48 49 50 51 52 53 | 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 | + - + + + + - + + | self.assertTrue(self.oDic.isValid(sWord), sWord) def test_isvalid_failed (self): for sWord in ["BranchE", "BRanche", "BRAnCHE", "émilie", "éMILIE", "émiLie"]: self.assertFalse(self.oDic.isValid(sWord), sWord) def test_suggest (self): for sWord in [ |
︙ |
Modified gc_lang/fr/perf_memo.text from [6a0d81df00] to [ad156793c1].
︙ | |||
26 27 28 29 30 31 32 | 26 27 28 29 30 31 32 33 34 35 | - - + + + | 0.6.2 2018.02.19 19:06 5.51302 1.29359 0.874157 0.260415 0.271596 0.290641 0.684754 0.376905 0.0815201 0.00919633 (spelling normalization) 1.0 2018.11.23 10:59 2.88577 0.702486 0.485648 0.139897 0.14079 0.148125 0.348751 0.201061 0.0360297 0.0043535 (x2, with new GC engine) 1.1 2019.05.16 09:42 1.50743 0.360923 0.261113 0.0749272 0.0763827 0.0771537 0.180504 0.102942 0.0182762 0.0021925 (×2, but new processor: AMD Ryzen 7 2700X) 1.2.1 2019.08.06 20:57 1.42886 0.358425 0.247356 0.0704405 0.0754886 0.0765604 0.177197 0.0988517 0.0188103 0.0020243 1.6.0 2020.01.03 20:22 1.38847 0.346214 0.240242 0.0709539 0.0737499 0.0748733 0.176477 0.0969171 0.0187857 0.0025143 (nouveau dictionnaire avec lemmes masculins) 1.9.0 2020.04.20 19:57 1.51183 0.369546 0.25681 0.0734314 0.0764396 0.0785668 0.183922 0.103674 0.0185812 0.002099 (NFC normalization) 1.9.2 2020.05.12 08:43 1.62465 0.398831 0.273012 0.0810811 0.080937 0.0845885 0.204133 0.114146 0.0212864 0.0029547 |
Modified gc_lang/fr/webext/gce_worker.js from [c94d68b34b] to [d11e3267b8].
︙ | |||
181 182 183 184 185 186 187 | 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 | - + | oTokenizer = new Tokenizer("fr"); if (dOptions !== null) { if (!(dOptions instanceof Map)) { dOptions = helpers.objectToMap(dOptions); } gc_engine.setOptions(dOptions); } |
︙ | |||
288 289 290 291 292 293 294 | 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 | - + + + + + + + + + + | function resetOptions (oInfo={}) { gc_engine.resetOptions(); let dOptions = helpers.mapToObject(gc_engine.getOptions()); postMessage(createResponse("resetOptions", dOptions, oInfo, true)); } function tests () { |
︙ |
Modified graphspell-js/char_player.js from [0602ec129b] to [dab3d08473].
1 2 3 4 5 6 7 8 9 10 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 | - + + + + + - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + | // list of similar chars // useful for suggestion mechanism /* jshint esversion:6 */ /* jslint esversion:6 */ ${map} var char_player = { |
︙ | |||
316 317 318 319 320 321 322 | 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 | - - + + + + | ["UN", ["EIN",]], ]), // End of word dFinal1: new Map([ ["a", ["as", "at", "ant", "ah"]], ["A", ["AS", "AT", "ANT", "AH"]], |
︙ |
Modified graphspell-js/ibdawg.js from [69d7490b82] to [fa91a09215].
︙ | |||
18 19 20 21 22 23 24 | 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 | - + + - - - + + + + + + + + - + - - - - - - - - - + + + + + + + + + + + + + - - - - - - - + + + + + + + + + + + - + + - - - + + + - + + + - - + + + + + - - - + + + + + + + + + + - - + - + - - + + + | var char_player = require("./char_player.js"); } class SuggResult { // Structure for storing, classifying and filtering suggestions |
︙ | |||
120 121 122 123 124 125 126 | 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 | - - - - - - - - - - - - - - + + + + + + + + + + + + - - - - - + + + + + + + + + + + + + + - - - + - - - + + + + - - - - - - - + + + - - - - - - - - - - - - - + - | } /* Properties: sName, nCompressionMethod, sHeader, lArcVal, nArcVal, sByDic, sLang, nChar, nBytesArc, nBytesNodeAddress, nEntry, nNode, nArc, nAff, cStemming, nTag, dChar, nBytesOffset, */ |
︙ | |||
304 305 306 307 308 309 310 | 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 | - + - + - + - + - + + - + | return false; } iAddr = this._lookupArcNode(this.dChar.get(c), iAddr); if (iAddr === null) { return false; } } |
︙ | |||
376 377 378 379 380 381 382 | 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 | - + | } } } _suggest (oSuggResult, sRemain, nMaxSwitch=0, nMaxDel=0, nMaxHardRepl=0, nMaxJump=0, nDist=0, nDeep=0, iAddr=0, sNewWord="", bAvoidLoop=false) { // returns a set of suggestions // recursive function |
︙ | |||
486 487 488 489 490 491 492 | 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 | - + - - - - | } _getTails (iAddr, sTail="", n=2) { // return a list of suffixes ending at a distance of <n> from <iAddr> let aTails = new Set(); for (let [nVal, jAddr] of this._getArcs(iAddr)) { if (nVal <= this.nChar) { |
︙ | |||
528 529 530 531 532 533 534 | 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 | - + - - - + - + - + - + - + - + - - + + - + - - + + - + - + - + - + - - + + - + - + - - + + - + - + - + - - - + + + - + - - - - - - - - - - - - - - - - - - - - - - - - - - - | zFlexPattern = (sFlexPattern !== "") ? new RegExp(sFlexPattern) : null; zTagsPattern = (sTagsPattern !== "") ? new RegExp(sTagsPattern) : null; } catch (e) { console.log("Error in regex pattern"); console.log(e.message); } |
Modified graphspell-js/str_transform.js from [4c4ee6009e] to [3bf8e3b480].
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | + | // STRING TRANSFORMATION /* jshint esversion:6, -W097 */ /* jslint esversion:6 */ /* global exports, console */ "use strict"; if (typeof(process) !== 'undefined') { var char_player = require("./char_player.js"); } // Note: 48 is the ASCII code for "0" var str_transform = { getNgrams: function (sWord, n=2) { let lNgrams = []; |
︙ | |||
61 62 63 64 65 66 67 68 69 70 71 72 73 74 | 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 | + + + + + + | if (c != sWord.slice(i, i+1) || (c == 'e' && sWord.slice(i, i+2) != "ee")) { // exception for <e> to avoid confusion between crée / créai sNewWord += c; } i++; } return sNewWord.replace(/eau/g, "o").replace(/au/g, "o").replace(/ai/g, "éi").replace(/ei/g, "é").replace(/ph/g, "f"); }, cleanWord: function (sWord) { // word clean for the user who make commun and preditive error help suggest // remove letters repeated more than 2 times return sWord.replace(/(.)\1{2,}/ig,'$1$1'); }, _xTransNumbersToExponent: new Map([ ["0", "⁰"], ["1", "¹"], ["2", "²"], ["3", "³"], ["4", "⁴"], ["5", "⁵"], ["6", "⁶"], ["7", "⁷"], ["8", "⁸"], ["9", "⁹"] ]), numbersToExponent: function (sWord) { let sNewWord = ""; |
︙ | |||
148 149 150 151 152 153 154 155 156 157 158 159 160 161 | 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 | + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + | } return matrix[nLen1][nLen2]; } catch (e) { console.error(e); } }, distanceJaroWinkler: function(a, b, boost = .666) { // https://github.com/thsig/jaro-winkler-JS //if (a == b) { return 1.0; } let a_len = a.length; let b_len = b.length; let a_flag = []; let b_flag = []; let search_range = Math.floor(Math.max(a_len, b_len) / 2) - 1; let minv = Math.min(a_len, b_len); // Looking only within the search range, count and flag the matched pairs. let Num_com = 0; let yl1 = b_len - 1; for (let i = 0; i < a_len; i++) { let lowlim = (i >= search_range) ? i - search_range : 0; let hilim = ((i + search_range) <= yl1) ? (i + search_range) : yl1; for (let j = lowlim; j <= hilim; j++) { if (b_flag[j] !== 1 && a[j] === b[i]) { a_flag[j] = 1; b_flag[i] = 1; Num_com++; break; } } } // Return if no characters in common if (Num_com === 0) { return 0.0; } // Count the number of transpositions let k = 0; let N_trans = 0; for (let i = 0; i < a_len; i++) { if (a_flag[i] === 1) { let j; for (j = k; j < b_len; j++) { if (b_flag[j] === 1) { k = j + 1; break; } } if (a[i] !== b[j]) { N_trans++; } } } N_trans = Math.floor(N_trans / 2); // Adjust for similarities in nonmatched characters let N_simi = 0; let adjwt = char_player.oDistanceBetweenChars; if (minv > Num_com) { for (let i = 0; i < a_len; i++) { if (!a_flag[i]) { for (let j = 0; j < b_len; j++) { if (!b_flag[j]) { if (adjwt[a[i]] && adjwt[a[i]][b[j]]) { N_simi += adjwt[a[i]][b[j]]; b_flag[j] = 2; break; } } } } } } let Num_sim = (N_simi / 10.0) + Num_com; // Main weight computation let weight = Num_sim / a_len + Num_sim / b_len + (Num_com - N_trans) / Num_com; weight = weight / 3; // Continue to boost the weight if the strings are similar if (weight > boost) { // Adjust for having up to the first 4 characters in common let j = (minv >= 4) ? 4 : minv; let i; for (i = 0; (i < j) && a[i] === b[i]; i++) { } if (i) { weight += i * 0.1 * (1.0 - weight) }; // Adjust for long strings. // After agreeing beginning chars, at least two more must agree // and the agreeing characters must be more than half of the // remaining characters. if (minv > 4 && Num_com > i + 1 && 2 * Num_com >= minv + i) { weight += (1 - weight) * ((Num_com - i - 1) / (a_len * b_len - i*2 + 2)); } } return weight; }, showDistance (s1, s2) { console.log(`Distance: ${s1} / ${s2} = ${this.distanceDamerauLevenshtein(s1, s2)})`); }, // Suffix only defineSuffixCode: function (sFlex, sStem) { |
︙ |
Modified graphspell/char_player.py from [e2b351100d] to [24cf38a4cb].
1 2 3 4 5 6 7 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 | + + + - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + | """ List of similar chars useful for suggestion mechanism """ dDistanceBetweenChars = { # dDistanceBetweenChars: # - with Jaro-Winkler, values between 1 and 10 # - with Damerau-Levenshtein, values / 10 (between 0 and 1: 0.1, 0.2 ... 0.9) |
︙ | |||
313 314 315 316 317 318 319 | 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 | - - + + + + | # End of word dFinal1 = { "a": ("as", "at", "ant", "ah"), "A": ("AS", "AT", "ANT", "AH"), |
︙ |
Modified graphspell/ibdawg.py from [d16ed0d683] to [d672255b46].
︙ | |||
11 12 13 14 15 16 17 18 19 20 21 22 23 24 | 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | + | import re from functools import wraps import time import json import binascii import importlib from collections import OrderedDict from math import floor #import logging #logging.basicConfig(filename="suggestions.log", level=logging.DEBUG) from . import str_transform as st from . import char_player as cp from .echo import echo |
︙ | |||
36 37 38 39 40 41 42 | 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 | - + + - - - + + + + + + + + - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + - + - - - - - - - + + + - - + - - - - + + + + + + + - + | return result return wrapper class SuggResult: """Structure for storing, classifying and filtering suggestions""" |
︙ | |||
116 117 118 119 120 121 122 123 124 125 | 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 | + + + + + + + + + + + + + + + + + + + + - - - - - - - - - - - - - - - - - - - - - - - | else: raise OSError("# Error. Unknown file type: "+source) else: self._initJSON(source) self.sFileName = source if isinstance(source, str) else "[None]" # Performance trick: # Instead of converting bytes to integers each times we parse the binary dictionary, # we do it once, then parse the array nAcc = 0 byBuffer = b"" lTemp = [] nDivisor = (self.nBytesArc + self.nBytesNodeAddress) / 2 for i in range(0, len(self.byDic)): byBuffer += self.byDic[i:i+1] if nAcc == (self.nBytesArc - 1): lTemp.append(int.from_bytes(byBuffer, byteorder="big")) byBuffer = b"" elif nAcc == (self.nBytesArc + self.nBytesNodeAddress - 1): lTemp.append(round(int.from_bytes(byBuffer, byteorder="big") / nDivisor)) byBuffer = b"" nAcc = -1 nAcc = nAcc + 1 self.byDic = lTemp; # masks self._arcMask = (2 ** ((self.nBytesArc * 8) - 3)) - 1 self._finalNodeMask = 1 << ((self.nBytesArc * 8) - 1) self._lastArcMask = 1 << ((self.nBytesArc * 8) - 2) |
︙ | |||
200 201 202 203 204 205 206 | 206 207 208 209 210 211 212 213 214 215 216 217 218 219 | - | self.cStemming = l.pop(0) self.nTag = self.nArcVal - self.nChar - self.nAff # <dChar> to get the value of an arc, <dCharVal> to get the char of an arc with its value self.dChar = {} for i in range(1, self.nChar+1): self.dChar[self.lArcVal[i]] = i self.dCharVal = { v: k for k, v in self.dChar.items() } |
︙ | |||
244 245 246 247 248 249 250 | 249 250 251 252 253 254 255 256 257 258 259 260 261 262 | - | "nNode": self.nNode, "nArc": self.nArc, "nArcVal": self.nArcVal, "lArcVal": self.lArcVal, "nCompressionMethod": self.nCompressionMethod, "nBytesArc": self.nBytesArc, "nBytesNodeAddress": self.nBytesNodeAddress, |
︙ | |||
296 297 298 299 300 301 302 | 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 | - + - + - + - + - + + - + | iAddr = 0 for c in sWord: if c not in self.dChar: return False iAddr = self._lookupArcNode(self.dChar[c], iAddr) if iAddr is None: return False |
︙ | |||
352 353 354 355 356 357 358 | 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 | - + | sWord1, sWord2 = sWord.split(cSplitter, 1) if self.isValid(sWord1) and self.isValid(sWord2): oSuggResult.addSugg(sWord1+" "+sWord2) def _suggest (self, oSuggResult, sRemain, nMaxSwitch=0, nMaxDel=0, nMaxHardRepl=0, nMaxJump=0, nDist=0, nDeep=0, iAddr=0, sNewWord="", bAvoidLoop=False): # recursive function #logging.info((nDeep * " ") + sNewWord + ":" + sRemain) |
︙ | |||
419 420 421 422 423 424 425 | 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 | - + | yield (self.dCharVal[nVal], jAddr) def _getTails (self, iAddr, sTail="", n=2): "return a list of suffixes ending at a distance of <n> from <iAddr>" aTails = set() for nVal, jAddr in self._getArcs(iAddr): if nVal <= self.nChar: |
︙ | |||
467 468 469 470 471 472 473 | 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 | - + - - - - - + - + - + - + - + - + - - + + - + - - + + - - + + - + - + - - + + - + - + - - + + - + - + - + - - - + + + - + - + - - + + - + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + - - - - - - - - - - - - - - - - - - - - - - - - - - - + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - | if sFlexPattern: zFlexPattern = re.compile(sFlexPattern) if sTagsPattern: zTagsPattern = re.compile(sTagsPattern) except re.error: print("# Error in regex pattern") traceback.print_exc() |
Modified graphspell/str_transform.py from [e27e70d363] to [98c57fa9ba].
1 2 3 4 5 6 7 8 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | + - + | """ Operations on strings: - calculate distance between two strings - transform strings with transformation codes """ import unicodedata import re |
︙ | |||
48 49 50 51 52 53 54 55 56 57 58 59 60 61 | 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 | + + + + + | sWord = sWord.lower().translate(_xTransCharsForSimplification) sNewWord = "" for i, c in enumerate(sWord, 1): if c != sWord[i:i+1] or (c == 'e' and sWord[i:i+2] != "ee"): # exception for <e> to avoid confusion between crée / créai sNewWord += c return sNewWord.replace("eau", "o").replace("au", "o").replace("ai", "éi").replace("ei", "é").replace("ph", "f") def cleanWord (sWord): "remove letters repeated more than 2 times" return re.sub("(.)\\1{2,}", '\\1\\1', sWord) _xTransNumbersToExponent = str.maketrans({ "0": "⁰", "1": "¹", "2": "²", "3": "³", "4": "⁴", "5": "⁵", "6": "⁶", "7": "⁷", "8": "⁸", "9": "⁹" }) def numbersToExponent (sWord): "convert numeral chars to exponant chars" |
︙ | |||
102 103 104 105 106 107 108 109 110 111 112 113 114 115 | 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 | + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + | d[i, j-1] + 1, # Insertion d[i-1, j-1] + nCost, # Substitution ) if i and j and s1[i] == s2[j-1] and s1[i-1] == s2[j]: d[i, j] = min(d[i, j], d[i-2, j-2] + nCost) # Transposition return d[nLen1-1, nLen2-1] def distanceJaroWinkler (a, b, boost = .666): # https://github.com/thsig/jaro-winkler-JS #if (a == b): return 1.0 a_len = len(a) b_len = len(b) nMax = max(a_len, b_len) a_flag = [None for _ in range(nMax)] b_flag = [None for _ in range(nMax)] search_range = (max(a_len, b_len) // 2) - 1 minv = min(a_len, b_len) # Looking only within the search range, count and flag the matched pairs. Num_com = 0 yl1 = b_len - 1 for i in range(a_len): lowlim = i - search_range if i >= search_range else 0 hilim = i + search_range if (i + search_range) <= yl1 else yl1 for j in range(lowlim, hilim+1): if b_flag[j] != 1 and a[j:j+1] == b[i:i+1]: a_flag[j] = 1 b_flag[i] = 1 Num_com += 1 break # Return if no characters in common if Num_com == 0: return 0.0 # Count the number of transpositions k = 0 N_trans = 0 for i in range(a_len): if a_flag[i] == 1: for j in range(k, b_len): if b_flag[j] == 1: k = j + 1 break if a[i] != b[j]: N_trans += 1 N_trans = N_trans // 2 # Adjust for similarities in nonmatched characters N_simi = 0 if minv > Num_com: for i in range(a_len): if not a_flag[i]: for j in range(b_len): if not b_flag[j]: if a[i] in dDistanceBetweenChars and b[j] in dDistanceBetweenChars[a[i]]: N_simi += dDistanceBetweenChars[a[i]][b[j]] b_flag[j] = 2 break Num_sim = (N_simi / 10.0) + Num_com # Main weight computation weight = Num_sim / a_len + Num_sim / b_len + (Num_com - N_trans) / Num_com weight = weight / 3 # Continue to boost the weight if the strings are similar if weight > boost: # Adjust for having up to the first 4 characters in common j = 4 if minv >= 4 else minv i = 0 while i < j and a[i] == b[i]: i += 1 if i: weight += i * 0.1 * (1.0 - weight) # Adjust for long strings. # After agreeing beginning chars, at least two more must agree # and the agreeing characters must be more than half of the # remaining characters. if minv > 4 and Num_com > i + 1 and 2 * Num_com >= minv + i: weight += (1 - weight) * ((Num_com - i - 1) / (a_len * b_len - i*2 + 2)) return weight def distanceSift4 (s1, s2, nMaxOffset=5): "implementation of general Sift4." # https://siderite.blogspot.com/2014/11/super-fast-and-accurate-string-distance.html if not s1: return len(s2) if not s2: |
︙ |