How to match word patterns with Visual Basic
Someone recently asked a question about solving crypto class="content-fragment-top fiji-content-fragment-top">
Someone recently asked a question about solving crypto class="content-fragment-top fiji-content-fragment-top">
This code is the core of the example linked in the section below, in order to see an example of it's implementation, you will need to download the example project below.
''' <summary>''' This function calculates likely word matches for cryptogram words.''' </summary>''' <param name="Word">The encrypted word</param>''' <param name="Dictionary">A list of words to match the encrypted word against.</param>''' <param name="Filter">A filter pattern for reducing results.</param>''' <param name="PB">Optional Progressbar to report progress.</param>''' <param name="UpdateLabel">Optional Label to report current match count.</param>''' <returns></returns>''' <remarks></remarks>FunctionGetWordPatternMatches(WordAsString, _DictionaryAsList(OfString), _OptionalFilterAsString="*", _OptionalPBAsProgressBar =Nothing, _OptionalUpdateLabelAsLabel =Nothing) _AsListViewItem()'If the user specified a progressbar, then update the valuesIfNotPBIs">AsLabel =Nothing) _&nbsNothingThenPB.Value = 0IfNotPBIsNothingThenPB.Maximum = 0'A list of identifications for pattern matchingConstLegendAsString="01234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ"'return an empty array if there is no word to matchIfWord.Length = 0ThenReturn{}'Create a new pattern tableDimmapAsNewList(Of pt), I = 0, WordPatternAsString=""'If the user specified a progressbar, then update the valuesIfNotPBIsNothingThenPB.Maximum += Word.Count'Examine each letter in the encrypted wordForEachSAsStringInWord'If the user specified a progressbar, then update the valuesIfNotPBIsNothingThenPB.Increment(1)'search the pattern table to see if the letter was already assigned an identificationDimQ1 = From PInmap Where P.Letter = SSelectP'If it has then use the same identification for that letterIfNotQ1.ToArray.Count = 0Thenmap.Add(Newpt(Q1.ToArray(0).ID, S)) : ContinueFor'If it has not, then assign a new pattern identificationmap.Add(Newpt((Legend)(I), S))'Increment the next pattern id index numberI += 1Next'If the user specified a progressbar, then update the valuesIfNotPBIsNothingThenPB.Maximum += map.Count'Go through each mapped letterForEachPAsptInmap'If the user specified a progressbar, then update the valuesIfNotPBIsNothingThenPB.Increment(1)'Assemble the encrypted word's patternWordPattern = WordPattern & P.ID :Next'Get all word from the dictionary that are:'A.) The same length of the bord'B.) Match the FILTER specified'Assemble the encrypted word's patternWordPattern = WordPattern & P.ID :DimQ2 = From WInDictionary Where (W.Length = Word.Length)And(WLikeFilter)SelectW'Create a list for holding the resultDimresultsAsNewList(OfString)'If the user specified a progressbar, then update the valuesIfNotPBIsNothingThenPB.Maximum += Q2.ToArray.Count'Go through each dictionary word from the LINQ resultForEachWInQ2.ToArray'If the user specified a progressbar, then update the valuesIfNotPBIsNothingThenPB.Increment(1)'Create a pattern map for each word from the LINQ result, create a' legend index counter, create a dictionary word pattern to compare against the encrypted word patternDimmap2AsNewList(Of pt), I2 = 0, DictPatternAsString=""'Go through each character, of each word from the LINQ resultForEachSAsStringInW'search the pattern table to see if the letter was already assigned an identificationForEachSDimQ3 = From PInmap2 Where P.Letter = SSelectP'If it has then use the same identification for that letterIfNotQ3.ToArray.Count = 0Thenmap2.Add(Newpt(Q3.ToArray(0).ID, S)) : ContinueFor'If it has not, then assign a new pattern identificationmap2.Add(Newpt((Legend)(I2), S))'Increment the next pattern id index numberI2 += 1 :Next'Go through each mapped letterForEachPAsptInmap2'Assemble the dictionary word's patternDictPattern = DictPattern & P.IDNext'Compare the encrypted word's pattern to the pattern of each result from the LINQ query(Q2)IfDictPattern = WordPatternThenresults.Add(W)'If the user provided a label to update statusIfNotUpdateLabelIsNothingThen'Change the label's text to reflect the current matches foundUpdateLabel.Text = results.Count &" matches found so far..."'refresh the label/appApplication.DoEvents()EndIfNext'Create a list for returning the final resultsDimItemsAsNewList(Of ListViewItem)'If the user specified a progressbar, then update the valuesIfNotPBIsNothingThenPB.Maximum += results.CountForEachSAsStringInresults'If the user specified a progressbar, then update the valuesIfNotPBIsNothingThenPB.Increment(1)'Create a new listview item with subitem(0) being the encrypted wordDimItemAsNewListViewItem(Word)'Add 2 subitems to the item(Dictionary word, the pattern that they were matched with)Item.SubItems.AddRange({S, WordPattern})'Add the item to the final resultsItems.Add(Item)Next'convert the resuts and returnary word, the pattern that they were matched with)Item.SubItems.AddRange({S, WordPattern})ReturnItems.ToArrayEndFunctionPrivateClasspt' Pattern Table'I.e. The letter can only receive this ID, this ID can only represent this letterPublicID, LetterAsStringSubNew(IDAsString, LetterAsString)'Populate the ID and Letter values of this pattern tableMe.ID = ID :Me.Letter = LetterEndSubEndClass
Please view my other wiki articles!
Please update this article if you see any mistakes.