public void IssuesTest_20() { string text = "A10021003吃饭"; var keywords = new string[] { "1", "A", "2", "0", "吃" }; var iws = new IllegalWordsSearch(); var ss = new StringSearch(); var sse = new StringSearchEx2(); iws.SetKeywords(keywords); iws.UseIgnoreCase = true; iws.UseDBCcaseConverter = true; var iwsFirst = iws.FindFirst(text); Assert.AreEqual("吃", iwsFirst.Keyword); var iwsAll = iws.FindAll(text); Assert.AreEqual(1, iwsAll.Count);// 因为1A20左右都是英文或数字,所以识别失败 ss.SetKeywords(keywords); var ssFirst = ss.FindFirst(text); Assert.AreEqual("A", ssFirst); var ssAll = ss.FindAll(text); Assert.AreEqual(9, ssAll.Count); sse.SetKeywords(keywords); var sseFirst = sse.FindFirst(text); Assert.AreEqual("A", sseFirst); var sseAll = sse.FindAll(text); Assert.AreEqual(9, sseAll.Count); }
public void can_compare3() { //this.s1 = new StringSearch("doof", Track.Property.Titel.ToString(), Titel.Property.Name.ToString()); this.s2 = new StringSearch("doof", Track.Property.Autor.ToString(), Titel.Property.Name.ToString()); Assert.AreNotEqual(this.s1, this.s2); }
private void DeleteDirtyWordsBase_Click(object sender, EventArgs e) { string extension = Application.StartupPath; int index = MaintextBox.GetFirstCharIndexOfCurrentLine();//得到当前行第一个字符的索引 int line = GetTextboxLine(index); //int line = MaintextBox.GetLineFromCharIndex(index) + 1;//得到当前行的行号,从0开始,习惯是从1开始,所以+1. string DeleteWordsStr = null; try { StreamReader sr = new StreamReader(extension + "\\品牌词库.txt", Encoding.GetEncoding("gb2312")); DeleteWordsStr = sr.ReadToEnd(); sr.Close(); } catch (Exception ex)//没有读取到文件 { MessageBox.Show("没有找到品牌词库文件!请先在可执行文件路径下导入【品牌词库.txt】 文件!" + ex); } if (DeleteWordsStr != null && DeleteWordsStr != "") { StringSearch iwords = new StringSearch(); iwords.SetKeywords(DeleteWordsStr.Split(new char[] { '|' }, StringSplitOptions.RemoveEmptyEntries)); for (int i = 0; i < TitleStrList.Count(); i++) { string DeletedStr = iwords.Replace(TitleStrList[i], ' '); TitleStrList[i] = DeletedStr.Replace(" ", ""); } MaintextBox.Text = FormatStr(TitleStrList); MessageBox.Show("删除完成!"); } MaintextBox.SelectionStart = getnewindex(line); MaintextBox.SelectionLength = 0; MaintextBox.ScrollToCaret();//到指定行 MaintextBox.Focus(); }
public static int FindInFile(FileStream file, string value) { // returns complement of number of characters in file if not found // else returns index where value found int index = 0; file.Seek(0, SeekOrigin.Begin); System.IO.StreamReader reader = new System.IO.StreamReader(file, Encoding.ASCII); if (String.IsNullOrEmpty(value)) { return(0); } StringSearch valueSearch = new StringSearch(value); int readChar; while ((readChar = reader.Read()) >= 0) { ++index; if (valueSearch.Found(readChar)) { return(index - value.Length); } } return(~index); }
public void test() { string s = "中国|国人|zg人"; string test = "我是中国人"; StringSearch iwords = new StringSearch(); iwords.SetKeywords(s.Split('|')); var b = iwords.ContainsAny(test); Assert.AreEqual(true, b); var f = iwords.FindFirst(test); Assert.AreEqual("中国", f); var all = iwords.FindAll(test); Assert.AreEqual("中国", all[0]); Assert.AreEqual("国人", all[1]); Assert.AreEqual(2, all.Count); var str = iwords.Replace(test, '*'); Assert.AreEqual("我是***", str); }
static void Main(string[] args) { string value = CalculatePi(19);//Math.Round(Math.PI, 15).ToString(); string fileName = "D:\\pi_test"; int index = 0; using (System.IO.StreamReader reader = new System.IO.StreamReader(fileName)) { if (String.IsNullOrEmpty(value)) { Console.WriteLine("0"); } StringSearch valueSearch = new StringSearch(value); int readChar; while ((readChar = reader.Read()) >= 0) { ++index; if (valueSearch.Found(readChar)) { Console.WriteLine("pi was found at index {0}", index - value.Length); } } } Console.WriteLine("am I hired ?"); }
public void IssuesTest_36() { StringSearch iwords = new StringSearch(); // 此处有4000个A var words = new List<string>() { "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA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AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA" }; iwords.SetKeywords(words); }
public static List <int> Search(String source, String find) { if (tasks != null) { Task.WaitAll(tasks); tasks = null; } StringSearch.ClearFoundPos(); tasks = new Task[source.Length / charAmount + 1]; for (int i = 0; i *charAmount < source.Length; i++) { int length; if (i * charAmount + charAmount + find.Length - 1 >= source.Length) { length = source.Length - i * charAmount; } else { length = charAmount + find.Length - 1; } String subSource = source.Substring(i * charAmount, length); int currentI = i; tasks[i] = new Task(() => new StringSearch(subSource, find, currentI * charAmount).Search()); tasks[i].Start(); } Task.WaitAll(tasks); return(StringSearch.GetFoundPos()); }
/// <summary> /// Replaces the next instance of the given string in the given range of the <see cref="Scintilla"/> text with the given string. /// </summary> /// <param name="searchRange"><see cref="CharacterRange"/> in which to search.</param> /// <param name="searchString">Text for which to search.</param> /// <param name="replaceString">String to replace any matches. Can be a regular expression pattern.</param> /// <param name="searchFlags"><see cref="SearchFlags"/> enumeration that sets the pattern matching options.</param> /// <param name="wrap">Set to true to allow the search to wrap back to the beginning of the text.</param> /// <returns><see cref="CharacterRange"/> where the result was found. /// <see cref="CharacterRange.cpMin"/> will be the same as <see cref="CharacterRange.cpMax"/> if no match was found.</returns> public CharacterRange Replace(CharacterRange searchRange, string searchString, string replaceString, SearchFlags searchFlags, bool wrap) { StringSearch query = new StringSearch() { SearchRange = searchRange, SearchString = searchString, Flags = searchFlags }; return(Replace(query, replaceString, wrap)); }
/// <summary> /// Replaces all instances of the given string in the given range of the <see cref="Scintilla"/> text with the given string. /// </summary> /// <param name="searchRange"><see cref="CharacterRange"/> in which to search.</param> /// <param name="searchString">Text for which to search.</param> /// <param name="replaceString">String to replace any matches.</param> /// <param name="searchFlags"><see cref="SearchFlags"/> enumeration that sets the pattern matching options.</param> /// <param name="mark">Set to true to use the configured margin marker to indicate the lines where matches were found.</param> /// <param name="highlight">Set to true to use the configured text indicator to highlight each match.</param> /// <returns><see cref="List{CharacterRange}"/> containing the locations of every match. Empty if none were found.</returns> public List <CharacterRange> ReplaceAll(CharacterRange searchRange, string searchString, string replaceString, SearchFlags searchFlags, bool mark, bool highlight) { StringSearch query = new StringSearch() { SearchRange = searchRange, SearchString = searchString, Flags = searchFlags }; return(ReplaceAll(query, replaceString, mark, highlight)); }
/// <summary> /// Searches for the previous instance of the given string in the given range of the <see cref="Scintilla"/> text based on the current caret position, using the specified options, and optionally wrapping back to the end. /// </summary> /// <param name="searchRange"><see cref="CharacterRange"/> in which to search.</param> /// <param name="searchString">Text for which to search.</param> /// <param name="searchFlags"><see cref="SearchFlags"/> enumeration that sets the pattern matching options.</param> /// <param name="wrap">Set to true to allow the search to wrap back to the end of the text.</param> /// <returns><see cref="CharacterRange"/> where the result was found. /// <see cref="CharacterRange.cpMin"/> will be the same as <see cref="CharacterRange.cpMax"/> if no match was found.</returns> public CharacterRange FindPrevious(CharacterRange searchRange, string searchString, SearchFlags searchFlags, bool wrap) { StringSearch query = new StringSearch() { SearchRange = searchRange, SearchString = searchString, Flags = searchFlags }; return(FindPrevious(query, wrap)); }
public IEnumerable ShortBooksCatalog([FromRoute] string SearchString) { SearchString = SearchString.Replace(",", " "); SearchString = SearchString.Replace("_", " "); SearchString = SearchString.Replace("-", " "); SearchString = SearchString.Replace("(", " "); SearchString = SearchString.Replace(")", " "); SearchString = SearchString.Replace("(", " "); SearchString = SearchString.Replace(":", " "); SearchString = SearchString.Replace(";", " "); SearchString = SearchString.Replace("+", " "); SearchString = SearchString.Replace("=", " "); SearchString = SearchString.Replace("/", " "); SearchString = SearchString.Replace("*", " "); string[] m = SearchString.Split("."); var users = (from u in _context.User select u).Include(u => u.InstitutionBranch); var UserBranch = (from u in _context.User select u).Include(u => u.InstitutionBranch).Select(u => u.InstitutionBranchId).ToList(); int UserBranchId = (int)UserBranch[0]; // Cheks whether a search string was typed and prepares for search by each word string[] myString = SearchString != null ? m[1].Trim().Split(" ") : new string[0]; int MyUser = 0; int.TryParse(m[0], out MyUser); if (MyUser == 0) { return(null); } // Get the seach's recordset already sorted var books = StringSearch.SearchBooks(_context, myString); //Getting the books from the same institution var booksCatalog = (from b in books join u in users on b.UserId equals u.UserId where b.Blocked == false && u.InstitutionBranchId == UserBranchId select new { b.BookId, b.Title, b.Description, b.Edition, b.ISBN, b.Publisher, b.Price, b.BookCondition.Condition, b.StudyArea.StudyAreaName, b.User.InstitutionBranch.InstitutionBranchName }); return(booksCatalog); }
private void SetSearchStructure() { try { stringSearch = null; hashTable = null; if (dictionaryInputString != null && inputString != null) { if (settings.Search == ContainsSettings.SearchType.AhoCorasick) { // if DicDelimiter is set we have to split the input string if (settings.DelimiterDictionary.Length == 1) { stringSearch = new StringSearch(dictionaryInputString.Split(settings.DelimiterDictionary[0])); } else { string[] arr = new string[1]; arr[0] = dictionaryInputString; stringSearch = new StringSearch(arr); } } else if (settings.Search == ContainsSettings.SearchType.Hashtable) { hashTable = new Hashtable(); string[] theWords = null; if (settings.DelimiterDictionary.Length == 1) { theWords = dictionaryInputString.Split(settings.DelimiterDictionary[0]); } else { theWords = new string[1]; theWords[0] = dictionaryInputString; } foreach (string item in theWords) { string tmp = item; //if( settings.IgnoreDiacritics ) tmp = RemoveDiacritics(item); if (!hashTable.ContainsKey(tmp)) { hashTable.Add(tmp, null); } } } } } catch (Exception exception) { EventsHelper.GuiLogMessage(OnGuiLogNotificationOccured, this, new GuiLogEventArgs(exception.Message, this, NotificationLevel.Error)); EventsHelper.GuiLogMessage(OnGuiLogNotificationOccured, this, new GuiLogEventArgs(exception.StackTrace, this, NotificationLevel.Error)); } }
private void OneKeyReplace_Click(object sender, EventArgs e) { StringSearch iwords = new StringSearch(); string ChangeText = ChangeWordBox.Text; int index = MaintextBox.GetFirstCharIndexOfCurrentLine();//得到当前行第一个字符的索引 int line = GetTextboxLine(index); string ReplacedText = ReplaceBox.Text; if (ChangeText != "") { for (int i = 0; i < TitleStrList.Count(); i++) { string basestr = TitleStrList[i]; TitleStrList[i] = basestr.Replace(ChangeText, ReplacedText); } MaintextBox.Text = FormatStr(TitleStrList);//显示到textbox string extension = System.Windows.Forms.Application.StartupPath; //把关键字写入历史纪录 try { StreamReader sr = new StreamReader(extension + "\\HisToryReplaceword.txt", Encoding.GetEncoding("gb2312")); string KeyWordsStr = sr.ReadToEnd(); sr.Close(); using (FileStream fs = new FileStream(extension + "\\HisToryReplaceword.txt", FileMode.Open)) { using (StreamWriter sw = new StreamWriter(fs, Encoding.GetEncoding("gb2312"))) { sw.Write(ChangeText + "|"); sw.Write(KeyWordsStr); sw.Close(); } fs.Close(); } } catch //没有读取到文件 { using (FileStream fs = new FileStream(extension + "\\HisToryReplaceword.txt", FileMode.Create)) //新建一个文件 { using (StreamWriter newFile = new StreamWriter(fs, Encoding.GetEncoding("gb2312"))) { newFile.Write(ChangeText + "|"); newFile.Close(); } fs.Close(); } } } else { } MaintextBox.SelectionStart = getnewindex(line); MaintextBox.SelectionLength = 0; MaintextBox.ScrollToCaret();//到指定行 MaintextBox.Focus(); }
public void CanFindInMiddle() { const string toSearch = "ABCD"; var search = new StringSearch("BC"); search.Execute(toSearch); Assert.True(search.IsFound); Assert.Equal(1, search.Start); }
public void OrderedWordsBinarySearch() { var words = File .ReadAllLines(@"../../../test-data/string-words.csv") .ToList(); var found = StringSearch.BinarySearch(words, "Jword"); var didNotFind = !StringSearch.BinarySearch(words, "Anonexistingword"); Assert.IsTrue(found); Assert.IsTrue(didNotFind); }
public void CanFindAtBegining() { const string toSearch = "ABCD"; var search = new StringSearch("AB"); search.Execute(toSearch); Assert.True(search.IsFound); Assert.Equal(0, search.Start); }
public void SinglePatternAllOccurrences() { string t = "searching for a pattern in this string with multiple patterns"; string p = "pattern"; var expectedOccur = new int[] { 16, 53 }; var positions = StringSearch.RabinKarpSearchAll(t, p); Assert.IsTrue(positions.Count == expectedOccur.Length); for (int i = 0; i < expectedOccur.Length; i++) { Assert.IsTrue(positions[i] == expectedOccur[i]); } t = "test1 test2 test3 test4"; p = "test"; expectedOccur = new int[] { 0, 6, 12, 18 }; positions = StringSearch.RabinKarpSearchAll(t, p); Assert.IsTrue(positions.Count == expectedOccur.Length); for (int i = 0; i < expectedOccur.Length; i++) { Assert.IsTrue(positions[i] == expectedOccur[i]); } p = "t"; expectedOccur = new int[] { 0, 3, 6, 9, 12, 15, 18, 21 }; positions = StringSearch.RabinKarpSearchAll(t, p); Assert.IsTrue(positions.Count == expectedOccur.Length); for (int i = 0; i < expectedOccur.Length; i++) { Assert.IsTrue(positions[i] == expectedOccur[i]); } t = "кирилица"; p = "и"; expectedOccur = new int[] { 1, 3, 5 }; positions = StringSearch.RabinKarpSearchAll(t, p); Assert.IsTrue(positions.Count == expectedOccur.Length); for (int i = 0; i < expectedOccur.Length; i++) { Assert.IsTrue(positions[i] == expectedOccur[i]); } t = "no such pattern"; p = "smth"; expectedOccur = new int[] { }; positions = StringSearch.RabinKarpSearchAll(t, p); Assert.IsTrue(positions.Count == expectedOccur.Length); }
public void OrderedSpecialCharacterWordsBinarySearch() { var specialCharacterStrings = File .ReadAllLines(@"../../../test-data/string-special-characters.csv") .ToList(); var found = StringSearch.BinarySearch(specialCharacterStrings, "!Y"); var didNotFind = !StringSearch.BinarySearch(specialCharacterStrings, "%$#"); Assert.IsTrue(found); Assert.IsTrue(didNotFind); }
public void OrderedMixedWordsBinarySearch() { var mixedStrings = File .ReadAllLines(@"../../../test-data/string-mixed.csv") .ToList(); var found = StringSearch.BinarySearch(mixedStrings, "LN750"); var didNotFind = !StringSearch.BinarySearch(mixedStrings, "wordsmith"); Assert.IsTrue(found); Assert.IsTrue(didNotFind); }
/// <summary> /// 查找字符串数量 /// </summary> /// <param name="str"></param> /// <param name="search"></param> /// <returns></returns> public int SearchStr(string str, string search) { StringSearch iwords = new StringSearch(); iwords.SetKeywords(new string[1] { search }); var all = iwords.FindAll(str); return(all.Count); }
public void beforTest() { ARelation r5 = new Between_Int(1, 2, "a", "b"); ARelation r6 = new Between_String("a", "b", "c", "d"); ARelation r3 = new IntSearch(1234, "a", "b"); ARelation r4 = new EndingAttributeSearch(Ending.Attribute.Cold, "a", "b"); ARelation r1 = new StringSearch("a", "b", "c"); ARelation r2 = new ComplexRelation(); rArray = new ARelation[4] { r1, r2, r3, r4 }; }
private bool IsContainsKeyword(string TitleStr, string keyword) { StringSearch isearch = new StringSearch(); string[] keywords = { keyword }; isearch.SetKeywords(keywords); if (isearch.ContainsAny(TitleStr)) { return(true); } return(false); }
/// <summary> /// Searches for the first or last instance of the given string in the given range of the <see cref="Scintilla"/> text, using the specified options. /// </summary> /// <param name="searchRange"><see cref="CharacterRange"/> in which to search.</param> /// <param name="searchString">Text for which to search.</param> /// <param name="searchFlags"><see cref="SearchFlags"/> enumeration that sets the pattern matching options.</param> /// <param name="searchUp">Search direction. Set to true to search from the bottom up.</param> /// <returns><see cref="CharacterRange"/> where the result was found. /// <see cref="CharacterRange.cpMin"/> will be the same as <see cref="CharacterRange.cpMax"/> if no match was found.</returns> public CharacterRange Find(CharacterRange searchRange, string searchString, SearchFlags searchFlags, bool searchUp) { StringSearch query = new StringSearch() { SearchRange = searchRange, SearchString = searchString, Flags = searchFlags, SearchUp = searchUp }; return(query.Find(Editor)); }
public ActionResult Create(int articleID, string userID, string content) { try { if (string.IsNullOrWhiteSpace(content)) { return(Json(Comm.ToJsonResult("ContentIsNull", "评论不能为空"))); } var wordPath = HttpContext.Server.MapPath("~/App_Start/IllegalWords/keywords.txt"); var words = System.IO.File.ReadAllText(wordPath).SplitToArray <string>('|'); StringSearch iwords = new StringSearch(); iwords.SetKeywords(words); content = iwords.Replace(content, '*');//敏感词过滤 if (string.IsNullOrWhiteSpace(userID) || !db.Users.Any(s => s.Id == userID)) { return(Json(Comm.ToJsonResult("UserNoFound", "用户不存在"))); } if (!db.Articles.Any(s => s.ID == articleID)) { return(Json(Comm.ToJsonResult("ArticleNoFound", "文章不存在"))); } //var limitSec = 60; //var limit = DateTime.Now.AddSeconds(limitSec * -1); //if (db.ArticleComments.Any(s => s.UserID == userID && s.CreateDateTime > limit)) //{ // return Json(Comm.ToJsonResult("CreateLimit", $"评论在{limitSec}s内只能评论一次")); //} var comment = new ArticleComment { ArticleID = articleID, UserID = userID, CreateDateTime = DateTime.Now, Content = content }; db.ArticleComments.Add(comment); db.SaveChanges(); var article = db.Articles.FirstOrDefault(s => s.ID == articleID); var count = db.ArticleComments.Count(s => s.ArticleID == articleID); article.Comment = count; db.SaveChanges(); return(Json(Comm.ToJsonResult("Success", "成功", new { CommentID = comment.ID, Count = count }))); } catch (Exception ex) { return(Json(Comm.ToJsonResult("Error", ex.Message))); } }
public async Task <IList <OrganisationData> > HandleAsync(FindMatchingOrganisations query) { var searchTerm = PrepareQuery(query); // This search uses the Levenshtein edit distance as a search algorithm. var permittedDistance = CalculateMaximumLevenshteinDistance(searchTerm); var possibleOrganisations = await GetPossibleOrganisationNames(searchTerm); var uppercaseOrganisationNames = possibleOrganisations.Select(o => new KeyValuePair <string, Guid>(o.Name.ToUpperInvariant(), o.Id)) .ToArray(); // Special cases should be ignored when counting the distance. This loop replaces special cases with string.Empty. for (var i = 0; i < possibleOrganisations.Length; i++) { foreach (var specialCase in specialCases) { specialCase.CleanseSpecialCases(ref uppercaseOrganisationNames[i]); } } var matchingIdsWithDistance = new List <KeyValuePair <Guid, int> >(); for (var i = 0; i < possibleOrganisations.Length; i++) { var distance = StringSearch.CalculateLevenshteinDistance(searchTerm, uppercaseOrganisationNames[i].Key); if (distance <= permittedDistance) { matchingIdsWithDistance.Add(new KeyValuePair <Guid, int>(uppercaseOrganisationNames[i].Value, distance)); } } matchingIdsWithDistance = matchingIdsWithDistance.OrderBy(m => m.Value).ToList(); var matchingOrganisations = matchingIdsWithDistance.Select( m => possibleOrganisations.Single(o => o.Id == m.Key)); return(matchingOrganisations.Select(o => new OrganisationData { Id = o.Id, Name = o.Name }).ToList()); }
public static string ReviseRealmName(string realm) { realm = realm.ToLower(); RealmData realmInfo = new RealmData(); var realms = realmInfo._model.realms; List <string> listOfWords = new List <string>(); for (int i = 0; i < realms.Length; i++) { listOfWords.Add(realms[i].slug); } StringSearch ssearch = new StringSearch(realm, listOfWords); return(StringSearch.FindClosestMatch(ssearch)); }
public void MultiplePatternsFirstOccurence() { string t = "searching for a pattern in this string with multiple patterns"; string[] p = new string[] { "pattern", "for", "multiple", "s", "with" }; var expectedOccur = new int[] { 16, 10, 44, 0, 39 }; var positions = StringSearch.RabinKarpMultipleSearchFirst(t, p); Assert.IsTrue(positions.Count == expectedOccur.Length); for (int i = 0; i < expectedOccur.Length; i++) { Assert.IsTrue(positions[p[i]] == expectedOccur[i]); } t = "test1 test2 test3 test4"; p = new string[] { "test", "2", "test3", " " }; expectedOccur = new int[] { 0, 10, 12, 5 }; positions = StringSearch.RabinKarpMultipleSearchFirst(t, p); Assert.IsTrue(positions.Count == expectedOccur.Length); for (int i = 0; i < expectedOccur.Length; i++) { Assert.IsTrue(positions[p[i]] == expectedOccur[i]); } t = "кирлица... още малко"; p = new string[] { "и", "ца.", "още", "малко" }; expectedOccur = new int[] { 1, 5, 11, 15 }; positions = StringSearch.RabinKarpMultipleSearchFirst(t, p); Assert.IsTrue(positions.Count == expectedOccur.Length); for (int i = 0; i < expectedOccur.Length; i++) { Assert.IsTrue(positions[p[i]] == expectedOccur[i]); } t = "no such pattern"; p = new string[] { "ha", "asdf", "wtf", "test" }; expectedOccur = new int[] { }; positions = StringSearch.RabinKarpMultipleSearchFirst(t, p); Assert.IsTrue(positions.Count == expectedOccur.Length); }
public void test() { string s = "中国|国人|zg人"; string test = "我是中国人"; StringSearchEx iwords2 = new StringSearchEx(); iwords2.SetKeywords(s.Split('|')); iwords2.Save("BigStringSearchEx.dat"); StringSearchEx iwords = new StringSearchEx(); iwords.Load("BigStringSearchEx.dat"); var b = iwords.ContainsAny(test); Assert.AreEqual(true, b); var f = iwords.FindFirst(test); Assert.AreEqual("中国", f); var all = iwords.FindAll(test); Assert.AreEqual("中国", all[0]); Assert.AreEqual("国人", all[1]); Assert.AreEqual(2, all.Count); var str = iwords.Replace(test, '*'); Assert.AreEqual("我是***", str); string s2 = "ABCDEFG|BCDEF|BCDEG|CDEK|CDE"; StringSearch stringSearch = new StringSearch(); stringSearch.SetKeywords(s2.Split('|')); all = stringSearch.FindAll("AAAAAABCDEKDE"); Assert.AreEqual("CDE", all[0]); Assert.AreEqual("CDEK", all[1]); Assert.AreEqual(2, all.Count); }
public void CanFindBetween() { const string toSearch1 = "ABCD"; const string toSearch2 = "EFGH"; var search = new StringSearch("CDEFGH"); search.Execute(toSearch1); Assert.False(search.IsFound); Assert.Equal(2, search.Start); Assert.Equal(2, search.Index); search.Execute(toSearch2); Assert.True(search.IsFound); Assert.Equal(-2, search.Start); }
public void test2() { string s = "中国人|中国|国人|zg人|我是中国人|我是中国|是中国人"; string test = "我是中国人"; StringSearch iwords = new StringSearch(); iwords.SetKeywords(s.Split('|')); var all = iwords.FindAll(test); Assert.AreEqual(6, all.Count); var str = iwords.Replace(test, '*'); Assert.AreEqual("*****", str); }
public void TestStringSearch(string search, string str, int expectedStart, int expectedEnd, SearchFlags searchFlags = SearchFlags.NONE) { StringSearch.Result expected = new StringSearch.Result(expectedStart, expectedEnd); StringSearch stringSearch = new StringSearch(search, searchFlags); StringSearch.Result result1 = stringSearch.SearchIn(str, searchFlags); StringSearch.Result result2 = StringSearch.Search(search, str, searchFlags); if (expected.Found) { Assert.AreEqual(expected, result1); Assert.AreEqual(expected, result2); } else { Assert.IsFalse(result1.Found); Assert.IsFalse(result2.Found); } }
public static int FindInFile(FileStream file, string value) { // returns complement of number of characters in file if not found // else returns index where value found int index = 0; file.Seek(0, SeekOrigin.Begin); System.IO.StreamReader reader = new System.IO.StreamReader(file, Encoding.ASCII); if (String.IsNullOrEmpty(value)) return 0; StringSearch valueSearch = new StringSearch(value); int readChar; while ((readChar = reader.Read()) >= 0) { ++index; if (valueSearch.Found(readChar)) return index - value.Length; } return ~index; }
public void CreateStringSearch() { ss = new StringSearch(); }
public void cant_init() { this.s1 = new StringSearch(Relator.Unary.IS, null); }
public void cant_init2() { this.s1 = new StringSearch(Relator.Unary.NOT, ""); }
public void cant_init3() { this.s1 = new StringSearch(""); }
public void runBeforTest() { this.s1 = new StringSearch("doof", Track.Property.Titel.ToString(), Titel.Property.Name.ToString()); this.s2 = new StringSearch("doof", Track.Property.Titel.ToString(), Titel.Property.Name.ToString()); }
public void can_do() { ASimpleRelation NotTITEL = new StringSearch(Relator.Unary.NOT, this.titel, Track.Property.Titel.ToString(), Titel.Property.Name.ToString()); ASimpleRelation NotAUTOR = new StringSearch(Relator.Unary.NOT, this.autor, Track.Property.Autor.ToString(), Autor.Property.Name.ToString()); OR.addRelata(TITEL, AUTOR); IList<Track> titel_oder_autor = TrackSearch_Service.getTrackListing(OR); IList<Track> titel_and_not_autor = TrackSearch_Service.getTrackListing(new ComplexRelation(TITEL, NotAUTOR)); IList<Track> not_titel_and_autor = TrackSearch_Service.getTrackListing(new ComplexRelation(NotTITEL, AUTOR)); foreach(Track track in titel_oder_autor) { Assert.IsTrue( ( Regex.IsMatch(track.Titel.Name, titel, RegexOptions.IgnoreCase) || Regex.IsMatch(track.Autor.Name, autor, RegexOptions.IgnoreCase) ) ); } foreach(Track track in titel_and_not_autor) { Assert.IsTrue( ( Regex.IsMatch(track.Titel.Name, titel, RegexOptions.IgnoreCase) && !Regex.IsMatch(track.Autor.Name, autor, RegexOptions.IgnoreCase) ) ); } foreach(Track track in not_titel_and_autor) { Assert.IsTrue( ( !Regex.IsMatch(track.Titel.Name, titel, RegexOptions.IgnoreCase) && Regex.IsMatch(track.Autor.Name, autor, RegexOptions.IgnoreCase) ) ); } }
public void can_getTrackListing_by_Titel_and_not_Autor_OR_not_Titel_or_Autor() { ASimpleRelation NotTITEL = new StringSearch(Relator.Unary.NOT, this.titel, Track.Property.Titel.ToString(), Titel.Property.Name.ToString()); ASimpleRelation NotAUTOR = new StringSearch(Relator.Unary.NOT, this.autor, Track.Property.Autor.ToString(), Autor.Property.Name.ToString()); AND.addRelata(NotTITEL, AUTOR); OR.addRelata(TITEL, NotAUTOR); IList<Track> trackListing = TrackSearch_Service.getTrackListing(new ComplexRelation(Relator.Binary.OR, AND, OR)); this.showResult(trackListing); this.showCount(trackListing); foreach(Track track in trackListing) { Assert.IsTrue( ( !Regex.IsMatch(track.Titel.Name, titel, RegexOptions.IgnoreCase) && Regex.IsMatch(track.Autor.Name, autor, RegexOptions.IgnoreCase) ) || ( Regex.IsMatch(track.Titel.Name, titel, RegexOptions.IgnoreCase) || !Regex.IsMatch(track.Autor.Name, autor, RegexOptions.IgnoreCase) ) ); } }
public void can_do_Titel_und_nicht_Titel() { ASimpleRelation NotTITEL = new StringSearch(Relator.Unary.NOT, this.titel, Track.Property.Titel.ToString(), Titel.Property.Name.ToString()); IList<Track> trackListing = TrackSearch_Service.getTrackListing(new ComplexRelation(this.TITEL, NotTITEL)); this.showResult(trackListing); this.showCount(trackListing); foreach(Track track in trackListing) { Assert.IsTrue( ( Regex.IsMatch(track.Titel.Name, titel, RegexOptions.IgnoreCase) && !Regex.IsMatch(track.Titel.Name, titel, RegexOptions.IgnoreCase) ) ); } }