简体   繁体   English

如何实现简单的自动完成功能?

[英]How to implement a simple auto-complete functionality?

I'd like to implement a simple class (in Java) that would allow me to register and deregister strings, and on the basis of the current set of strings auto-complete a given string. 我想实现一个简单的类(在Java中),它允许我注册和注销字符串,并在当前字符串集的基础上自动完成给定的字符串。 So, the interface would be: 所以,界面将是:

  • void add(String) void add(String)
  • void remove(String) void remove(String)
  • String complete(String) 字符串完整(String)

What's the best way to do this in terms of algorithms and data-structures? 在算法和数据结构方面,最好的方法是什么?

you should consider to use a PATRICIA trie for the data structure. 您应该考虑使用PATRICIA trie作为数据结构。 Search for 'patricia trie' on google and you'll find a lot of information... 在谷歌上搜索'patricia trie',你会发现很多信息......

The datastructure you are after is called a Ternary Search Tree. 您所追求的数据结构称为三元搜索树。

There's a great JavaWorld example at www.javaworld.com/javaworld/jw-02-2001/jw-0216-ternary.html 有一个很棒的JavaWorld示例,请访问www.javaworld.com/javaworld/jw-02-2001/jw-0216-ternary.html

For those who stumble upon this question... 对于那些偶然发现这个问题的人......

I just posted a server-side autocomplete implementation on Google Code. 我刚在Google Code上发布了服务器端自动完成实现 The project includes a java library that can be integrated into existing applications and a standalone HTTP AJAX autocomplete server. 该项目包括一个可以集成到现有应用程序中的Java库和一个独立的HTTP AJAX自动完成服务器。

My hope is that enables people to incorporate efficient autocomplete into their applications. 我希望能够让人们将高效的自动完成功能整合到他们的应用程序中。 Kick the tires! 踢轮胎!

I have created a JQuery plugin called Simple AutoComplete, that allows you to add many autocomplete as you want on the same page, and to add filters with extra param, and execute a callback function to bring other params, like the id of the item. 我创建了一个名为Simple AutoComplete的JQuery插件,它允许您在同一页面上添加许多自动完成,并添加带有额外参数的过滤器,并执行回调函数以带来其他参数,例如项目的ID。

See it at http://www.idealmind.com.br/projetos/simple-autocomplete-jquery-plugin/ 请访问http://www.idealmind.com.br/projetos/simple-autocomplete-jquery-plugin/

It would have to be some kind of a List that you can maintain in sorted order. 它必须是某种类型的List,您可以按排序顺序维护它。 You would also have to write your own search algorithm that would give you the index of the first element in the list that matches your search pattern. 您还必须编写自己的搜索算法,该算法将为您提供列表中与搜索模式匹配的第一个元素的索引。 Then iterate from that index until the first element that doesn't match and you have your list of possible completions. 然后从该索引迭代,直到第一个不匹配的元素,并且您有可能的完成列表。

I'd look at TreeList from commons-collections. 我将从commons-collections中查看TreeList It has fast insert and remove times from the middle of the List which you'll want in order to maintain sorted order. 它具有从列表中间快速插入和删除的时间,以便维护排序顺序。 It would probably be fairly easy to write your search function off of the tree that backs that list. 从支持该列表的树中编写搜索功能可能相当容易。

常用表达。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM