levenshteinDistance does not support unicode characters

Aperta
#307 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
2/5
Tempo stimato
1-3 ore
Idoneità per principianti
45/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Ferma
Stack tecnologico
javascript
Ambito
devtools

Direzione di ricerca

Start by locating the levenshteinDistance implementation and its existing tests. Reproduce the astral Unicode case from the issue, then add regression coverage for it and verify that the distance for '\u{1f431}' versus an empty string is 1.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

Details:

levenshteinDistance does not support unicode characters outside of the BMP range properly.
Step to reproduce: expect(levenshteinDistance('\u{1f431}', '')).toBe(1) // got 2

Fix

I can issue a PR with the fix https://github.com/dubzzz/javascript-algorithms/commit/856ba360134956cca2bed165929a4d4409537201

How did I find it?

Thanks to property based testing framework fast-check.
The property was the following:

import fc from 'fast-check';

fc.assert(
    fc.property(
      fc.fullUnicodeString(), fc.fullUnicodeString(),
      fc.fullUnicodeString(), fc.fullUnicodeString(),
      fc.fullUnicodeString(),
      (aBegin, aEnd, bBegin, bEnd, common) =>
        levenshteinDistance(aBegin + common + aEnd, bBegin + common + bEnd)
          <= Math.max([...aBegin].length, [...bBegin].length) + Math.max([...aEnd].length, [...bEnd].length)
    )
  )

Or:

for any a and b - strings
such that a = aBegin + common + aEnd and b = bBegin + common + bEnd
levenshteinDistance(a, b) is at most max(numChars(aBegin), numChars(bBegin)) + max(numChars(aEnd), numChars(bEnd))

Lingua principale
JavaScript
Stelle
197k
Fork
31k
Metriche di merge delle PR
Nessuna PR unita negli ultimi 30g

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di trekhleb/javascript-algorithms

Tutte le issue di trekhleb/javascript-algorithms

Issue simili

Altre issue su JavaScript

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.