快速查找字典文本文件中是否存在单词
我有一个很大的文本文件(~10mb),其中或多或少包含特定语言的每个词典,并且每个单词都是换行分隔的。
我想做一次非常快速的查找,看看文件中是否存在某个单词 - 在不循环每一行的情况下执行此操作的最快方法是什么?
它已排序,我可以进行所有我想要的预处理。
我考虑进行某种二进制搜索,但我不知道如何做到这一点,因为我的所有行都不是固定数量的字节(因此我不知道将流跳转到哪里)。令人惊讶的是,我找不到一个工具来为我完成固定宽度的事情。
有什么建议吗? 谢谢!
I have a large text file (~10mb) that has more or less every dictionary in a specific language, and each word is new line deliminated.
I want to do a really fast lookup to see if a word exists in a file -
What is the fastest way to do this without looping through each line?
It is sorted, and I can do all the pre-processing I want.
I considered doing some sort of Binary search, but I didnt know how I could do this, since all my lines are not a fixed number of bytes (and thus I wouldn't know where to jump the stream to). And surprisingly, I couldnt find a tool to do the fixed-width thing for me.
Any suggestions?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我建议从字典中构建一个 Trie 。这使您可以非常快速地查找单词是否在其中。
I'd suggest building a Trie from the dictionary. That gives you very quick lookups to see whether a word is in there.
如果您不介意使用更多存储空间,则 trie 是一个不错的选择:http://en.wikipedia。 org/wiki/Trie
A trie is a good bet if you don't mind using some more storage: http://en.wikipedia.org/wiki/Trie