Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
• Status: Solved
• Priority: Medium
• Security: Public
• Views: 252

# AutoComplete A Word

I need a function that checks a global collection of words for the best match for auto completing a word that is passed to it  to teh largest number of matchiong common characters. E.g If the collection contains
sheep
ship
dog
elephant
elephants

Input        output
-----       ------
sh      -->sh(since next chr is not knwon &can be i or e)
shi      -->   ship
d or do  -->   dog
dog      -->   dog
e, el,ele ,elep etc --> elephant
elephant --> elephant
x-->x ( no match found)

Any ideas on how I should match? I was thinking :

Function autoComplete(word As text)
Dim i
For i = 1 To allWords.Count
' The collection is created fresh for each call to this function
' Remove all words that dont match
If (Left(allWords(i), Len(word)) <> word) Then allWords.Remove i
Next i

'only 1 match
If allWords.Count =1 Then
autoComplete=  allWords(1)
Exit Function
End If

' Now the collection contains only matches (s->sheep,ship)
' How can I get the the common part (sh) ?

End Function
0
sambha03
1 Solution

Commented:
Function autoComplete(word As text)
Dim i, j
For j = 0 To Len(word)
For i = 1 To allWords.Count
' The collection is created fresh for each call to this function
' Remove all words that dont match
If (Left(allWords(i), j) <> Left(word, j)) Then allWords.Remove i
Next i
Next j

'only 1 match
If allWords.Count =1 Then
autoComplete=  allWords(1)
Exit Function
End If
End Function

This should do it for you..
Basically I am trying to check for one more character after I reduce the collection size by the no of words that match for every iteration of the j loop (which depends on the length of the word) . Ofcourse you might want to list the entire word match and you can't use the function per se, but can modify it to stop iterating the entire length by passing the iteration count to the function and usign that as the j loop's max value instead of length.

eg:

Function autoComplete(word As text)
Dim i
For i = 1 To allWords.Count
' The collection is created fresh for each call to this function
' Remove all words that dont match
If (Left(allWords(i), len(word)) <> word) Then allWords.Remove i
Next i
End Function

this should work better if you call the function with the word everytime the user types a character in the text box..etc

don't return anything.. just do a count on the colletion and display all the values there... this should return to you the max string matched.
0

Commented:

This source code shows you how to provide parameters and an autocomplete feature to your program, just like in Visual Basic! When the user is typing, a listbox appears listing the possible options. They can press the space bar to have the word completed for them, carry on typing, or select another item on the list.
http://www.developerfusion.com/show/360/

0

Author Commented:
That shld do the trick with some modifications.
0

## Featured Post

Tackle projects and never again get stuck behind a technical roadblock.