【发布时间】:2011-04-21 09:52:39
【问题描述】:
我目前正在实施BK-Tree 来进行拼写检查。我正在使用的字典非常大(数百万字),这就是我无法承受任何低效率的原因。但是,我知道我编写的查找函数(可以说是整个程序中最重要的部分)可以做得更好。我希望能找到一些关于同样的帮助。这是我写的查找:
public int get(String query, int maxDistance)
{
calculateLevenshteinDistance cld = new calculateLevenshteinDistance();
int d = cld.calculate(root, query);
int tempDistance=0;
if(d==0)
return 0;
if(maxDistance==Integer.MAX_VALUE)
maxDistance=d;
int i = Math.max(d-maxDistance, 1);
BKTree temp=null;
for(;i<=maxDistance+d;i++)
{
temp=children.get(i);
if(temp!=null)
{
tempDistance=temp.get(query, maxDistance);
}
if(maxDistance<tempDistance)
maxDistance=tempDistance;
}
return maxDistance;
}
我知道我运行循环的次数非常多,而且我们可以修剪搜索空间以加快查找速度。我只是不确定如何最好地做到这一点。
【问题讨论】:
-
@Mitch - 这可能是真的......但是仅仅以被接受为借口回答的人开始变得有点老了。人们不应该回答是有帮助的吗?
-
@efficiencyIsBliss - 我回答问题是因为我需要接受我的答案。祝你好运。
-
@Justin,我知道你来自哪里。但我认为可以提出一个健康的论点,即从公共知识库的角度来看,鼓励公民参与最佳实践是好的。对于遇到 SO 的随机谷歌用户来说,带有已检查答案的问题比没有这样答案的问题更有用。
-
你看到上面的那个盒子了吗?说“未答复”的那个。这就是为什么人们需要接受答案。他们污染了列表,浪费了人们试图帮助解决实际上没有答案的问题的时间。
-
出于好奇,什么语言拼写需要百万字?
标签: java algorithm performance implementation bk-tree