Created
July 17, 2023 15:49
-
-
Save netdur/a777f75fb70e0abc19c407c2ff7f9e0c to your computer and use it in GitHub Desktop.
GZIP for text similarity
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<body> | |
<script src="https://cdnjs.cloudflare.com/ajax/libs/pako/2.0.3/pako.min.js"></script> | |
<script> | |
// Training set | |
const trainingSet = [ | |
["How can I create a new project?", "To create a new project, go to the dashboard and click on 'New Project'. Then, fill out the details and click 'Save'."], | |
["How can I invite team members to my project?", "To invite team members to your project, open the project and click on 'Invite Members'. Enter their email addresses and click 'Send Invites'."], | |
["Can I assign tasks to team members?", "Yes, you can assign tasks to team members. To do this, create a new task and select a team member from the 'Assign To' dropdown."], | |
["What happens when a task is completed?", "When a task is completed, it should be marked as 'Complete'. It will then be moved to the 'Completed Tasks' section."] | |
]; | |
// Test set | |
const testSet = ["How do I start a new project?", "How can I delegate tasks to others in my team?", "What do I do when I finish a task?"]; | |
// KNN algorithm | |
testSet.forEach(testQuestion => { | |
let distances = trainingSet.map(([trainingQuestion, trainingAnswer]) => { | |
const Cx1 = pako.deflate(testQuestion).length; | |
const Cx2 = pako.deflate(trainingQuestion).length; | |
const Cx1x2 = pako.deflate(testQuestion + trainingQuestion).length; | |
const ncd = (Cx1x2 - Math.min(Cx1, Cx2)) / Math.max(Cx1, Cx2); | |
return {ncd, trainingAnswer}; | |
}); | |
distances.sort((a, b) => a.ncd - b.ncd); | |
console.log(`Best matched answer for '${testQuestion}' is '${distances[0].trainingAnswer}'`); | |
}); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment