Autocomplete for all words in a string
Based upon a string is it possible to create an autocomplete for all the words in that string?
For example: String str="Wave supports Robots and Gadgets. 开发者_StackOverflow中文版Wave robot development requires Java 1.6. A wave can be seen as an envelop which contains wavelets"
Now if the user enters Wave in the input text box, in the dropdown it should show: "Wave supports", "Wave robot" and "wave can".
In short it should show the next word besides the typed word.
Some pointers to get you started.
- Split the string to list of words using for example
List<string> words = new List<string>(sData.Split(' '));
- Iterate the words using ordinary
for (int i = 0; i < words.Count...
loop and when the current item in the loop is equal to given input, addwords[i] + words[i + 1]
to a List that is initially empty let's call itmatches
. - Send
matches
to the browser, for example delimeted by|
character:Response.Write(string.Join("|"), matches);
- In the calling JS, parse the result in the
success
method of the AJAX and for each possible match build one line user can select.
I did this with the jQuery Autocomplete.
I returned (from my database) a multiple set of possible answers.
Based on what was typed, I had groups of priority in my returned list, sorted alpha within that subsection of list. Groups were:
- Exact match - strings with an exact match
- Begins with - strings that begin with the entered words
- Contains - broke down the list for words, then return that list.
I made it so that the words in the return list within the entered set were highlighted within the return results list. My return results listed the entire string with a match, with bold on the matched words.
You could easily use a your "word plus" approach to match strings with a word, plus the next word for each of a list of words as you describe - probably this would be applied between the "Begins" and "Contains" groups in my strategy but yours might differ.
From a performance standpoint, I was only using about 10,000 strings (up to 255 characters each) as a possible result set and only returned a limited set (say 50 "best matches" from the original sample of strings.
What you want is a trie-like datastructure. A trie is tree-like-binary-tree and makes searching in a dictionary very fast and simple. Most likely a trie is used to store words and not a couple of words but I guess you can still use a trie to finish your task. For example you can make a first trie with just words of your sentence. Then you make a 2nd trie with 2 words of your sentence and so on. Now you have to lookup in each trie to autocomplete the user's input.
It is possible if you have the whole word stored in a data source[database etc] somewhere. Do check out jQuery autocomplete or just search for autocomplete javascript plugin.
Create a tree with expanding letter combinations
"Err sorry i never read his whole question ... I suppose it could be adapted for words"
function wordTree(){
var WT = this;
WT.tree = {};
WT.word = function(word){
var current = WT.tree;
for(var i = 0;i < word.length; i ++){
var check = current[word[i ]];
if(!check){
current = current[word[i]] = {value: word.substr(0,i +1), words: [word]};
}else{
current = check;
current.words_ahead.push(word);
}
}
}
WT.getWords = function(base){
var current = WT.tree;
for(var i = 0;i < base.length; i ++){
var check = current[base[i]];
if(check){
current = check;
} else {
break;
}
}
return current.words;
}
}
Now create base tree
var tt = new wordTree();
Add all your words
tt.word("word");
tt.word("about");
tt.word("after");
tt.word("again");
tt.word("air");
tt.word("all");
tt.word("along");
tt.word("also");
tt.word("an");
tt.word("and");
tt.word("another");
tt.word("any");
tt.word("are");
tt.word("around");
tt.word("as");
tt.word("at");
tt.word("away");
tt.word("back");
tt.word("be");
tt.word("because");
tt.word("been");
tt.word("before");
tt.word("below");
tt.word("between");
tt.word("both");
tt.word("but");
tt.word("by");
tt.word("came");
tt.word("can");
tt.word("come");
tt.word("could");
tt.word("day");
tt.word("did");
tt.word("different");
tt.word("do");
tt.word("does");
tt.word("don");
tt.word("t");
tt.word("down");
tt.word("each");
tt.word("end");
tt.word("even");
tt.word("every");
tt.word("few");
tt.word("find");
tt.word("first");
tt.word("for");
tt.word("found");
tt.word("from");
tt.word("get");
tt.word("give");
tt.word("go");
tt.word("good");
tt.word("great");
tt.word("had");
tt.word("has");
tt.word("have");
tt.word("he");
tt.word("help");
tt.word("her");
tt.word("here");
tt.word("him");
tt.word("his");
tt.word("home");
tt.word("house");
tt.word("how");
tt.word("I");
tt.word("if");
tt.word("in");
tt.word("into");
tt.word("is");
tt.word("it");
tt.word("its");
tt.word("just");
tt.word("know");
tt.word("large");
tt.word("last");
tt.word("left");
tt.word("like");
tt.word("line");
tt.word("little");
tt.word("long");
tt.word("look");
tt.word("made");
tt.word("make");
tt.word("man");
tt.word("many");
tt.word("may");
tt.word("me");
tt.word("men");
tt.word("might");
tt.word("more");
tt.word("most");
tt.word("Mr");
tt.word("must");
tt.word("my");
tt.word("name");
tt.word("never");
tt.word("new");
tt.word("next");
tt.word("no");
tt.word("not");
tt.word("now");
tt.word("number");
tt.word("of");
tt.word("off");
tt.word("old");
tt.word("on");
tt.word("one");
tt.word("only");
tt.word("or");
tt.word("other");
tt.word("our");
tt.word("out");
tt.word("over");
tt.word("own");
tt.word("part");
tt.word("people");
tt.word("place");
tt.word("put");
tt.word("read");
tt.word("right");
tt.word("said");
tt.word("same");
tt.word("saw");
tt.word("say");
tt.word("see");
tt.word("she");
tt.word("should");
tt.word("show");
tt.word("small");
tt.word("so");
tt.word("some");
tt.word("something");
tt.word("sound");
tt.word("still");
tt.word("such");
tt.word("take");
tt.word("tell");
tt.word("than");
tt.word("that");
tt.word("the");
tt.word("them");
tt.word("then");
tt.word("there");
tt.word("these");
tt.word("they");
tt.word("thing");
tt.word("think");
tt.word("this");
tt.word("those");
tt.word("thought");
tt.word("three");
tt.word("through");
tt.word("time");
tt.word("to");
tt.word("together");
tt.word("too");
tt.word("two");
tt.word("under");
tt.word("up");
tt.word("us");
tt.word("use");
tt.word("very");
tt.word("want");
tt.word("water");
tt.word("way");
tt.word("we");
tt.word("well");
tt.word("went");
tt.word("were");
tt.word("what");
tt.word("when");
tt.word("where");
tt.word("which");
tt.word("while");
tt.word("who");
tt.word("why");
tt.word("will");
tt.word("with");
tt.word("word");
tt.word("work");
tt.word("world");
tt.word("would");
tt.word("write");
tt.word("year");
tt.word("you");
tt.word("your");
search for anything statring with "wo"
tt.getWords("wo");
chrome console outputs something like this
Object {item_value: "wo", words: Array[5], r: Object, u: Object}
item_value: "wo"
r: Object
u: Object
words_ahead: Array[5]
0: "word"
1: "word"
2: "work"
3: "world"
4: "would"
length: 5
__proto__: Array[0]
__proto__: Object
精彩评论