2021-06-15 00:49:18 -07:00
---
id: 5a23c84252665b21eecc7ec2
2022-02-18 00:29:34 +05:30
title: Distanza di Jaro
2021-06-15 00:49:18 -07:00
challengeType: 5
forumTopicId: 302292
dashedName: jaro-distance
---
# --description--
2022-02-18 00:29:34 +05:30
La distanza di Jaro è una misura di similitudine tra due stringhe. Maggiore la distanza di Jaro per due stringhe, più simili sono. Il punteggio è normalizzato affinché `0` inidichi nessuna similarità e `1` che sono identiche.
2021-06-15 00:49:18 -07:00
2022-02-18 00:29:34 +05:30
**Definizione**
2021-06-15 00:49:18 -07:00
2022-02-18 00:29:34 +05:30
La distanza di Jaro \\( d_j \\) di due stringhe date \\(s_1\\) e \\(s_2\\) è
2021-06-15 00:49:18 -07:00
\\begin{align}d_j = \\begin{cases}0& & \\text{if }m=0 \\\\\\\\{\\frac {1}{3}}\\left({\\frac {m}{|s\_{1}|}}+{\\frac {m}{|s\_{2}|}}+{\\frac {m-t}{m}}\\right)& & \\text{otherwise}\\end{cases}\\end{align}
2022-02-18 00:29:34 +05:30
Dove:
2021-06-15 00:49:18 -07:00
< ul >
2022-02-18 00:29:34 +05:30
< li > \(m\) è il numero di < i > caratteri combacianti</ i > ;</ li >
< li > \(t\) è metà del numero di < i > trasposizioni</ i > .</ li >
2021-06-15 00:49:18 -07:00
< / ul >
2022-02-18 00:29:34 +05:30
Due caratteri da \\(s_1\\) e \\(s_2\\) rispettivamente, sono considerati *combacianti* solo se sono uguali e non più lontani di \\(\\left\\lfloor\\frac{\\max(|s_1|,|s_2|)}{2}\\right\\rfloor-1\\).
2021-06-15 00:49:18 -07:00
2022-02-18 00:29:34 +05:30
Ogni carattere di \\(s_1\\) è comparato con tutti i caratteri combacianti in \\(s_2\\) . Il numero di caratteri combacianti (ma in differente ordine di sequenza) diviso 2 definisce il numero di *trasposizioni* .
2021-06-15 00:49:18 -07:00
2022-02-18 00:29:34 +05:30
**Esempio**
2021-06-15 00:49:18 -07:00
2022-02-18 00:29:34 +05:30
Date le stringhe \\(s_1\\) *DWAYNE* e \\(s_2\\) *DUANE* troviamo:
2021-06-15 00:49:18 -07:00
< ul >
< li > \(m = 4\)</ li >
< li > \(|s_1| = 6\)</ li >
< li > \(|s_2| = 5\)</ li >
< li > \(t = 0\)</ li >
< / ul >
2022-02-18 00:29:34 +05:30
Troviamo un punteggio Jaro di: \\(d_j = \\frac{1}{3}\\left(\\frac{4}{6} + \\frac{4}{5} + \\frac{4-0}{4}\\right) = 0.822\\).
2021-06-15 00:49:18 -07:00
# --instructions--
2022-02-18 00:29:34 +05:30
Scrivi una funzione che prende due stringhe come parametri e restituisce l'associata distanza di Jaro.
2021-06-15 00:49:18 -07:00
# --hints--
2022-02-18 00:29:34 +05:30
`jaro` dovrebbe essere una funzione.
2021-06-15 00:49:18 -07:00
```js
assert(typeof jaro == 'function');
```
2022-02-18 00:29:34 +05:30
`jaro("MARTHA", "MARHTA")` dovrebbe restituire un numero.
2021-06-15 00:49:18 -07:00
```js
assert(typeof jaro('MARTHA', 'MARHTA') == 'number');
```
2022-02-18 00:29:34 +05:30
`jaro("MARTHA", "MARHTA")` dovrebbe restituire `0.9444444444444445` .
2021-06-15 00:49:18 -07:00
```js
assert.equal(jaro('MARTHA', 'MARHTA'), 0.9444444444444445);
```
2022-02-18 00:29:34 +05:30
`jaro("DIXON", "DICKSONX")` dovrebbe restituire `0.7666666666666666` .
2021-06-15 00:49:18 -07:00
```js
assert.equal(jaro('DIXON', 'DICKSONX'), 0.7666666666666666);
```
2022-02-18 00:29:34 +05:30
`jaro("JELLYFISH", "SMELLYFISH")` dovrebbe restituire `0.8962962962962964` .
2021-06-15 00:49:18 -07:00
```js
assert.equal(jaro('JELLYFISH', 'SMELLYFISH'), 0.8962962962962964);
```
2022-02-18 00:29:34 +05:30
`jaro("HELLOS", "CHELLO")` dovrebbe restituire `0.888888888888889` .
2021-06-15 00:49:18 -07:00
```js
assert.equal(jaro('HELLOS', 'CHELLO'), 0.888888888888889);
```
2022-02-18 00:29:34 +05:30
`jaro("ABCD", "BCDA")` dovrebbe restituire `0.8333333333333334` .
2021-06-15 00:49:18 -07:00
```js
assert.equal(jaro('ABCD', 'BCDA'), 0.8333333333333334);
```
# --seed--
## --seed-contents--
```js
function jaro(s, t) {
}
```
# --solutions--
```js
function jaro(s, t) {
var s_len = s.length;
var t_len = t.length;
if (s_len == 0 & & t_len == 0) return 1;
var match_distance = Math.max(s_len, t_len) / 2 - 1;
var s_matches = new Array(s_len);
var t_matches = new Array(t_len);
var matches = 0;
var transpositions = 0;
for (var i = 0; i < s_len ; i + + ) {
var start = Math.max(0, i - match_distance);
var end = Math.min(i + match_distance + 1, t_len);
for (var j = start; j < end ; j + + ) {
if (t_matches[j]) continue;
if (s.charAt(i) != t.charAt(j)) continue;
s_matches[i] = true;
t_matches[j] = true;
matches++;
break;
}
}
if (matches == 0) return 0;
var k = 0;
for (var i = 0; i < s_len ; i + + ) {
if (!s_matches[i]) continue;
while (!t_matches[k]) k++;
if (s.charAt(i) != t.charAt(k)) transpositions++;
k++;
}
return ((matches / s_len) +
(matches / t_len) +
((matches - transpositions / 2.0) / matches)) / 3.0;
}
```