public void TrieSearch_Should_ReturnExpected() { var trie = new AcPatternMatcher(new List <string> { "abcd", "bcd", "BC", "c", "bc cd" }); var ret = trie.Match("dd abdbc cd zzzz").ToArray(); Assert.AreEqual(4, ret.Length); Assert.AreEqual((6, "bc"), ret[0]); Assert.AreEqual((7, "c"), ret[1]); Assert.AreEqual((9, "c"), ret[2]); Assert.AreEqual((6, "bc cd"), ret[3]); }
public void TrieExists_Should_ReturnExpected() { var trie = new AcPatternMatcher(new List <string> { "how", "hi", "her", "hello", "so", "see" }); Assert.IsTrue(trie.Exists("how")); Assert.IsTrue(trie.Exists("hi")); Assert.IsTrue(trie.Exists("HER")); Assert.IsTrue(trie.Exists("Hello")); Assert.IsTrue(trie.Exists("so")); Assert.IsTrue(trie.Exists("See")); Assert.IsFalse(trie.Exists("HERE")); Assert.IsFalse(trie.Exists("he")); Assert.IsFalse(trie.Exists("she")); }
/// <summary> /// Replace contents in text that match given patterns /// </summary> /// <param name="text">Text</param> /// <param name="acMachine">Aho-Corasick</param> /// <param name="replaceWith">Replace content with</param> /// <returns>Replaced string</returns> public static string Replace(this string text, AcPatternMatcher acMatcher, char replaceWith = '*') { return(acMatcher.Replace(text, replaceWith)); }
/// <summary> /// Search in text with given patterns /// </summary> /// <param name="text">Text</param> /// <param name="acMachine">Aho-Corasick</param> /// <returns>Search results with position</returns> public static IEnumerable <(int position, string value)> Search(this string text, AcPatternMatcher acMatcher) { return(acMatcher.Match(text)); }
/// <summary> /// Replace contents in text that match given patterns /// </summary> /// <param name="text">Text</param> /// <param name="patterns">Patterns</param> /// <param name="replaceWith">Replace content with</param> /// <returns>Replaced string</returns> public static string Replace(this string text, IEnumerable <string> patterns, char replaceWith = '*') { var acMatcher = new AcPatternMatcher(patterns); return(Replace(text, acMatcher, replaceWith)); }
/// <summary> /// Search in text with given patterns /// </summary> /// <param name="text">Text</param> /// <param name="patterns">Patterns</param> /// <returns>Search results with position</returns> public static IEnumerable <(int position, string value)> Search(this string text, IEnumerable <string> patterns) { var acMatcher = new AcPatternMatcher(patterns); return(Search(text, acMatcher)); }