Skip to content

Instantly share code, notes, and snippets.

@abdasis
Created June 25, 2024 00:30
Show Gist options
  • Save abdasis/458609cf6ce518d1d7fe9145cb68dd66 to your computer and use it in GitHub Desktop.
Save abdasis/458609cf6ce518d1d7fe9145cb68dd66 to your computer and use it in GitHub Desktop.
Cek Khodam dengan String Matching
// Import modul string similarity (anda bisa gunakan library seperti 'string-similarity')
const stringSimilarity = require('string-similarity');
// Contoh data khodam
const khodamList = [
{
name: "Macan Putih",
keterangan: "sangat kuat dan sangat berwibawa"
}
];
// Contoh data nama
const nameList = [
{
name: "Abd. Asis",
tanggal_lahir: "17-11-2001"
}
];
// Fungsi untuk mencocokkan nama dan tanggal lahir dengan khodam
function matchKhodam(nameList, khodamList) {
const matchedResults = [];
nameList.forEach(person => {
let bestMatch = null;
let highestScore = 0;
khodamList.forEach(khodam => {
const similarity = stringSimilarity.compareTwoStrings(person.name, khodam.keterangan);
if (similarity > highestScore) {
highestScore = similarity;
bestMatch = khodam;
}
});
matchedResults.push({
person: person,
bestMatch: bestMatch,
similarityScore: highestScore
});
});
return matchedResults;
}
// Memanggil fungsi pencocokan
const results = matchKhodam(nameList, khodamList);
// Menampilkan hasil pencocokan
results.forEach(result => {
console.log(`Nama: ${result.person.name}, Tanggal Lahir: ${result.person.tanggal_lahir}`);
console.log(`Khodam: ${result.bestMatch ? result.bestMatch.name : 'Tidak ditemukan'}, Kesamaan: ${result.similarityScore}`);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment