From 3705d0c739216059c631370a366ea3fa7fa80c71 Mon Sep 17 00:00:00 2001
From: hchiam
Date: Thu, 29 Sep 2022 22:23:53 -0600
Subject: [PATCH] closer to CodePen demo
---
index.html | 22 ++++++++++---------
index.js | 62 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 74 insertions(+), 10 deletions(-)
diff --git a/index.html b/index.html
index 87cc200..f7a794b 100644
--- a/index.html
+++ b/index.html
@@ -20,13 +20,7 @@
Sentence 1:
-
+
Sentence 2:
Sentence 2:
-
+
Similarity:
This tool could possibly be used to check whether a free-form answer
closely matches the expected answer in meaning. For best results, you
probably should constrain responses to short sentences (i.e. short
- answer questions only).
+ answer questions only). For faster results, you might want to call
+ back-end code that runs a Python/PyTorch script that has been
+ pre-compiled to C. Reference for this demo:
+ https://github.com/hchiam/text-similarity-test
diff --git a/index.js b/index.js
index 5eaf739..1a173af 100644
--- a/index.js
+++ b/index.js
@@ -21,3 +21,65 @@ function showOutput(similarity) {
function get2Decimals(number) {
return Math.round(number * 100) / 100;
}
+
+// /**
+// * References:
+// *
+// * https://github.com/tensorflow/tfjs-models/tree/master/universal-sentence-encoder
+// * https://towardsdatascience.com/how-to-build-a-textual-similarity-analysis-web-app-aa3139d4fb71
+// * https://github.com/jinglescode/demos/tree/master/src/app/components/nlp-sentence-encoder
+// * https://towardsdatascience.com/how-to-measure-distances-in-machine-learning-13a396aa34ce
+// * https://en.wikipedia.org/wiki/Cosine_similarity
+// *
+// * */
+
+// // require("@tensorflow/tfjs-node");
+// // const use = require("@tensorflow-models/universal-sentence-encoder");
+
+// // const sentence1 = "How's it going?";
+// // const sentence2 = "How are you?";
+// // useModel(sentence1, sentence2, output);
+// function useModel(sentence1, sentence2, callback) {
+// // uses Universal Sentence Encoder (U.S.E.):
+// use.load().then((model) => {
+// embedSentences(model, sentence1, sentence2, callback);
+// });
+// }
+
+// function embedSentences(model, sentence1, sentence2, callback) {
+// const sentences = [sentence1, sentence2];
+// model.embed(sentences).then((embeddings) => {
+// const embeds = embeddings.arraySync();
+// const sentence1Embedding = embeds[0];
+// const sentence2Embedding = embeds[1];
+// getSimilarityPercent(sentence1Embedding, sentence2Embedding, callback);
+// });
+// }
+
+// function getSimilarityPercent(embed1, embed2, callback) {
+// const similarity = cosineSimilarity(embed1, embed2);
+// // cosine similarity -> % when doing text comparison, since cannot have -ve term frequencies: https://en.wikipedia.org/wiki/Cosine_similarity
+// if (callback) callback(similarity);
+// return similarity;
+// }
+
+// function cosineSimilarity(a, b) {
+// // https://towardsdatascience.com/how-to-build-a-textual-similarity-analysis-web-app-aa3139d4fb71
+
+// const magnitudeA = Math.sqrt(dotProduct(a, a));
+// const magnitudeB = Math.sqrt(dotProduct(b, b));
+// if (magnitudeA && magnitudeB) {
+// // https://towardsdatascience.com/how-to-measure-distances-in-machine-learning-13a396aa34ce
+// return dotProduct(a, b) / (magnitudeA * magnitudeB);
+// } else {
+// return 0;
+// }
+// }
+
+// function dotProduct(a, b) {
+// let sum = 0;
+// for (let i = 0; i < a.length; i++) {
+// sum += a[i] * b[i];
+// }
+// return sum;
+// }