2

I am making a program that solves anagrams in Visual Basic. How can I check if a string given by the anagram solver is a real word? I know I will have to access some sort of dictionary but I have no idea how to do this?

I need a function that checks the word to return a true/false boolean value. Is this possible?

I'm using Visual Basic in Microsoft's VS2015.

  • Of course this is possible, but given you haven't said what you've tried so far, or even what format the dictionary will be in, it will be difficult to help you. – Carcigenicate Feb 14 '17 at 17:33
  • @Carcigenicate So far I've got the code that generates the random string of letters (see below) however I have no idea how dictionaries work in Visual Basic and so that's what I'm also asking for help in. What format would you recommend? – michaelrh04 Feb 14 '17 at 17:35
  • `Dim lettersgiven() As Char = {"a"c, "b"c, "d"c, "e"c, "f"c} Dim results As New List(Of String) For start As Integer = 0 To lettersgiven.Length - 1 Dim usedindices As New List(Of Integer) For j As Integer = 1 To lettersgiven.Length Dim i As Integer = start While usedindices.Contains(i) i = (i + 1) Mod lettersgiven.Length End While usedindices.Add(i) Next Dim sb As New System.Text.StringBuilder() For Each i As Integer In usedindices sb.Append(lettersgiven(i)) Next If LookupDictionary(sb.ToString()) Then results.Add(sb.ToString()) Next` – michaelrh04 Feb 14 '17 at 17:36
  • Have you thought of making requests to some free dictionary API's? – gdrt Feb 14 '17 at 17:37
  • http://stackoverflow.com/q/2213607/1043824 gives a file with lots of words. You can search through it to check for existance. Searching is tough in flat files. You may want to put it in a database and sql search it. Hitting an api is a neat idea, but i guess it is over the high water mark. – inquisitive Feb 14 '17 at 17:44
  • @gdrt94 thanks for your answer. I'll try that out and get back to you. – michaelrh04 Feb 14 '17 at 17:56
  • @inquisitive thank you! – michaelrh04 Feb 14 '17 at 17:57
  • Check http://www.dictionaryapi.com/products/api-collegiate-dictionary.htm – gdrt Feb 14 '17 at 17:59
  • One solution is to check your word has no spaces first then use one of the myriads of spell checking examples. If it comes back as an error...it is not a real word. However, spell checking is not that easy either for a newb. – Trevor_G Feb 14 '17 at 18:12
  • Btw if you are using WPF the spell check part is easy. If you are using windows forms I suggest you create and use a WPF control on your form. – Trevor_G Feb 14 '17 at 19:30

2 Answers2

4

Hunspell is pretty easy to use.

  • Install the .net-library through Nuget (open your project in Visual Studio, then > Extras > Nuget-Package-Manager -> Console, type Install-Package NHunspell)
  • Download the .aiff and .dic files, see the dictionaries link on the Hunspell project page. Include these files in your project or use absolute paths.

Sample Code:

Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
    Using h As New NHunspell.Hunspell(
        "...path ...\en_US.aff",
        "...path ...\en_US.dic")
        Me.TextBox1.BackColor = If(h.Spell(Me.TextBox1.Text),
            Color.PaleGreen, Color.PeachPuff)
    End Using
End Sub

Hunspell

.net library NHunspell

NHunspell C# Code Samples

KekuSemau
  • 6,830
  • 4
  • 24
  • 34
1

If your are using WPF then checking if a word in a textbox can be done simply by checking if it has a spelling error.

Public Function WordOk(Word As String) As Boolean
    return TextBox1.GetNextSpellingErrorCharacterIndex(0, Windows.Documents.LogicalDirection.Forward) < 0 
End Function

If you are using windows forms then you can create a "User Control (WPF)" to do the same thing, though it is a bit tricky to explain how to do that here.

(There may be a better test than the one I showed though.. I'm not overly familiar with WPF)

Trevor_G
  • 1,331
  • 1
  • 8
  • 17