forked from niceplaces/website
-
Notifications
You must be signed in to change notification settings - Fork 0
/
search.html
90 lines (82 loc) · 3.17 KB
/
search.html
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
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Ricerca</title>
</head>
<body>
Nome: <input id="name" type="text" name="name" /><br>
<div id="list"></div>
<script src="lib/js/jquery-3.2.1.min.js"></script>
<script type="application/javascript">
$(document).ready(function () {
let base_url = "https://www.niceplaces.it/";
let url = base_url + "data/query.php?version=v2&mode=debug&p1=getnearestplaces&p2=44.494533&p3=11.3858214&p4=1000";
console.log(url);
let places = [];
$.ajax({
url: url,
type: "GET",
contentType: "application/json",
success: function (result) {
console.log(result);
places = result;
for (let i = 0; i < result.length; i++){
$("#list").append(result[i].name + "<br/>")
}
},
error: function (error) {
console.log(url);
console.log(error);
}
});
$("#name").keyup(function () {
let val = $("#name").val().toLowerCase();
places.sort(function(a, b) {
return levenshteinDistance(val, a.name.toLowerCase()) - levenshteinDistance(val, b.name.toLowerCase());
});
$("#list").empty();
for (let i = 0; i < places.length; i++){
if (places[i].name.toLowerCase().includes(val)){
$("#list").append(places[i].name + "<br/>");
}
}
});
});
// https://github.com/trekhleb/javascript-algorithms/blob/master/src/algorithms/string/levenshtein-distance/levenshteinDistance.js
/**
* @param {string} a
* @param {string} b
* @return {number}
*/
function levenshteinDistance(a, b) {
// Create empty edit distance matrix for all possible modifications of
// substrings of a to substrings of b.
const distanceMatrix = Array(b.length + 1).fill(null).map(() => Array(a.length + 1).fill(null));
// Fill the first row of the matrix.
// If this is first row then we're transforming empty string to a.
// In this case the number of transformations equals to size of a substring.
for (let i = 0; i <= a.length; i += 1) {
distanceMatrix[0][i] = i;
}
// Fill the first column of the matrix.
// If this is first column then we're transforming empty string to b.
// In this case the number of transformations equals to size of b substring.
for (let j = 0; j <= b.length; j += 1) {
distanceMatrix[j][0] = j;
}
for (let j = 1; j <= b.length; j += 1) {
for (let i = 1; i <= a.length; i += 1) {
const indicator = a[i - 1] === b[j - 1] ? 0 : 1;
distanceMatrix[j][i] = Math.min(
distanceMatrix[j][i - 1] + 1, // deletion
distanceMatrix[j - 1][i] + 1, // insertion
distanceMatrix[j - 1][i - 1] + indicator, // substitution
);
}
}
return distanceMatrix[b.length][a.length];
}
</script>
</body>
</html>