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

时间:2022-09-02 15:13:17

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. So, the interface would be:

我想实现一个简单的类(在Java中),它允许我注册和取消注册字符串,并在当前字符串集的基础上自动完成给定的字符串。所以,界面将是:

  • void add(String)
  • void remove(String)
  • String complete(String)

What's the best way to do this in terms of algorithms and data-structures?

在算法和数据结构方面,最好的方法是什么?

6 个解决方案

#1


4  

you should consider to use a PATRICIA trie for the data structure. Search for 'patricia trie' on google and you'll find a lot of information...

你应该考虑使用PATRICIA trie来实现数据结构。在谷歌上搜索'patricia trie',你会发现很多信息......

#2


3  

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

#3


0  

It would have to be some kind of a List that you can maintain in sorted order. 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.

它必须是某种类型的List,您可以按排序顺序维护它。您还必须编写自己的搜索算法,该算法将为您提供列表中与搜索模式匹配的第一个元素的索引。然后从该索引迭代直到第一个不匹配的元素,并且您有可能的完成列表。

I'd look at TreeList from commons-collections. 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.

我将从commons-collections中查看TreeList。它具有从列表中间快速插入和删除的时间,以便维护排序顺序。从支持该列表的树中编写搜索功能可能相当容易。

#4


0  

For those who stumble upon this question...

对于那些偶然发现这个问题的人......

I just posted a server-side autocomplete implementation on Google Code. The project includes a java library that can be integrated into existing applications and a standalone HTTP AJAX autocomplete server.

我刚刚在Google Code上发布了服务器端自动完成实现。该项目包括一个可以集成到现有应用程序的Java库和一个独立的HTTP AJAX自动完成服务器。

My hope is that enables people to incorporate efficient autocomplete into their applications. Kick the tires!

我希望能够让人们将高效的自动完成功能整合到他们的应用程序中。踢轮胎!

#5


0  

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/

#6


-2  

Regular expressions.

#1


4  

you should consider to use a PATRICIA trie for the data structure. Search for 'patricia trie' on google and you'll find a lot of information...

你应该考虑使用PATRICIA trie来实现数据结构。在谷歌上搜索'patricia trie',你会发现很多信息......

#2


3  

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

#3


0  

It would have to be some kind of a List that you can maintain in sorted order. 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.

它必须是某种类型的List,您可以按排序顺序维护它。您还必须编写自己的搜索算法,该算法将为您提供列表中与搜索模式匹配的第一个元素的索引。然后从该索引迭代直到第一个不匹配的元素,并且您有可能的完成列表。

I'd look at TreeList from commons-collections. 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.

我将从commons-collections中查看TreeList。它具有从列表中间快速插入和删除的时间,以便维护排序顺序。从支持该列表的树中编写搜索功能可能相当容易。

#4


0  

For those who stumble upon this question...

对于那些偶然发现这个问题的人......

I just posted a server-side autocomplete implementation on Google Code. The project includes a java library that can be integrated into existing applications and a standalone HTTP AJAX autocomplete server.

我刚刚在Google Code上发布了服务器端自动完成实现。该项目包括一个可以集成到现有应用程序的Java库和一个独立的HTTP AJAX自动完成服务器。

My hope is that enables people to incorporate efficient autocomplete into their applications. Kick the tires!

我希望能够让人们将高效的自动完成功能整合到他们的应用程序中。踢轮胎!

#5


0  

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/

#6


-2  

Regular expressions.