Reputation:
Working with a Visual Basic.NET console application that features a VERY BASIC natural language recognition function-it only has to work with specific fields of words so it wasn't that hard. I got the logic all lay out on paper, but I've encountered a problem.
I want the application to check if the user input is only consist of valid words.
When the user inputs something, I used a function to cut it down to only alphanumeric characters, and used the string.split method to create an array that contains separate words the user input. What I wish to do now is to compare the input array to another array (a full set of valid words), and return an error message if the input array contains only elements that exist in the valid words array.
For instance, if all valid words are "ALPHA", "BETA" and "GAMMA". When the use inputs something like "ALPHA BETA"-the program will accept the input. If the input is "APPLES" then it will return an error message because the string APPLE is not a member of the valid words array.
I hope I've made my question clear enough, anyone please help. Thanks.
Upvotes: 2
Views: 3136
Reputation: 1809
In your case, it is not necessary to use array as data structure. Instead, you can store your list of valid words in System.Collections.Specialized.NameValueCollection
class. Then, you can test each user input against the list of valid words that stored in System.Collections.Specialized.NameValueCollection
class.
How to store valid word in System.Collections.Specialized.NameValueCollection
class:
See this.
How to check if there is valid word entry in System.Collections.Specialized.NameValueCollection
class: See this
Upvotes: 1
Reputation: 1324
You could use a HashSet(T) to store your allowed words. HashSet.Contains
is an O(1) operation rather than an O(n) found in other collections, which makes a HashSet(T)
more efficient.
Private Shared AllowedWords As New HashSet(Of String)(StringComparer.OrdinalIgnoreCase) From
{
"Alpha", "Beta", "Gamma"
}
Public Shared Function WordsAreValid(ParamArray words() As String) As Boolean
For Each word As String In words
If Not AllowedWords.Contains(word) Then Return False
Next
Return True
End Function
Upvotes: 1
Reputation: 2064
I'd use Except, http://msdn.microsoft.com/en-us/library/bb300779.aspx
Private AllowedWords As String() = {"these", "are", "good", "words"}
Sub Main()
Dim badUserInput As String() = {"these", "are", "bad", "words"}
Dim badWords As IEnumerable(Of String) = badUserInput.Except(AllowedWords)
If badWords.Any Then
' User has entered a disallowed word
Throw New ArgumentException(String.Format("Words '{0}' are not allowed", String.Join(",", badWords)))
End If
End Sub
I also wrote a blog on different ways to join collections a while ago:
http://dotnetrene.blogspot.co.uk/2012/01/joining-collections-in-linq-contains-vs.html
Upvotes: 2
Reputation: 101042
Maybe you are looking for something like the All()-Method, which checks if each element in a collection satisfies a condition. Consider the following example:
Dim validWords = {"ALPHA", "BETA", "GAMMA"}
Dim thisIsNotValid = {"ALPHA", "APPLES"}.All(Function(word) validWords.Contains(word))
Dim thisIsValid = {"ALPHA", "BETA"}.All(Function(word) validWords.Contains(word))
thisIsNotValid
will evaluate to False
, and thisIsValid
will evaluate to True
.
Upvotes: 3