Myers Diff с пользовательской функцией стоимостиJavascript

Форум по Javascript
Ответить
Anonymous
 Myers Diff с пользовательской функцией стоимости

Сообщение Anonymous »

Myers Diff Алгоритм обрабатывает вставку и удаление в равной степени по той же стоимости и обрабатывает равенство по нулевой стоимости (бесплатный путь). < /p>
Я попытался изменить это поведение с помощью пользовательской функции затрат, которая Начнем с одолжений удаления (с стоимостью -1), затем способствует равенству (с стоимостью 0), а затем способствует внедрению (с стоимостью 1). < /p>
при запуске Следующая модифицированная версия и выполнение Diff на PQRSTUZ V/S PQTuars , я получаю tua в качестве вставленного и tuz в качестве удаленного.

Код: Выделить всё

        class Keep {
constructor(line, coordinate, cost) {
this.type = 'Keep';
this.line = line;
this.coordinate = coordinate;
this.cost = cost;
}
}

class Insert {
constructor(line, coordinate, cost) {
this.type = 'Insert';
this.line = line;
this.coordinate = coordinate;
this.cost = cost;
}
}

class Remove {
constructor(line, coordinate, cost) {
this.type = 'Remove';
this.line = line;
this.coordinate = coordinate;
this.cost = cost;
}
}

// Represents a point on the frontier with its history AND cost
class Frontier {
constructor(x, history, cost) {
this.x = x; // x-coordinate in edit graph
this.history = history; // sequence of operations to reach this point
this.cost = cost; // Total cost to reach this point
}
}

function visualizePath(history, oldSequence, newSequence) {
const matrix = [];
const costMatrix = [];
const rows = newSequence.length + 1;
const cols = oldSequence.length + 1;
const firstRow = Array.from(` ${oldSequence.map(item => item.hashVal).join('')}`);

matrix[0] = firstRow;
costMatrix[0] = firstRow;

for(let r = 1, c = 0; r < rows; r++) {
matrix[r] = Array.from("·".repeat(cols))
matrix[r][c] = newSequence[r-1].hashVal;

costMatrix[r] = Array.from("·".repeat(cols))
costMatrix[r][c] = newSequence[r-1].hashVal;
}

for(const item of history) {
const {type, coordinate, cost} = item;
let sign = null;
switch(type) {
case 'Keep':
sign = '↘';
break;
case 'Insert':
sign = '↓';
break;
case 'Remove':
sign = '→';
break;
}
if(coordinate[0] === 0) {
coordinate[0] = 1;
}
matrix[coordinate[1]][coordinate[0]] = sign;
costMatrix[coordinate[1]][coordinate[0]] = cost;
}
for(const row of matrix) {
console.log(row.join(" "));
}
console.log("\n")
for(const row of costMatrix) {
console.log(row.join(" "));
}
}

function myersDiff(old, current, costFunction) {
// Initialize the frontier with starting point.   Start with cost 0
const frontier = { 1: new Frontier(0, [], 0) };

// Convert from 1-based to 0-based indexing
// Algorithm uses 1-based indexing, but JavaScript uses 0-based
function one(idx) {
return idx - 1;
}

const aMax = old.length; // horizontal size of edit graph
const bMax = current.length; // vertical size of edit graph

// Main loop: try increasing numbers of edits
for (let d = 0; d 

Подробнее здесь: [url]https://stackoverflow.com/questions/79432427/myers-diff-with-a-custom-cost-function[/url]
Ответить

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

Вернуться в «Javascript»