Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I don't know python, but the following C# code works if that's any help.</p> <pre class="lang-cs prettyprint-override"><code>public class EditDistanceCalculator { public double SubstitutionCost { get; private set; } public double DeletionCost { get; private set; } public double InsertionCost { get; private set; } public EditDistanceCalculator() : this(1,1, 1) { } public EditDistanceCalculator(double substitutionCost, double insertionCost, double deletionCost) { InsertionCost = insertionCost; DeletionCost = deletionCost; SubstitutionCost = substitutionCost; } public Move[] CalcEditDistance(string s, string t) { if (s == null) throw new ArgumentNullException("s"); if (t == null) throw new ArgumentNullException("t"); var distances = new Cell[s.Length + 1, t.Length + 1]; for (int i = 0; i &lt;= s.Length; i++) distances[i, 0] = new Cell(i, Move.Delete); for (int j = 0; j &lt;= t.Length; j++) distances[0, j] = new Cell(j, Move.Insert); for (int i = 1; i &lt;= s.Length; i++) for (int j = 1; j &lt;= t.Length; j++) distances[i, j] = CalcEditDistance(distances, s, t, i, j); return GetEdit(distances, s.Length, t.Length); } private Cell CalcEditDistance(Cell[,] distances, string s, string t, int i, int j) { var cell = s[i - 1] == t[j - 1] ? new Cell(distances[i - 1, j - 1].Cost, Move.Match) : new Cell(SubstitutionCost + distances[i - 1, j - 1].Cost, Move.Substitute); double deletionCost = DeletionCost + distances[i - 1, j].Cost; if (deletionCost &lt; cell.Cost) cell = new Cell(deletionCost, Move.Delete); double insertionCost = InsertionCost + distances[i, j - 1].Cost; if (insertionCost &lt; cell.Cost) cell = new Cell(insertionCost, Move.Insert); return cell; } private static Move[] GetEdit(Cell[,] distances, int i, int j) { var moves = new Stack&lt;Move&gt;(); while (i &gt; 0 &amp;&amp; j &gt; 0) { var move = distances[i, j].Move; moves.Push(move); switch (move) { case Move.Match: case Move.Substitute: i--; j--; break; case Move.Insert: j--; break; case Move.Delete: i--; break; default: throw new ArgumentOutOfRangeException(); } } for (int k = 0; k &lt; i; k++) moves.Push(Move.Delete); for (int k = 0; k &lt; j; k++) moves.Push(Move.Insert); return moves.ToArray(); } class Cell { public double Cost { get; private set; } public Move Move { get; private set; } public Cell(double cost, Move move) { Cost = cost; Move = move; } } } public enum Move { Match, Substitute, Insert, Delete } </code></pre> <p>Some tests:</p> <pre><code> [TestMethod] public void TestEditDistance() { var expected = new[] { Move.Delete, Move.Substitute, Move.Match, Move.Match, Move.Match, Move.Match, Move.Match, Move.Insert, Move.Substitute, Move.Match, Move.Substitute, Move.Match, Move.Match, Move.Match, Move.Match }; Assert.IsTrue(expected.SequenceEqual(new EditDistanceCalculator().CalcEditDistance("thou-shalt-not", "you-should-not"))); var calc = new EditDistanceCalculator(3, 1, 1); var edit = calc.CalcEditDistance("democrat", "republican"); Console.WriteLine(string.Join(",", edit)); Assert.AreEqual(3, edit.Count(m =&gt; m == Move.Match)); //eca } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload