TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

Autocomplete using Tries

69 点作者 mdibaiee将近 10 年前

7 条评论

nostrademons将近 10 年前
So tries are a really cool data structure and they&#x27;re the natural fit for an autocomplete, but a while back I had to implement an autocomplete widget (over stock tickers) and it turns out that for many relatively small, dense data-sets, binary search over a sorted array with a linear scan to find suffixes is a much (10x+) faster approach.<p>The problem is that a trie is a very cache-loose data structure. Every node is connected to every other by pointers, and they may be spread all over main memory. Finding the first node usually involves chasing several pointers; enumerating each successive one involves chasing several more. Meanwhile, if you just had a sorted array, all subsequent results would be on the same page at least, and many would be on the same cache line.<p>YMMV, based on your data&#x27;s size and shape. Sorted arrays usually work best when the full data set is small and dense (eg. stock tickers), while tries may work better for sparse data (eg. sentences typed in by users). Always benchmark before committing to an implementation.
评论 #9951651 未加载
评论 #9951979 未加载
评论 #9952432 未加载
评论 #9951659 未加载
ot将近 10 年前
When you have more than a handful of strings, you will want to return only a small number (k) of completions, preferably sorted by some pre-assigned score. Depending on how you order the trie, you can only get the first k completions by lexicographic ordering, which could be very far from the &quot;best&quot; completions.<p>A common approach to solve this is to store in each internal node the maximum score among all its descendants, and sort the children of each node by maximum score. This way by doing a best-first search with a heap you can efficiently retrieve the top-k completions by score.<p>If you then have millions of strings, it becomes tricky to do this in a space-efficient and cache-efficient way. I wrote a paper a few years ago with different space-time tradeoffs: <a href="http:&#x2F;&#x2F;www.di.unipi.it&#x2F;~ottavian&#x2F;files&#x2F;topk_completion_www13.pdf" rel="nofollow">http:&#x2F;&#x2F;www.di.unipi.it&#x2F;~ottavian&#x2F;files&#x2F;topk_completion_www13...</a>
gokhan将近 10 年前
You may also want to check this spelling correction algo. I&#x27;m using it along with trie. Original is in C# but ports are available.<p><a href="http:&#x2F;&#x2F;blog.faroo.com&#x2F;2012&#x2F;06&#x2F;07&#x2F;improved-edit-distance-based-spelling-correction&#x2F;" rel="nofollow">http:&#x2F;&#x2F;blog.faroo.com&#x2F;2012&#x2F;06&#x2F;07&#x2F;improved-edit-distance-base...</a>
AceJohnny2将近 10 年前
Speaking of autocomplete, how would you design a data structure and declaration syntax to feed to the autocomplete&#x2F;parsing system of a constrained CLI?<p>By &quot;constrained CLI&quot;, I mean the kind that controls a router or other piece of equipment which has a well-defined (um, somewhat) syntax, as opposed to the somewhat more free-form aspect of the shell.<p>I ask, because one of my jobs had me occasionally working in the CLI declarations for a type of router. The cli commands could be in the form &quot;interface foo enable&quot;, where &quot;interface&quot; could be shortened its shortest non-ambiguous form (&quot;int&quot; IIRC), and &quot;foo&quot; was a string corresponding to one of the interfaces on the system. Pressing TAB after &quot;int&quot; would automatically suggest the list of interfaces that exist on the system. This command had an opposite in the form &quot;no interface foo enable&quot; (yeah, prefixing &quot;no&quot; instead of postfixing &quot;disable&quot;, though to be fair that may have been possible as well). A declaration of such a syntax was done with something along the line of:<p><pre><code> node(id=interface, type=keyword, keyword=interface, no_prefix=yes, next=int_name); node(id=int_name, type=string, next=enable); node(id=enable, type=keyword, keyword=enable); </code></pre> (with several extra bells and whistles to account for the int_name string referencing the existing interfaces on the system and to indicate that the command was only complete with &quot;enable&quot; at the end).<p>There were hundreds of declaration files containing lines like that, it was parsed by a humongous Perl script that output a 7MB source file containing the data structures fed to libinput. It was easily the slowest part of our compilation process, and Emacs would choke on that 7MB sourcefile.<p>It was terrible, but I never found examples of how to do it better. Anyone have any experience?
ocharles将近 10 年前
It seems you&#x27;d get more mileage out of a trie if you also compared it with a sort of zipper approach. The idea being that if I type &quot;a&quot;, I should just focus the trie around &quot;A&quot;. If I next type &quot;l&quot;, then I focus the &quot;l&quot; around my new root, so I find all strings starting &quot;Al&quot;. It doesn&#x27;t change the complexity class of completion, but maybe the small optimisation is worth it.
msoad将近 10 年前
I was watching this lecture last night about tries and suffix arrays. Pretty cool stuff!<p><a href="http:&#x2F;&#x2F;youtu.be&#x2F;NinWEPPrkDQ" rel="nofollow">http:&#x2F;&#x2F;youtu.be&#x2F;NinWEPPrkDQ</a>
mrsmee89将近 10 年前
This looks really cool.<p>Do you have any speed comparisons vs traditional autocomplete methods?
评论 #9951742 未加载