Tensorflow Text Simularity

by Cody Bennett

HTML

<script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs"></script>
<script src="https://cdn.jsdelivr.net/npm/@tensorflow-models/universal-sentence-encoder"></script>
<script src="https://cdn.plot.ly/plotly-latest.min.js"></script>
<textarea id="input_sentences" rows="10"></textarea>
<label for="input_sentences">Input text</label>

<label for="input_threshold">Threshold</label>
<input type="text" id="input_threshold" placeholder="example: 0.5" value="0.5">

<button onclick="onClickAnalyzeSentences()">Analyze Sentences</button>

<div id="output_resultshtml"></div>
<div id="div_heatmap" style="width:100%;"></div>

CSS

html,
body {
  display: flex;
  flex-direction: column;
  gap: 16px;
  height: 100vh;
  margin: 0;
  padding: 0;
  padding: 32px;
  width: 100vw;
}

textarea,
input,
button {
  max-width: 500px;
}

JavaScript

list_sentences = [];
plotly_heatmap = {
  data: [],
  layout: {},
};
input_sentences = '';
input_threshold = 0.5;
output_resultshtml = '';
analyzing_text = true;

input_sentences =
`Will it snow tomorrow?
Recently a lot of hurricanes have hit the US
Global warming is real

An apple a day, keeps the doctors away
Eating strawberries is healthy

what is your age?
How old are you?
How are you?

The dog bit Johnny
Johnny bit the dog

The cat ate the mouse
The mouse ate the cat
`;

document.getElementById('input_sentences').value = input_sentences;
document.getElementById('input_threshold').value = input_threshold;

async function onClickAnalyzeSentences() {
  const list_sentences = [];
  const input_sentences = document.getElementById('input_sentences').value.split('\n');

  for (const i in input_sentences) {
    if (input_sentences[i].length) {
      list_sentences.push(input_sentences[i]);
    }
  }

  this.get_similarity(list_sentences);
}

function get_embeddings(list_sentences, callback) {
  use.load().then(model => {
    model.embed(list_sentences).then(embeddings => {
    	console.log(embeddings.arraySync()[0].length);
      callback(embeddings);
    });
  });
}

function dot(a, b) {
  const hasOwnProperty = Object.prototype.hasOwnProperty;

  let sum = 0;
  for (const key in a) {
    if (hasOwnProperty.call(a, key) && hasOwnProperty.call(b, key)) {
      sum += a[key] * b[key];
    }
  }

  return sum;
}

function similarity(a, b) {
  const magnitudeA = Math.sqrt(this.dot(a, a));
  const magnitudeB = Math.sqrt(this.dot(b, b));

  const magnitudes = magnitudeA && magnitudeB;

  return magnitudes && this.dot(a, b) / (magnitudeA * magnitudeB);
}

function cosine_similarity_matrix(matrix) {
  let cosine_similarity_matrix = [];

  for (let i = 0; i < matrix.length; i++) {
    let row = [];
    for (let j = 0; j < i; j++) {
      row.push(cosine_similarity_matrix[j][i]);
    }

    row.push(1);

    for (let j = i + 1; j < matrix.length; j++) {
     ...