public int FindCategoryBM(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) { BoyerMoore bm = new BoyerMoore(item.Data.ToLower()); if (bm.Search(TField) != -1) { Find.Add(item); } } else { StringPattern P = new StringPattern(); P = item; bool valid = true; while (P != null) { BoyerMoore bm = new BoyerMoore(P.Data.ToLower()); if (bm.Search(TField) == -1) { valid = false; break; } P = P.Next; } if (valid == true) { Find.Add(item); } } } int min = 999; foreach (var item in Find) { BoyerMoore bm = new BoyerMoore(item.Data.ToLower()); if (bm.Search(TField) < min) { min = bm.Search(TField); } } foreach (var item in Find) { BoyerMoore bm = new BoyerMoore(item.Data.ToLower()); if (bm.Search(TField) == min) { kategori = item.Kategori; } } return(kategori); }
public void TestBoyerMoore() { var search = new BoyerMoore(); //Assert.AreEqual(search.Search("this is a string", "is"), 2); Assert.AreEqual(search.Search("this this a ac tac string", "tac"), 15); }
private static void SearchPattern(string pat, string txt) { var pattern = pat.ToCharArray(); var text = txt.ToCharArray(); var boyerMoore1 = new BoyerMoore(pat); var offset1 = boyerMoore1.Search(txt); var boyerMoore2 = new BoyerMoore(pattern, 256); var offset2 = boyerMoore2.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); }
public void Test() { String text = "o fare thee well, poor devil of a Sub-Sub, whose commen- \n" + "tator I am. Thou belongest to that hopeless, sallow tribe \n" + "which no wine of this world will ever warm ; and for whom \n" + "even Pale Sherry would be too rosy-strong ; but with whom \n" + "one sometimes loves to sit, and feel poor-devilish, too ; and \n" + "grow convivial upon tears ; and say to them bluntly with full \n" + "eyes and empty glasses, and in not altogether unpleasant \n" + "sadness Give it up, Sub-Subs ! For by how much the more \n" + "pains ye take to please the world, by so much the more shall \n" + "ye forever go thankless ! Would that I could clear out \n" + "Hampton Court and the Tuileries for ye ! But gulp down \n" + "your tears and hie aloft to the royal-mast with your hearts ; \n" + "for your friends who have gone before are clearing out the \n" + "seven-storied heavens, and making refugees of long-pampered \n" + "Gabriel, Michael, and Raphael, against your coming. Here \n" + "ye strike but splintered hearts together there, ye shall \n" + "strike unsplinterable glasses! "; BoyerMoore bm = new BoyerMoore("the"); print("found at " + bm.Search(text)); Assert.NotEqual(-1, bm.Search(text)); }
private static int GetFirstEndOfJpeg(byte[] fileBytes, out BoyerMoore bm) { var endOfJpegBytes = MovingPhotoExtraction.ToBytes("FF D9".Split(' ')); bm = new BoyerMoore(endOfJpegBytes); int endOfJpeg = bm.Search(fileBytes); return(endOfJpeg); }
void SubstringTest() { var strArr = FileHandler.ReadFileAsStrArr("words3.txt"); var text = string.Join("", strArr); Console.WriteLine(text); var bm = new BoyerMoore("zooni"); Console.WriteLine(bm.Search(text)); //Console.WriteLine(Brute.Search(text, "aaa")); //Console.WriteLine(Brute.Search(text, "yet")); //Console.WriteLine(Brute.Search(text, "ye2")); //Console.WriteLine(Brute.Search(text, "yet2")); //Console.WriteLine(Brute.Search(text, "zoo")); Console.ReadKey(); }
public static void Main() { var trie = new BoyerMoore("lorem"); string text = File.ReadAllText("../../largeTextFile.txt"); int count = 0; int nextIndex = 0; while (true) { nextIndex = trie.Search(text, nextIndex + 1); if (nextIndex == -1) { break; } count += 1; } Console.WriteLine(count); }
private void InitializeScanMemory() { var sysInfo = new SYSTEM_INFO(); GetSystemInfo(out sysInfo); var mbi = new MEMORY_BASIC_INFORMATION(); IntPtr procHandle = OpenProcess(0x10 | 0x20 | 0x400, false, process.Id); uint mbiSize = (uint)Marshal.SizeOf(mbi); uint minAddr = 0; uint maxAddr = (uint)sysInfo.MaxAddress; bool isDone = false; long findAddress = 0; do { VirtualQueryEx(procHandle, sysInfo.MinAddress, out mbi, mbiSize); byte[] buffer = new byte[(int)mbi.RegionSize]; int readBytes = 0; if (mbi.RegionSize.ToInt32() > 0) { ReadProcessMemory(procHandle, (IntPtr)minAddr, buffer, buffer.Length, out readBytes); if (readBytes > 0) { var searchEngine = new BoyerMoore(AstConstants.SCAN_PATTERN); int resultOffset = searchEngine.Search(buffer); if (resultOffset > 0) { findAddress = minAddr + resultOffset; isDone = true; } } } minAddr += (uint)mbi.RegionSize; } while (minAddr < maxAddr && !isDone); baseAddress = IntPtr.Add((IntPtr)findAddress, -0x4006); Memory.WriteInt32(baseAddress, 142857); }
public void Search(string pattern) { for (int i = _buttonContainer.childCount - 1; i >= 0; i--) { Destroy(_buttonContainer.GetChild(i).gameObject); } var searchAlgorithm = new BoyerMoore(pattern, true); foreach (var dataSet in _dataSets) { var shiftValue = searchAlgorithm.Search(dataSet); if (shiftValue >= 0 && shiftValue <= 1) { Debug.Log($"{shiftValue} {dataSet}"); var g = GameObject.Instantiate(_buttonPrefab, _buttonContainer); g.GetComponentInChildren <TextMeshProUGUI>().text = dataSet; } } }
public static bool Read(string file, List <byte[]> byteSearch) { //var r1 = new ExifLib.ExifReader(file); //var m1 = MetadataExtractor.ImageMetadataReader.ReadMetadata(file); Console.Write("Searching " + file); var fileBytes = System.IO.File.ReadAllBytes(file); int indexOfMp4 = -1; foreach (var search in byteSearch) { indexOfMp4 = new BoyerMoore(search).Search(fileBytes); if (indexOfMp4 >= 0) { break; } } if (indexOfMp4 >= 0) { Console.WriteLine(" Found the video"); WriteVideo(file, fileBytes, indexOfMp4); return(true); } else { Console.Write(" Did not find known video, trying generic approach"); // http://dev.exiv2.org/projects/exiv2/wiki/The_Metadata_in_JPEG_files says that a JPEG start of image with 0xFFD8 and ends with 0xFFD9. var endOfJpeg = MovingPhotoExtraction.ToBytes("FF D9".Split(' ')); var indexOfMp4_Part1 = MovingPhotoExtraction.ToBytes("00 00 00".Split(' ')); var indexOfMp4_Part2 = MovingPhotoExtraction.ToBytes("66 74 79 70".Split(' ')); var indexOfMp4_Part3 = MovingPhotoExtraction.ToBytes("6D 70 34 32".Split(' ')); var bm = new BoyerMoore(endOfJpeg); var endOf = bm.Search(fileBytes); bm.SetPattern(indexOfMp4_Part2); var part2s = bm.SearchAll(fileBytes); bm.SetPattern(indexOfMp4_Part3); foreach (var part2 in part2s) { if (part2 < endOf) { Console.Write("Not yet at end of jpeg"); } var part3 = bm.SearchAll(fileBytes).FirstOrDefault(a => a > part2); // part 3 is just a bit further than part2 if (part3 > part2 && part2 + 20 > part3) { var minus4 = fileBytes[part2 - 4]; var minus3 = fileBytes[part2 - 3]; var minus2 = fileBytes[part2 - 2]; var minus1 = fileBytes[part2 - 1]; if (minus4 == 0 && minus3 == 0 && minus2 == 0) { Console.WriteLine("... Found video via pattern search"); WriteVideo(file, fileBytes, part2 - 4); return(true); } Console.WriteLine($"... Found part2 and 3, not 1: {minus4} {minus3} {minus2} {minus1}"); } } Console.WriteLine($"Failed to find the video out of {part2s.Count} possibilites"); Console.WriteLine("Please report this output to https://github.com/cliveontoast/GoMoPho/issues and attach your moving image jpeg file"); } return(false); }
/// <inheritdoc /> internal override HandleResult OnReceive <TSender>(TSender sender, IntPtr connId, byte[] data, ParseRequestBody <TSender, TRequestBodyType> parseRequestBody) { var cache = _dataReceiveAdapterCache.Get(connId); if (cache == null) { return(HandleResult.Error); } cache.Data.AddRange(data); if (cache.Data.Count > data.Length) { data = cache.Data.ToArray(); } var startLength = _startBoyerMoore.PatternLength; var endLength = _endBoyerMoore.PatternLength; HandleResult result; var lastPosition = 0; do { // 长度不够一个包等下次再来 if (data.Length - lastPosition < startLength + endLength) { result = HandleResult.Ok; break; } // 搜索起始标志 var startPosition = _startBoyerMoore.Search(data, lastPosition); // 是否找到了 if (startPosition == -1) { result = HandleResult.Error; break; } startPosition = lastPosition + startPosition + startLength; // 搜索结束标志, 从起始位置+起始标志长度开始找 var count = _endBoyerMoore.Search(data, startPosition); if (count == -1) { result = HandleResult.Ignore; break; } // 得到一条完整数据包 var bodyData = cache.Data.GetRange(startPosition, count).ToArray(); lastPosition += count + startLength + endLength; // 包体解析对象从适配器子类中获取 var obj = ParseRequestBody(bodyData); // 调用解析请求包体事件 if (parseRequestBody.Invoke(sender, connId, obj) == HandleResult.Error) { result = HandleResult.Error; break; } // 下次继续解析 } while (true); if (lastPosition > 0) { // 再移除已经读了的数据 cache.Data.RemoveRange(0, lastPosition); } return(result); }
public static int Count(string pattern, string source, SearchAlgorithm sa) { int id = (int)sa; List <int> res = new List <int>(); if (id == 0) { res = ApostolicoCrochemore.Search(pattern, source); preProcessTime = ApostolicoCrochemore.preProcessTime; searchTime = ApostolicoCrochemore.searchTime; return(res.Count); } else if (id == 1) { res = ApostolicoGiancarlo.Search(pattern, source); preProcessTime = ApostolicoGiancarlo.preProcessTime; searchTime = ApostolicoGiancarlo.searchTime; return(res.Count); } else if (id == 2) { res = BackwardNondeterministicDawgMatching.Search(pattern, source); preProcessTime = BackwardNondeterministicDawgMatching.preProcessTime; searchTime = BackwardNondeterministicDawgMatching.searchTime; return(res.Count); } else if (id == 3) { res = BackwardOracleMatching.Search(pattern, source); preProcessTime = BackwardOracleMatching.preProcessTime; searchTime = BackwardOracleMatching.searchTime; return(res.Count); } else if (id == 4) { res = BerryRavindran.Search(pattern, source); preProcessTime = BerryRavindran.preProcessTime; searchTime = BerryRavindran.searchTime; return(res.Count); } else if (id == 5) { res = BoyerMoore.Search(pattern, source); preProcessTime = BoyerMoore.preProcessTime; searchTime = BoyerMoore.searchTime; return(res.Count); } else if (id == 6) { res = BruteForce.Search(pattern, source); preProcessTime = BruteForce.preProcessTime; searchTime = BruteForce.searchTime; return(res.Count); } else if (id == 7) { res = Colussi.Search(pattern, source); preProcessTime = Colussi.preProcessTime; searchTime = Colussi.searchTime; return(res.Count); } else if (id == 8) { res = DeterministicFiniteAutomaton.Search(pattern, source); preProcessTime = DeterministicFiniteAutomaton.preProcessTime; searchTime = DeterministicFiniteAutomaton.searchTime; return(res.Count); } else if (id == 9) { res = ForwardDawgMatching.Search(pattern, source); preProcessTime = ForwardDawgMatching.preProcessTime; searchTime = ForwardDawgMatching.searchTime; return(res.Count); } else if (id == 10) { res = GalilGiancarlo.Search(pattern, source); preProcessTime = GalilGiancarlo.preProcessTime; searchTime = GalilGiancarlo.searchTime; return(res.Count); } else if (id == 11) { res = Horspool.Search(pattern, source); preProcessTime = Horspool.preProcessTime; searchTime = Horspool.searchTime; return(res.Count); } else if (id == 12) { res = KarpRabin.Search(pattern, source); preProcessTime = KarpRabin.preProcessTime; searchTime = KarpRabin.searchTime; return(res.Count); } else if (id == 13) { res = KMPSkipSearch.Search(pattern, source); preProcessTime = KMPSkipSearch.preProcessTime; searchTime = KMPSkipSearch.searchTime; return(res.Count); } else if (id == 14) { res = KnuthMorrisPratt.Search(pattern, source); preProcessTime = KnuthMorrisPratt.preProcessTime; searchTime = KnuthMorrisPratt.searchTime; return(res.Count); } else if (id == 15) { res = MaximalShift.Search(pattern, source); preProcessTime = MaximalShift.preProcessTime; searchTime = MaximalShift.searchTime; return(res.Count); } else if (id == 16) { res = MorrisPratt.Search(pattern, source); preProcessTime = MorrisPratt.preProcessTime; searchTime = MorrisPratt.searchTime; return(res.Count); } else if (id == 17) { res = NotSoNaive.Search(pattern, source); preProcessTime = NotSoNaive.preProcessTime; searchTime = NotSoNaive.searchTime; return(res.Count); } else if (id == 18) { res = OptimalMismatch.Search(pattern, source); preProcessTime = OptimalMismatch.preProcessTime; searchTime = OptimalMismatch.searchTime; return(res.Count); } else if (id == 19) { res = QuickSearch.Search(pattern, source); preProcessTime = QuickSearch.preProcessTime; searchTime = QuickSearch.searchTime; return(res.Count); } else if (id == 20) { res = Raita.Search(pattern, source); preProcessTime = Raita.preProcessTime; searchTime = Raita.searchTime; return(res.Count); } else if (id == 21) { res = ReverseColussi.Search(pattern, source); preProcessTime = ReverseColussi.preProcessTime; searchTime = ReverseColussi.searchTime; return(res.Count); } else if (id == 22) { res = ReverseFactor.Search(pattern, source); preProcessTime = ReverseFactor.preProcessTime; searchTime = ReverseFactor.searchTime; return(res.Count); } else if (id == 23) { res = ShiftOr.Search(pattern, source); preProcessTime = ShiftOr.preProcessTime; searchTime = ShiftOr.searchTime; return(res.Count); } else if (id == 24) { res = Simon.Search(pattern, source); preProcessTime = Simon.preProcessTime; searchTime = Simon.searchTime; return(res.Count); } else if (id == 25) { res = SkipSearch.Search(pattern, source); preProcessTime = SkipSearch.preProcessTime; searchTime = SkipSearch.searchTime; return(res.Count); } else if (id == 26) { res = Smith.Search(pattern, source); preProcessTime = Smith.preProcessTime; searchTime = Smith.searchTime; return(res.Count); } else if (id == 27) { res = StringMatchingonOrderedAlphabets.Search(pattern, source); preProcessTime = StringMatchingonOrderedAlphabets.preProcessTime; searchTime = StringMatchingonOrderedAlphabets.searchTime; return(res.Count); } else if (id == 28) { res = TunedBoyerMoore.Search(pattern, source); preProcessTime = TunedBoyerMoore.preProcessTime; searchTime = TunedBoyerMoore.searchTime; return(res.Count); } else if (id == 29) { res = TurboBM.Search(pattern, source); preProcessTime = TurboBM.preProcessTime; searchTime = TurboBM.searchTime; return(res.Count); } else if (id == 30) { res = TurboReverseFactor.Search(pattern, source); preProcessTime = TurboReverseFactor.preProcessTime; searchTime = TurboReverseFactor.searchTime; return(res.Count); } else if (id == 31) { res = TwoWay.Search(pattern, source); preProcessTime = TwoWay.preProcessTime; searchTime = TwoWay.searchTime; return(res.Count); } else if (id == 32) { res = ZhuTakaoka.Search(pattern, source); preProcessTime = ZhuTakaoka.preProcessTime; searchTime = ZhuTakaoka.searchTime; return(res.Count); } else if (id == 33) { res = NET_IndexOf.Search(pattern, source); preProcessTime = NET_IndexOf.preProcessTime; searchTime = NET_IndexOf.searchTime; return(res.Count); } else if (id == 34) { res = NET_IndexOf_Ordinal.Search(pattern, source); preProcessTime = NET_IndexOf_Ordinal.preProcessTime; searchTime = NET_IndexOf_Ordinal.searchTime; return(res.Count); } else { return(0); } }
public int BMSeartch() { return(BM.Search(txt)); }