Esempio n. 1
0
        static List <EditDistanceMatchObject> DeleteN(string s, int index, int n)
        {
            List <EditDistanceMatchObject> toReturn = new List <EditDistanceMatchObject>();

            EditDistanceMatchObject rowAsEDMO = new EditDistanceMatchObject
            {
                Index          = index,
                DeletedIndices = new List <int>(),
                Substring      = s
            };

            toReturn.Add(rowAsEDMO);
            List <EditDistanceMatchObject> currentGeneration = toReturn;

            for (int i = 0; i < n; i++)
            {
                List <EditDistanceMatchObject> nextGeneration = new List <EditDistanceMatchObject>();

                foreach (var element in currentGeneration)
                {
                    nextGeneration.AddRange(DeleteOneMore(element));
                }

                toReturn.AddRange(nextGeneration);

                currentGeneration = nextGeneration;
            }

            return(toReturn);
        }
Esempio n. 2
0
        /// <summary>
        /// Assumes substrings match
        /// </summary>
        /// <returns></returns>
        static int EditDistance(EditDistanceMatchObject e1, EditDistanceMatchObject e2)
        {
            int toReturn = 0;

            //We will verify that the union of deleted indices has size at most n

            //Indices to deleted indices.  It is also the index offset to the diff string.  I know that doesn't make sense, but it's meant to be a clue as to why the algorithm works.
            int i1 = 0, i2 = 0;

            while (i1 < e1.DeletedIndices.Count && i2 < e2.DeletedIndices.Count)
            {
                int nextString1Index = e1.DeletedIndices[i1] + i2;
                int nextString2Index = e2.DeletedIndices[i2] + i1;
                if (nextString1Index < nextString2Index)
                {
                    i1++;
                }
                else if (nextString2Index < nextString1Index)
                {
                    i2++;
                }
                else
                {
                    i1++;
                    i2++;
                }

                toReturn++;
            }

            toReturn += e1.DeletedIndices.Count - i1 + e2.DeletedIndices.Count - i2;

            return(toReturn);
        }
Esempio n. 3
0
        static List <EditDistanceMatchObject> DeleteOneMore(EditDistanceMatchObject edmo)
        {
            List <EditDistanceMatchObject> toReturn = new List <EditDistanceMatchObject>();

            int oldStringLength      = edmo.Substring.Length;
            int oldDeletedIndexCount = edmo.DeletedIndices.Count;
            int minDeletionIndex     = oldDeletedIndexCount == 0 ? 0 : edmo.DeletedIndices.Max() + 1;
            int maxDeletionIndex     = oldStringLength + oldDeletedIndexCount - 1;

            for (int i = minDeletionIndex; i <= maxDeletionIndex; i++)
            {
                EditDistanceMatchObject toAdd = new EditDistanceMatchObject
                {
                    DeletedIndices = edmo.DeletedIndices.Select(index => index).ToList(),
                    Index          = edmo.Index,
                    Substring      = edmo.Substring.Remove(i - oldDeletedIndexCount, 1)
                };
                toAdd.DeletedIndices.Add(i);

                toReturn.Add(toAdd);
            }

            return(toReturn);
        }