0

I have extracted OSX English language dictionary and want to use it in my Swift iPhone app. It has about 236,000 words which I have added to a swift string array.

When I try to run the build, it takes a long time to compile and then throws Segmentation Fault 11

Is this because the array is too big?

Am I going the correct path trying to add english dictionary in my project?

Kashif
  • 4,642
  • 7
  • 44
  • 97
  • Can you post your code here? Especially the for loop part – ipraba Jul 09 '15 at 14:12
  • Is the English dictionary part of your source code or do you add it as a resource and read it from the file system? – freytag Jul 09 '15 at 14:14
  • An average word in English has 7 characters. 236K words take 1.5MB. That's not too big, even on iOS devices. – Code Different Jul 09 '15 at 14:14
  • I havn't even implemented a for loop yet. I am just declaring the array with 236,000 word strings in it. It is in the swift file not a separate resource file. – Kashif Jul 09 '15 at 14:17
  • The swift file is about 2.5MB – Kashif Jul 09 '15 at 14:19
  • I'd strongly suggest using a different way. For example a C file with an array of char*. – gnasher729 Jul 09 '15 at 14:20
  • I only know swift. What would be the equivalent of 'c file with char' in swift? – Kashif Jul 09 '15 at 14:21
  • You don't have to use C. Please find the required information in this posting: http://stackoverflow.com/questions/24097826/read-and-write-data-from-text-file – freytag Jul 09 '15 at 14:26
  • Save your dictionary as a delimited text file and then load it using `String.stringWithContentsOfFile(...)` and then split the string up using `split(myString) { $0 == }` – GoatInTheMachine Jul 09 '15 at 14:26
  • @GoatlinTheMachine: But then would it not result in the same huge array of string with 236,000 entries in the memory, which is causing the issue right now? – Kashif Jul 09 '15 at 14:38
  • You might be better using `CoreData` or some sort of DB framework. – Matteo Piombo Jul 09 '15 at 14:57

2 Answers2

1

You should probably not store this as a single string. There are more efficient data structures that you can use, such as a trie. You should also consider not loading the entire content into memory at one point but be able to navigate it from the filesystem.

AlBlue
  • 23,254
  • 14
  • 71
  • 91
  • As I answered myself, I was able to solve the issue but looping through array still takes some 0.2 sec or so which is slow for the purpose of my app. I like the trie idea but this concept is new to me and I do not know how to implement it in SWIFT. Will I need an additional framework or extension to accomplish it? – Kashif Jul 10 '15 at 12:04
1

I was able to solve this problem by adding the actual dictionary text file into my xcode project. then utilize below code to fill words from the file to an array. it was pretty fast.

let path = NSBundle.mainBundle().pathForResource("dict2", ofType: "txt")
let dico = String(contentsOfFile: path!, encoding: NSUTF8StringEncoding, error: nil)
let dict = dico!.componentsSeparatedByString("\n")

Hope it helps someone.

Kashif
  • 4,642
  • 7
  • 44
  • 97