Detect differences between two strings

前端 未结 5 987
天命终不由人
天命终不由人 2020-12-05 06:40

I have 2 strings

string a = \"foo bar\";
string b = \"bar foo\";

and I want to detect the changes from a to b. <

5条回答
  •  星月不相逢
    2020-12-05 07:28

    You are looking for (minimum) edit distance / (minimum) edit sequence. You can find the theory of the process here:

    https://web.stanford.edu/class/cs124/lec/med.pdf

    Let's implement (simplest) Levenstein Distance / Sequence algorithm (for details see https://en.wikipedia.org/wiki/Levenshtein_distance). Let's start from helper classes (I've changed a bit your implementation of them):

      public enum EditOperationKind : byte {
        None,    // Nothing to do
        Add,     // Add new character
        Edit,    // Edit character into character (including char into itself)
        Remove,  // Delete existing character
      };
    
      public struct EditOperation {
        public EditOperation(char valueFrom, char valueTo, EditOperationKind operation) {
          ValueFrom = valueFrom;
          ValueTo = valueTo;
    
          Operation = valueFrom == valueTo ? EditOperationKind.None : operation;
        }
    
        public char ValueFrom { get; }
        public char ValueTo {get ;}
        public EditOperationKind Operation { get; }
    
        public override string ToString() {
          switch (Operation) {
            case EditOperationKind.None:
              return $"'{ValueTo}' Equal";
            case EditOperationKind.Add:
              return $"'{ValueTo}' Add";
            case EditOperationKind.Remove:
              return $"'{ValueFrom}' Remove";
            case EditOperationKind.Edit:
              return $"'{ValueFrom}' to '{ValueTo}' Edit";
            default:
              return "???";
          }
        }
      }
    

    As far as I can see from the examples provided we don't have any edit operation, but add + remove; that's why I've put editCost = 2 when insertCost = 1, int removeCost = 1 (in case of tie: insert + remove vs. edit we put insert + remove). Now we are ready to implement Levenstein algorithm:

    public static EditOperation[] EditSequence(
      string source, string target, 
      int insertCost = 1, int removeCost = 1, int editCost = 2) {
    
      if (null == source)
        throw new ArgumentNullException("source");
      else if (null == target)
        throw new ArgumentNullException("target");
    
      // Forward: building score matrix
    
      // Best operation (among insert, update, delete) to perform 
      EditOperationKind[][] M = Enumerable
        .Range(0, source.Length + 1)
        .Select(line => new EditOperationKind[target.Length + 1])
        .ToArray();
    
      // Minimum cost so far
      int[][] D = Enumerable
        .Range(0, source.Length + 1)
        .Select(line => new int[target.Length + 1])
        .ToArray();
    
      // Edge: all removes
      for (int i = 1; i <= source.Length; ++i) {
        M[i][0] = EditOperationKind.Remove;
        D[i][0] = removeCost * i;
      }
    
      // Edge: all inserts 
      for (int i = 1; i <= target.Length; ++i) {
        M[0][i] = EditOperationKind.Add;
        D[0][i] = insertCost * i;
      }
    
      // Having fit N - 1, K - 1 characters let's fit N, K
      for (int i = 1; i <= source.Length; ++i)
        for (int j = 1; j <= target.Length; ++j) {
          // here we choose the operation with the least cost
          int insert = D[i][j - 1] + insertCost;
          int delete = D[i - 1][j] + removeCost;
          int edit = D[i - 1][j - 1] + (source[i - 1] == target[j - 1] ? 0 : editCost);
    
          int min = Math.Min(Math.Min(insert, delete), edit);
    
          if (min == insert) 
            M[i][j] = EditOperationKind.Add;
          else if (min == delete)
            M[i][j] = EditOperationKind.Remove;
          else if (min == edit)
            M[i][j] = EditOperationKind.Edit;
    
          D[i][j] = min;
        }
    
      // Backward: knowing scores (D) and actions (M) let's building edit sequence
      List result = 
        new List(source.Length + target.Length);
    
      for (int x = target.Length, y = source.Length; (x > 0) || (y > 0);) {
        EditOperationKind op = M[y][x];
    
        if (op == EditOperationKind.Add) {
          x -= 1;
          result.Add(new EditOperation('\0', target[x], op));
        }
        else if (op == EditOperationKind.Remove) {
          y -= 1;
          result.Add(new EditOperation(source[y], '\0', op));
        }
        else if (op == EditOperationKind.Edit) {
          x -= 1;
          y -= 1;
          result.Add(new EditOperation(source[y], target[x], op));
        }
        else // Start of the matching (EditOperationKind.None)
          break;
      }
    
      result.Reverse();
    
      return result.ToArray();
    }
    

    Demo:

    var sequence = EditSequence("asdfghjk", "wsedrftr"); 
    
    Console.Write(string.Join(Environment.NewLine, sequence));
    

    Outcome:

    'a' Remove
    'w' Add
    's' Equal
    'e' Add
    'd' Equal
    'r' Add
    'f' Equal
    'g' Remove
    'h' Remove
    'j' Remove
    'k' Remove
    't' Add
    'r' Add
    

提交回复
热议问题