2011-03-22 22 views
1

基于字符串是否可以为该字符串中的所有单词创建自动完成功能?自动完成字符串中的所有单词

例如:字符串str =“波支持机器人和小工具波机器人发展需要Java 1.6 A波可以被看作是其中包含的子波包络线”。

现在,如果用户在进入输入文本框,在下拉框中应该显示:“Wave supports”,“Wave robot”和“wave can”。

总之,它应该显示除了键入的单词之外的下一个单词。

+0

是的,这是可能的。你想自己写这个吗?寻找其他人已经做了什么?只是指导? – 2011-03-22 09:53:31

+0

如果您使用MS SQL Server您可以查看FullText目录搜索选项。 – 2011-03-22 10:09:18

+0

只有一些关于如何开始的指针 – dazzle 2011-03-22 10:35:16

回答

0

你想要的是一个类似于trie的数据结构。一个特里特树是一个树状二叉树,使得在字典中搜索非常快速和简单。最有可能的是一个trie用于存储单词而不是几个字,但我想你仍然可以使用trie来完成你的任务。例如,你可以用你的句子的单词做出第一个句号。然后你用你的句子的2个单词做第二个句子,等等。现在您必须在每个特里查找来自动完成用户的输入。

+0

是的,这是特里! http://en.wikipedia.org/wiki/Trie – bnieland 2011-03-22 11:47:01

0

如果您将整个单词存储在某个数据源[数据库等]中,则这是可能的。检查jQuery自动完成或只是搜索自动完成JavaScript插件。

1

一些指针让你开始。

  1. 拆分使用例如List<string> words = new List<string>(sData.Split(' '));
  2. 迭代的话用普通for (int i = 0; i < words.Count...循环,当回路电流项等于给定的输入,添加words[i] + words[i + 1]到一个列表,最初是空的字符串的单词列表我们称之为matches
  3. 发送matches给浏览器,例如由|字符delimeted:Response.Write(string.Join("|"), matches);
  4. 在主叫JS,解析结果在AJAX的success方法以及对于每个可能的匹配建立一条线,用户可以选择。
+0

或者如果他正在使用jquery ui只是将'array'传递给'source:'选项。 ;)+1 – 2011-03-22 11:45:11

+0

@aSeptik谢谢,这确实是个好主意! :) – 2011-03-22 11:54:14

1

我是用jQuery Autocomplete完成的。

我从我的数据库中返回了多组可能的答案。

根据输入的内容,我在返回的列表中列出了优先级组,并在该列表的该子部分中对排序的alpha进行排序。组为:

  • 精确匹配 - 这与输入的单词
  • 包含开头的字符串 - - 字符串与精确匹配
  • 与开始打破了名单的话,然后返回该列表。

我把它使内进入设置在返回列表中的话返回结果列表中突出显示。我的返回结果用匹配列出整个字符串,在匹配的单词上用粗体显示。

您可以很容易地使用您的“单词加”方法来匹配字符串与单词,再加上单词列表中的每个单词的下一个单词,如您所描述的 - 可能这将应用于“开始”和“包含“我的战略中的团队可能会有所不同。

从性能的角度来看,我只使用约10,000字符串(每个最多255个字符)作为一个可能的结果集,并只从字符串的原始样品返回的一组有限的(比方说50的“最佳匹配”。

0

创建树扩展字母组合

“错误对不起,我从来没有读过他的整个问题......我想它可以适用于言”

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; 

    } 
} 

现在创建基地树

var tt = new wordTree(); 

添加所有你的话

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"); 

搜索任何与 “沃”

tt.getWords("wo"); 

铬控制台输出这样的事情

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 
statring
相关问题