コード例 #1
0
    public int FindCategoryKMP(TwitterStatus tweet, List <StringPattern> Final)
    {
        int    kategori           = 0;
        String TField             = tweet.Text.ToLower();
        List <StringPattern> Find = new List <StringPattern>();

        foreach (var item in Final)
        {
            if (item.Next == null)
            {
                KMP kmp = new KMP(item.Data.ToLower());
                if (kmp.Search(TField) != -1)
                {
                    Find.Add(item);
                }
            }
            else
            {
                StringPattern P = new StringPattern();
                P = item;
                bool valid = true;
                while (P != null)
                {
                    KMP kmp = new KMP(P.Data.ToLower());
                    if (kmp.Search(TField) == -1)
                    {
                        valid = false;
                        break;
                    }
                    P = P.Next;
                }
                if (valid == true)
                {
                    Find.Add(item);
                }
            }
        }
        int min = 999;

        foreach (var item in Find)
        {
            KMP kmp = new KMP(item.Data.ToLower());
            if (kmp.Search(TField) < min)
            {
                min = kmp.Search(TField);
            }
        }
        foreach (var item in Find)
        {
            KMP kmp = new KMP(item.Data.ToLower());
            if (kmp.Search(TField) == min)
            {
                kategori = item.Kategori;
            }
        }
        return(kategori);
    }
コード例 #2
0
        private static void SearchPattern(string pat, string txt)
        {
            var pattern = pat.ToCharArray();
            var text    = txt.ToCharArray();

            var kmp1    = new KMP(pat);
            var offset1 = kmp1.Search(txt);

            var kmp2    = new KMP(pattern, 256);
            var offset2 = kmp2.Search(text);

            // print results
            Console.WriteLine("text:    " + txt);

            Console.Write("pattern: ");
            for (var i = 0; i < offset1; i++)
            {
                Console.Write(" ");
            }
            Console.WriteLine(pat);

            Console.Write("pattern: ");
            for (var i = 0; i < offset2; i++)
            {
                Console.Write(" ");
            }
            Console.WriteLine(pat);
        }
コード例 #3
0
        public void String_KMP_Test()
        {
            var algorithm = new KMP();

            var index = algorithm.Search("xabcabzabc", "abc");

            Assert.AreEqual(1, index);

            index = algorithm.Search("abdcdaabxaabxcaabxaabxay", "aabxaabxcaabxaabxay");

            Assert.AreEqual(5, index);

            index = algorithm.Search("aaaabaaaaaaa", "aaaa");

            Assert.AreEqual(0, index);

            index = algorithm.Search("abcabababdefgabcd", "fga");

            Assert.AreEqual(11, index);

            index = algorithm.Search("abxabcabcaby", "abcaby");

            Assert.AreEqual(6, index);

            index = algorithm.Search("abxabcabcaby", "abx");

            Assert.AreEqual(0, index);
        }
コード例 #4
0
    public String SearchLocat(TwitterStatus tweet)
    {
        String locat  = "";
        String s      = "di ";
        String TField = tweet.Text.ToLower();
        KMP    kmp    = new KMP(s);

        if (kmp.Search(TField) != -1)
        {
            int    begin = kmp.Search(TField) + s.Length;
            String temp  = "";
            for (int i = begin; i < TField.Length; i++)
            {
                if (TField[i] != ' ')
                {
                    temp = temp + TField[i];
                }
                else
                {
                    if (temp != "")
                    {
                        if (MatchLocat(temp))
                        {
                            locat = locat + temp;
                            temp  = "";
                        }
                        else
                        {
                            locat = locat + temp;
                            return(locat);
                        }
                    }
                }
            }
        }
        return(locat);
    }