用于搜索短语中的单词的数据结构

时间:2013-05-13 14:54:34

标签: java android data-structures collections full-text-search

(我正在用android sdk开发,但也许它并不重要) 我从一个文件中读取了几个短语并将它们保存在一个集合中。

例如: “你好,世界”, “你打招呼”, “大世界”

我想制作类似谷歌主页的内容。我有一个编辑文本,用户可以在其中编写一个或多个单词。当他输入第一个单词时,(例如:“你好”),我想在一些文本字段中显示可能的候选短语(例如:“hello world”,“mark say hello”)。 现在,用户可以键入另一个单词来优化搜索等等。

已经有了这样的事情吗? 哪个是旧数据的最佳数据结构? 我正在考虑使用word和idPhrase的MultiMap(例如:(“hello”,0),(“world”,0),(“你”,1),...) 我想要获得一个子集(例如,如果用户输入“hello”,我会删除第三个短语)等等,以优化搜索。 (但也许最好将短语从文件保存到sqlLite DB而不是集合,我不知道......)

存在更好的方法吗?

2 个答案:

答案 0 :(得分:0)

反向索引是正确的数据结构。不确定网上某处是否有实现(肯定有),但很容易自己构建它。看看这里:

http://en.wikipedia.org/wiki/Inverted_index

算法应该是这样的:

for(String phrase : phrases)  
{     
    for(String word : phrase.split(" "))  
    {  
        List temp= map.get(word); 
        if (temp == null) {
            temp= new ArrayList<String>();
            map.put(word, temp);
        }
        temp.add(phrase);
    }  
}  

稍后,您只需查询地图以查找用户查询(单词)并打印短语列表。

如果您需要短语搜索,可能需要查看:http://en.wikipedia.org/wiki/Tf - idf否则,找到为短语查询中每个单词返回的列表的并集。

答案 1 :(得分:0)

然后你正在寻找一个TextWatcher。

myEditText.addTextChangedListener(new TextWatcher() {
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

                // TODO Auto-generated method stub
                //Here implemens the logic stuff that at each new character entered

            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                // TODO Auto-generated method stub
            }

            @Override
            public void afterTextChanged(Editable s) {

                // TODO Auto-generated method stub
            }
        });

这是little example with a listView

另一种方法也可以是使用custom suggestion

相关问题