public static void Main()
    {
        string text = File.ReadAllText("../../text.txt").ToLower();
        int wordsCount = int.Parse(Console.ReadLine());
        string[] wordsOriginal = new string[wordsCount];
        string[] wordsLowerCase = new string[wordsCount];
        for (int i = 0; i < wordsCount; i++)
        {
            wordsOriginal[i] = Console.ReadLine();
            wordsLowerCase[i] = wordsOriginal[i].ToLower();
        }

        var occurances = new Dictionary<string, int>(wordsCount);
        foreach (string word in wordsLowerCase)
        {
            occurances[word] = 0;
        }

        var start = DateTime.Now;

        var search = new AhoCorasickSearch();
        var matches = search.SearchAll(text, wordsLowerCase);
        foreach (var match in matches)
        {
            string word = match.Match;
            occurances[word]++;
        }

        foreach (var wordOccurances in occurances)
        {
            Console.WriteLine("{0} -> {1}", wordOccurances.Key, wordOccurances.Value);
        }

        Console.WriteLine(DateTime.Now - start);
    }
示例#2
0
        public void FindAll_AXBY()
        {
            var matcher = new AhoCorasickSearch(new List <string>
            {
                "ax",
                "by"
            });

            matcher.FindAll(" if something then if goto J elsif");
        }
示例#3
0
        private static void searchTest(AhoCorasickSearch search)
        {
            var res = search.SearchAll("ushersushers");

            Console.WriteLine(res);
            for (int i = 0; i < res.Length; i++)
            {
                Console.WriteLine(res[i].ToString());
                Console.WriteLine("Match=" + res[i].Match);
            }
        }
示例#4
0
        public void FindAll_CommonPostfix_NoWordBreak_NoOverlap()
        {
            var matcher = new AhoCorasickSearch(new List <string>
            {
                "if",
                "elsif",
            }, false);

            var actual = matcher.FindAll(" elsif ").ToList();

            Assert.AreEqual(1, actual.Count, "Wrong number of matches");
            Assert.AreEqual("elsif", actual[0].Value, "Matched wrong keyword matched");
        }
示例#5
0
        static void Main(string[] args)
        {
            var search = new AhoCorasickSearch();
            var keywords = new string[] { "he", "she", "his", "hers" };
            search.Build(keywords);
            
            searchTest(search);

            searchTest(search);
            replaceTest2();
            replaceTest3();

            Console.Read();
        }
示例#6
0
        public void FindAll_CommonPostfix_NoWordBreak()
        {
            var matcher = new AhoCorasickSearch(new List <string>
            {
                "if",
                "elsif",
            }, false, true);
            TextReader rdr    = new StringReader("aaa");
            var        actual = matcher.FindAll(" elsif ").ToList();

            Assert.AreEqual(2, actual.Count, "Wrong number of matches");
            Assert.AreEqual("elsif", actual[0].Value, "Matched wrong keyword matched (1)");
            Assert.AreEqual("if", actual[1].Value, "Matched wrong keyword matched (2)");
        }
示例#7
0
        private static void replaceTest3()
        {
            var search = new AhoCorasickSearch();
            var keywords = new string[] { "一边", "刀锋", "烽火" };
            search.Build(keywords);

            var text = "一边烽火, 一边烽火天";
            var res = search.Replace(text, "-");
            Console.WriteLine(res);

            text = "一边刀锋很犀利";
            res = search.Replace(text, "-");
            Console.WriteLine(res);
        }
示例#8
0
        static void Main(string[] args)
        {
            var search   = new AhoCorasickSearch();
            var keywords = new string[] { "he", "she", "his", "hers" };

            search.Build(keywords);

            searchTest(search);

            searchTest(search);
            replaceTest2();
            replaceTest3();

            Console.Read();
        }
示例#9
0
        public void FindAll_CommonPostfix()
        {
            var matcher = new AhoCorasickSearch(new List <string>
            {
                "if",
                "elsif",
            });

            var actual = matcher.FindAll(" elsif ").ToList();

            Assert.AreEqual(1, actual.Count, "Wrong number of matches");
            Assert.AreEqual(1, actual[0].Position, "Matched wrong position");
            Assert.AreEqual(5, actual[0].Length, "Matched wrong length");
            Assert.AreEqual("elsif", actual[0].Value, "Matched wrong keyword");
        }
示例#10
0
        private static void replaceTest3()
        {
            var search   = new AhoCorasickSearch();
            var keywords = new string[] { "一边", "刀锋", "烽火" };

            search.Build(keywords);

            var text = "一边烽火, 一边烽火天";
            var res  = search.Replace(text, "-");

            Console.WriteLine(res);

            text = "一边刀锋很犀利";
            res  = search.Replace(text, "-");
            Console.WriteLine(res);
        }
示例#11
0
        public void FindAll_CommonPrefix_NoWordBreakB_NoOverlap()
        {
            var matcher = new AhoCorasickSearch(new List <string>
            {
                "if",
                "ifa",
                "ifb"
            }, false);

            var actual = matcher.FindAll("xifax").ToList();

            Assert.AreEqual(1, actual.Count, "Wrong number of matches");
            Assert.AreEqual(1, actual[0].Position, "Matched wrong position");
            Assert.AreEqual(3, actual[0].Length, "Matched wrong length");
            Assert.AreEqual("ifa", actual[0].Value, "Matched wrong keyword");
        }
示例#12
0
        public FileProcessor(string searchedWord, IDispatcher dispatcher)
        {
            Dispatcher = dispatcher;

            SearchAlgorithm = new AhoCorasickSearch();
            Results         = new ResultsProvider()
            {
                SearchedWord      = searchedWord,
                MatchingFilenames = ImmutableSortedSet <string> .Empty
            };

            SearchAlgorithm.AddPattern(Encoding.Unicode.GetBytes(searchedWord));
            SearchAlgorithm.AddPattern(Encoding.UTF8.GetBytes(searchedWord));
            SearchAlgorithm.AddPattern(Encoding.UTF32.GetBytes(searchedWord));
            SearchAlgorithm.Freeze();
        }
示例#13
0
        static void Main(string[] args)
        {
            var search = new AhoCorasickSearch()
            {
                IsCaseSensitive = true
            };
            var keywords = new string[] { "he", "She", "hIs", "herS" };

            search.Build(keywords);

            searchTest(search);
            replaceTest(search);
            replaceTest2();
            replaceTest3();

            Console.Read();
        }
示例#14
0
        private static void replaceTest(AhoCorasickSearch search)
        {
            var text = "ushers";
            var res = search.Replace(text, "-");
            Console.WriteLine(res);

            text = "shersx";
            res = search.Replace(text, "-");
            Console.WriteLine(res);

            text = "her";
            res = search.Replace(text, "-");
            Console.WriteLine(res);

            text = "she";
            res = search.Replace(text, "-");
            Console.WriteLine(res);
        }
示例#15
0
        private static void replaceTest(AhoCorasickSearch search)
        {
            var text = "ushers";
            var res  = search.Replace(text, "-");

            Console.WriteLine(res);

            text = "shersx";
            res  = search.Replace(text, "-");
            Console.WriteLine(res);

            text = "her";
            res  = search.Replace(text, "-");
            Console.WriteLine(res);

            text = "she";
            res  = search.Replace(text, "-");
            Console.WriteLine(res);
        }
示例#16
0
        public void FindAll_MultipleMatches()
        {
            var matcher = new AhoCorasickSearch(new List <string>
            {
                "if",
                "ifa",
                "ifb"
            });

            var actual = matcher.FindAll("Matches 'ifa', 'ifb' and 'if' in a longer text, Keywords: if, ifa, ifb").ToList();

            Assert.AreEqual(6, actual.Count, "Wrong number of matches");

            Assert.AreEqual("ifa", actual[0].Value, "Matched wrong keyword");
            Assert.AreEqual("ifb", actual[1].Value, "Matched wrong keyword");
            Assert.AreEqual("if", actual[2].Value, "Matched wrong keyword");
            Assert.AreEqual("if", actual[3].Value, "Matched wrong keyword");
            Assert.AreEqual("ifa", actual[4].Value, "Matched wrong keyword");
            Assert.AreEqual("ifb", actual[5].Value, "Matched wrong keyword");
        }
示例#17
0
    public static void Main()
    {
        string text       = File.ReadAllText("../../text.txt").ToLower();
        int    wordsCount = int.Parse(Console.ReadLine());

        string[] wordsOriginal  = new string[wordsCount];
        string[] wordsLowerCase = new string[wordsCount];
        for (int i = 0; i < wordsCount; i++)
        {
            wordsOriginal[i]  = Console.ReadLine();
            wordsLowerCase[i] = wordsOriginal[i].ToLower();
        }

        var occurances = new Dictionary <string, int>(wordsCount);

        foreach (string word in wordsLowerCase)
        {
            occurances[word] = 0;
        }

        var start = DateTime.Now;

        var search  = new AhoCorasickSearch();
        var matches = search.SearchAll(text, wordsLowerCase);

        foreach (var match in matches)
        {
            string word = match.Match;
            occurances[word]++;
        }


        foreach (var wordOccurances in occurances)
        {
            Console.WriteLine("{0} -> {1}", wordOccurances.Key, wordOccurances.Value);
        }

        Console.WriteLine(DateTime.Now - start);
    }
示例#18
0
        private static void replaceTest2()
        {
            var search = new AhoCorasickSearch();
            var keywords = new string[] { "伟大","特色主义","公园" };
            search.Build(keywords);

            var text = "从这里建设伟大的特色主义主题公园";
            var res = search.Replace(text, "-");
            Console.WriteLine(res);

            text = "主题公园";
            res = search.Replace(text, "-");
            Console.WriteLine(res);

            text = "伟大的特色主义主题公园";
            res = search.Replace(text, "-");
            Console.WriteLine(res);

            text = "伟大特色主义公园";
            res = search.Replace(text, "-");
            Console.WriteLine(res);
        }
示例#19
0
        private static void replaceTest2()
        {
            var search   = new AhoCorasickSearch();
            var keywords = new string[] { "伟大", "特色主义", "公园" };

            search.Build(keywords);

            var text = "从这里建设伟大的特色主义主题公园";
            var res  = search.Replace(text, "-");

            Console.WriteLine(res);

            text = "主题公园";
            res  = search.Replace(text, "-");
            Console.WriteLine(res);

            text = "伟大的特色主义主题公园";
            res  = search.Replace(text, "-");
            Console.WriteLine(res);

            text = "伟大特色主义公园";
            res  = search.Replace(text, "-");
            Console.WriteLine(res);
        }
示例#20
0
        /// <summary>
        /// Looks for byte patterns that look like 
        /// </summary>
        /// <param name="addrBegin"></param>
        /// <param name="addrEnd"></param>
        /// <returns></returns>
        public IEnumerable<Address> FindPossibleProcedureEntries(Address addrBegin, Address addrEnd)
        {
            var h = program.Platform.Heuristics;
            if (h.ProcedurePrologs == null || h.ProcedurePrologs.Length == 0)
                return new Address[0];

            byte[] pattern = h.ProcedurePrologs[0].Bytes;
            var search = new AhoCorasickSearch<byte>(new[] { pattern }, true, true);
            return search.GetMatchPositions(program.Image.Bytes)
                .Select(i => program.Image.BaseAddress + i);
        }
示例#21
0
        private static void searchTest(AhoCorasickSearch search)
        {
            var res = search.SearchAll("ushers");

            Console.WriteLine(res);
        }
示例#22
0
 private static void searchTest(AhoCorasickSearch search)
 {
     var res = search.SearchAll("ushers");
     Console.WriteLine(res);
 }
示例#23
0
 private IEnumerable<Address> FindPossibleProcedureEntries(Address addrBegin, Address addrEnd)
 {
     var pattern = new byte[] { 0x55, 0x8B, 0xEC };  //$TODO: platform-dependent.
     var search = new AhoCorasickSearch<byte>(new[] { pattern }, true, true);
     return search.GetMatchPositions(prog.Image.Bytes)
         .Select(i => prog.Image.BaseAddress + i);
 }
示例#24
0
 private static void AhoCorasickSearch(List <string> list, string txt)
 {
     AhoCorasickSearch ahoCorasickSearch = new AhoCorasickSearch(list);
 }