Пример #1
0
        public int CheckPat(string word1, string word2, MatchPat p, ref int minD)
        {
            int headCount = 0, bodyCount = 0, tailCount = 0;

            bodyCount = p.Word1Body.Len - p.Word2Body.Len;
            if (bodyCount >= minD)
            {
                return(bodyCount);
            }

            if (p.Word2Head.Len > 0)
            {
                if (p.Word1Head.Len > 0)
                {
                    var md = MinDistance(
                        word1.Substring(p.Word1Head.Start, p.Word1Head.Len),
                        word2.Substring(p.Word2Head.Start, p.Word2Head.Len));
                    headCount = Math.Min(md, Math.Max(p.Word1Head.Len, p.Word2Head.Len));
                }
                else
                {
                    // p.Word1Head.Len == 0
                    headCount = p.Word2Head.Len; // add if any
                }
            }
            else
            {
                // p.Word2Head.Len == 0
                headCount = p.Word1Head.Len; // remove if any
            }

            if (p.Word2Tail.Len > 0)
            {
                if (p.Word1Tail.Len > 0)
                {
                    var md = MinDistance(
                        word1.Substring(p.Word1Tail.Start, p.Word1Tail.Len),
                        word2.Substring(p.Word2Tail.Start, p.Word2Tail.Len));
                    tailCount = Math.Min(md, Math.Max(p.Word1Tail.Len, p.Word2Tail.Len));
                }
                else
                {
                    // p.Word1Tail.Len == 0
                    tailCount = p.Word2Tail.Len; // add if any
                }
            }
            else
            {
                // p.Word2Tail.Len == 0
                tailCount = p.Word1Tail.Len; // remove if any
            }

            int dist = headCount + bodyCount + tailCount;

            return(dist);
        }
Пример #2
0
        List <MatchPat> FindMatchPats(string word1, string word2)
        {
            var pats = new List <MatchPat>();

            for (var i = 0; i < word2.Length; i++)
            {
                for (var j = word2.Length - 1; j >= i; j--)
                {
                    var  len = j - i + 1;
                    var  sub = word2.Substring(i, len);
                    int  start, end;
                    bool find = FindSmallestSubInStr(sub, word1, out start, out end);
                    if (find)
                    {
                        var pat = new MatchPat();
                        if (start > 0)
                        {
                            pat.Word1Head.Start = 0;
                            pat.Word1Head.End   = start - 1;
                        }
                        else
                        {
                            pat.Word1Head.Start = -1;
                            pat.Word1Head.End   = pat.Word1Head.Start - 1; // len=0
                        }
                        pat.Word1Body.Start = start;
                        pat.Word1Body.End   = end;
                        pat.Word1Tail.Start = end + 1;
                        if (pat.Word1Tail.Start < word1.Length)
                        {
                            pat.Word1Tail.End = word1.Length - 1;
                        }
                        else
                        {
                            pat.Word1Tail.End = pat.Word1Tail.Start - 1; // len=0
                        }

                        if (i > 0)
                        {
                            pat.Word2Head.Start = 0;
                            pat.Word2Head.End   = i - 1;
                        }
                        else
                        {
                            pat.Word2Head.Start = -1;
                            pat.Word2Head.End   = pat.Word2Head.Start - 1; // len=0
                        }
                        pat.Word2Body.Start = i;
                        pat.Word2Body.End   = j;
                        pat.Word2Tail.Start = j + 1;
                        if (pat.Word2Tail.Start < word2.Length)
                        {
                            pat.Word2Tail.End = word2.Length - 1;
                        }
                        else
                        {
                            pat.Word2Tail.End = pat.Word2Tail.Start - 1; //len=0
                        }
                        pat.Len = len;
                        pats.Add(pat);
                    }
                }
            }
            // pats.Count will be 0 if no match pat found
            return(pats);
        }