简体   繁体   中英

Creation of the Tree Structure from a huge text file before Runtime (Java/Android)

I'm currently developing an Android word game written in Java which needs some spell-check method to check whether the word created by a player is correct or not. I have a full list of correct words (polish language) saved in the text file (*.txt) but it is huge because it contains nearly 3 milion words and has the size of 35 MB. I've done some research and found out that the best structure to hold this data will be Ternary Search Tree because it's space and time effective.

My question is how to create Ternary Search Tree structure from my huge text file before the Runtime? It cannot be performed during the Runtime because it will take ages and I will run out of memory (I've already tried it just from curiosity in what way it will crash). So I think that the best way to do it would be to create such a structure even before compilation and add to a project as some kind of resource but unfortunately i have no idea how to do it. If that's not possible to achieve before compilation time maybe you know how to create such a structure during compilation?

More details:

  • I really want to use this text file as my dictionary so please don't suggest me to use other dictionary services.
  • The only method which I need to perform on this data structure will be:
    boolean contains(String word) which will tell me whether the word is correct or not.

If all you need is .contains(String word) and the size is just 35 MB I would recommend a HashSet , HashSet<String> . It will act exactly like a dictionary and since Strings are nicely hashable to begin with it will also be blazingly fast to build. The contains(word) is also a builtin method of the set with O(1) performance.

If you don't want to build the dictionary at runtime you can dump it to a database once (first time your app runs) and just read from it in the future.

Edit: I was not aware there was a max memory limit for android apps. You can circumvent this by dumping the entire file to a database (which is just a file) that can very efficiently be read from and written to. Please check the Android Samples . If you have eclipse and the samples are installed from android sdk manager you can start a new project (Android Sample), from that choose Searchable Dictionary sample and look at the source and see how it handles creating a database from a .txt file. The above link contains full instruction on installing the samples. The android database (SQLite) supports databases up to 1TB apparrently so you should be just fine.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM