-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsearch.js
118 lines (100 loc) · 3.49 KB
/
search.js
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
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
// Sample database
const database = [
"Joe Biden",
"Kamala Harris",
"Donald Trump",
"JD Vance",
"Joe Manchin",
"Tim Walz",
"Chuck Schumer",
"Mitch McConnell",
"Ed Gainey"
];
// Levenshtein distance function
function levenshtein(a, b) {
const matrix = [];
for (let i = 0; i <= b.length; i++) {
matrix[i] = [i];
}
for (let j = 0; j <= a.length; j++) {
matrix[0][j] = j;
}
for (let i = 1; i <= b.length; i++) {
for (let j = 1; j <= a.length; j++) {
if (b.charAt(i - 1) === a.charAt(j - 1)) {
matrix[i][j] = matrix[i - 1][j - 1];
} else {
matrix[i][j] = Math.min(
matrix[i - 1][j - 1] + 1, // substitution
Math.min(
matrix[i][j - 1] + 1, // insertion
matrix[i - 1][j] + 1 // deletion
)
);
}
}
}
return matrix[b.length][a.length];
}
// Close-results algorithm
function closeResults(input, topX) {
const results = database.map(entry => ({
entry,
distance: levenshtein(input, entry),
}));
results.sort((a, b) => a.distance - b.distance);
return results.slice(0, topX);
}
// Event listener for input
const searchInput = document.getElementById('searchInput');
const dropdown = document.getElementById('dropdownMenu');
function loadPoliticianDescription(selectedPolitician){
// Create a FormData object to send data
const formData = new FormData();
formData.append('politician', selectedPolitician);
// Send the POST request using Fetch API
fetch('https://localhost:5500/description.php', {
method: 'POST',
body: formData,
})
.then(response => response.text()) // Process the response
.then(data => {
console.log(data); // Handle the response data
})
.catch(error => {
console.error('Error:', error); // Handle any errors
});
}
searchInput.addEventListener('input', () => {
const inputValue = searchInput.value;
dropdown.innerHTML = ''; // Clear previous results
if (inputValue.length > 0) {
const results = closeResults(inputValue, 5); // Get top 5 results
results.forEach(result => {
const entryDiv = document.createElement('div');
entryDiv.className = 'entry';
entryDiv.innerText = result.entry;
entryDiv.onclick = () => {
let fullName = result.entry;
let fileName = fullName.toLowerCase().replace(/\s+/g, '') + ".php";
searchInput.value = result.entry; // Set input value to selected entry
dropdown.style.display = 'none'; // Hide dropdown
localStorage.setItem('PoliticianName', result.entry);
/*loadPoliticianDescription(result.entry);*/
window.location.href=fileName;
};
dropdown.appendChild(entryDiv);
});
if (results.length > 0) {
dropdown.style.display = 'block'; // Show dropdown if there are results
} else {
dropdown.style.display = 'none'; // Hide dropdown if no results
}
} else {
dropdown.style.display = 'none'; // Hide dropdown if input is empty
}
});
/*document.getElementById('searchForm').addEventListener('submit', function(event) {
event.preventDefault(); // Prevent the default form submission
loadPoliticianDescription(searchInput.value);
});*/