// FUNGSI UTAMA // Mengklasifikasikan 'tweet' berdasarkan 'keyword' ke dalam dinas yang tepat pada dinasList public int classify(string tweet_initial) { int index = -1, iDinas = -1, i = 0; int min = -1; string str = null; string tweet = corrector.Corrected(tweet_initial); // Search keyword in tweet using KMP foreach (string[] dinas in keyword) { foreach (string key in dinas) { if (!useBm) { index = KnuthMorrisPratt.KMPSearch(tweet, key); } else { index = BoyerMoore.BMSearch(tweet, key); } if (index != -1) { if (min == -1) { min = index; str = key; iDinas = i; } else { if (index < min) { min = index; str = key; iDinas = i; } } } } i++; } Console.WriteLine(); if (min != -1) { Console.WriteLine("Found {0} in index {1}\nDisposisi \"{2}\" ke dinas {3}", str, min, tweet, this.dinasList [iDinas]); data.Add(iDinas); return(iDinas); } else { Console.WriteLine("Couldn't find any keyword in tweet\nDisposisi \"{0}\" ke dinas {1}", tweet, this.dinasList [dinasList.Count - 1]); data.Add(dinasList.Count - 1); return(dinasList.Count - 1); } }
public void ShouldAbortOnFirstOccurence() { const string text = "That is my text with the word text 3 times. That is why text again"; const string pattern = "Text"; var occurrences = KnuthMorrisPratt.FindAll(text, pattern, true, true).ToList(); Assert.Single(occurrences); Assert.Equal(11, occurrences[0]); }
public void SearchTest() { const string text = "abcdabcabcdabcdab"; const string pattern = "abca"; var result = KnuthMorrisPratt.Search(text, pattern); const int expectedResult = 4; Assert.IsTrue(result == expectedResult, "Found an incorrect result"); }
public void ShouldFindAllOccurrences() { const string text = "That is my text with the word text 3 times. That is why text again"; const string pattern = "Text"; var occurrences = KnuthMorrisPratt.FindAll(text, pattern, true).ToList(); Assert.Equal(3, occurrences.Count); Assert.Equal(11, occurrences[0]); Assert.Equal(30, occurrences[1]); Assert.Equal(56, occurrences[2]); }
private static void FindPattern(string text, string pattern, Dictionary <string, int> patterns) { if (patterns.ContainsKey(pattern)) { return; } ISearch kmpSearch = new KnuthMorrisPratt(); List <int> searchResult = kmpSearch.Search(text, pattern)?.ToList(); if (searchResult != null && searchResult.Any() && searchResult.Count > 1) { patterns.Add(pattern, searchResult.Count); } }
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! "; KnuthMorrisPratt bm = new KnuthMorrisPratt("the"); print("found at " + bm.Search(text)); Assert.NotEqual(-1, bm.Search(text)); }
public void KMP_Test() { ISearch kmpSearch = new KnuthMorrisPratt(); AssertStringSearch(kmpSearch); }
public void KnuthMorrisPrattPrattFindAll() => KnuthMorrisPratt.FindAll(Text, Word).ToList();
public void KnuthMorrisPrattContains() => KnuthMorrisPratt.HasPattern(Text, Word);
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 void GivenPatternLongerThanText_EmptyArray() { var hasHit = KnuthMorrisPratt.HasPattern("t", "longer"); Assert.False(hasHit); }
public void GivenNoHit_ThenEmptyArray() { var occurrences = KnuthMorrisPratt.FindAll("Word", "Text"); Assert.Empty(occurrences); }
public void ShouldReturnIfOccurrenceInText() { var occurrence = KnuthMorrisPratt.HasPattern("KnuthMorrisPratt", "t"); Assert.True(occurrence); }
public void ShouldReturnEmptyOccurrences_WhenGivenNullOrEmpty(string text, string pattern) { var occurrences = KnuthMorrisPratt.FindAll(text, pattern); Assert.Empty(occurrences); }