Way to store a large dictionary with low memory footprint + fast lookups (on Android)

前端 未结 7 975
醉话见心
醉话见心 2021-01-30 23:17

I\'m developing an android word game app that needs a large (~250,000 word dictionary) available. I need:

  • reasonably fast look ups e.g. constant time preferable, n
相关标签:
7条回答
  • 2021-01-30 23:45

    The devices that I worked basically worked from a binary compressed file, with a topology that resembled the structure of a binary tree. At the leafs, you would have the Huffmann compressed text. Finding a node would involve having to skip to various locations of the file, and then only load the portion of the data really needed.

    0 讨论(0)
  • 2021-01-30 23:45

    Very cool idea as suggested by "Antti Huima" trying to Store dictionary words using long. and then search using binary search.

    0 讨论(0)
  • 2021-01-30 23:50

    You can achieve your goals with more lowly approaches also... if it's a word game then I suspect you are handling 27 letters alphabet. So suppose an alphabet of not more than 32 letters, i.e. 5 bits per letter. You can cram then 12 letters (12 x 5 = 60 bits) into a single Java long by using 5 bits/letter trivial encoding.

    This means that actually if you don't have longer words than 12 letters / word you can just represent your dictionary as a set of Java longs. If you have 250,000 words a trivial presentation of this set as a single, sorted array of longs should take 250,000 words x 8 bytes / word = 2,000,000 ~ 2MB memory. Lookup is then by binary search, which should be very fast given the small size of the data set (less than 20 comparisons as 2^20 takes you to above one million).

    IF you have longer words than 12 letters, then I would store the >12 letters words in another array where 1 word would be represented by 2 concatenated Java longs in an obvious manner.

    NOTE: the reason why this works and is likely more space-efficient than a trie and at least very simple to implement is that the dictionary is constant... search trees are good if you need to modify the data set, but if the data set is constant, you can often run a way with simple binary search.

    0 讨论(0)
  • 2021-01-30 23:54

    A very efficient way to store a directory is a Directed Acyclic Word Graph (DAWG).

    Here are some links:

    • Directed Acyclic Word Graph or DAWG description with sourcecode
    • Construction of the CDAWG for a Trie
    • Implementation of directed acyclic word graph
    0 讨论(0)
  • 2021-01-30 23:54

    You could also use the Android NDK and do the structure in C or C++.

    0 讨论(0)
  • 2021-01-30 23:59

    I am assuming that you want to check if given word belongs to dictionary.

    Have a look at bloom filter.

    The bloom filter can do "does X belong to predefined set" type of queries with very small storage requirements. If the answer to query is yes, it has small (and adjustable) probability to be wrong, if the answer to query is no, then the answer guaranteed to be correct.

    According the Wikipedia article you could need less than 4 MB space for your dictionary of 250 000 words with 1% error probability.

    The bloom filter will correctly answer "is in dictionary" if the word actually is contained in dictionary. If dictionary does not have the word, the bloom filter may falsely give answer "is in dictionary" with some small probability.

    0 讨论(0)
提交回复
热议问题