jueves, 27 de julio de 2023
miércoles, 26 de julio de 2023
Levenstein a EXCEL
Function Levenshtein(ByVal string1 As String, ByVal string2 As String) As Long
Dim i As Long, j As Long
Dim string1_length As Long
Dim string2_length As Long
Dim distance() As Long
string1_length = Len(string1)
string2_length = Len(string2)
ReDim distance(string1_length, string2_length)
For i = 0 To string1_length
distance(i, 0) = i
Next
For j = 0 To string2_length
distance(0, j) = j
Next
For i = 1 To string1_length
For j = 1 To string2_length
If Asc(Mid$(string1, i, 1)) = Asc(Mid$(string2, j, 1)) Then
distance(i, j) = distance(i - 1, j - 1)
Else
distance(i, j) = Application.WorksheetFunction.Min _
(distance(i - 1, j) + 1, _
distance(i, j - 1) + 1, _
distance(i - 1, j - 1) + 1)
End If
Next
Next
Levenshtein = distance(string1_length, string2_length)
End Function
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();
lunes, 17 de julio de 2023
Get a list of unique words sorted alphabetically from drag txt file
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() : '';
// Split the content into an array of words
const words = content.split(/\s+/);
// Create a Set to store unique words
const uniqueWords = new Set(words);
// Convert the Set back to an array and sort it alphabetically
const sortedWords = Array.from(uniqueWords).sort();
console.log('Content:', content);
console.log('Word Count:', words.length);
console.log('Unique Words:', sortedWords);
// Open a new tab with the sorted words
const newTab = window.open('');
newTab.document.write('<html><head>');
newTab.document.write('<style>body { font-family: Consolas; font-size: 10pt; }</style>');
newTab.document.write('</head><body>');
newTab.document.write('<h1>Sorted Words</h1>');
newTab.document.write('<ul>');
sortedWords.forEach((word) => {
newTab.document.write(`<li>${word}</li>`);
});
newTab.document.write('</ul>');
newTab.document.write('</body></html>');
newTab.document.close();
Drag the txt file in Chrome and refer to it as Content object
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() : '';
console.log('Content:', content);