lunes, 24 de julio de 2023

Drag a txt file to Chrome. Two columns in txt file, tab separated, have values to Levenstein comparison (all from 1st col to all 2nd col)

// Function to calculate Levenshtein distance between two strings
function levenshteinDistance(str1, str2) {
  // Create a 2D array to store the distances
  const dp = Array.from({ length: str1.length + 1 }, () => Array(str2.length + 1).fill(0));

  // Initialize the array with base cases
  for (let i = 0; i <= str1.length; i++) {
    for (let j = 0; j <= str2.length; j++) {
      if (i === 0) dp[i][j] = j;
      else if (j === 0) dp[i][j] = i;
      else {
        const cost = str1[i - 1] === str2[j - 1] ? 0 : 1;
        dp[i][j] = Math.min(dp[i - 1][j] + 1, dp[i][j - 1] + 1, dp[i - 1][j - 1] + cost);
      }
    }
  }

  return dp[str1.length][str2.length];
}


// Main function to calculate Levenshtein distances and copy results to clipboard
function calculateLevenshteinDistances() {
  // Get the content of the current tab
  const result = document.evaluate('/html/body/pre', document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null);
  const preElement = result.singleNodeValue;
  const content = preElement ? preElement.textContent.trim() : '';

  const rows = content.split('\n');

  // Assuming the headers are present in the first row, you can extract them like this:
  const [header1, header2] = rows.shift().split('\t');

  // Use the Clipboard API to copy the header row to clipboard
  let clipboardData = `${header1}\t${header2}\tLevenshteinDistancesCalculated\n`;

  // Use the Clipboard API to append each row of data to clipboard
  for (let i = 0; i < rows.length; i++) {
    const [column1Value, column2Value] = rows[i].split('\t');

    if (column1Value !== '-' && column2Value !== '-') {
      for (let j = 0; j < rows.length; j++) {
        const [column1ValueToCompare, column2ValueToCompare] = rows[j].split('\t');

        if (column1ValueToCompare !== '-' && column2ValueToCompare !== '-') {
          const distance = levenshteinDistance(column1Value, column2ValueToCompare);
          clipboardData += `${column1Value}\t${column2ValueToCompare}\t${distance}\n`;
        }
      }
    }

    // Send a progress message to the console after each row is processed
    console.log(`Processing row ${i + 1} of ${rows.length}`);
  }

  // Copy the final clipboardData to the clipboard
  copyToClipboard(clipboardData);

  // Send completion message to the console
  console.log('Levenshtein distance calculations completed.');
}

// Function to copy text to clipboard
function copyToClipboard(text) {
  const el = document.createElement('textarea');
  el.value = text;
  document.body.appendChild(el);
  el.select();
  document.execCommand('copy');
  document.body.removeChild(el);

  console.log('Data copied to clipboard.');
}

// Call the function to execute the code
calculateLevenshteinDistances();


No hay comentarios:

Publicar un comentario