Access iOS dictionary programmatically
Asked Answered
G

8

35

I'm looking for a way to access iOS dictionary programmatically. What I need is to retrieve the list of the words of a particular language from inside a custom app.
Is it possible? Are there any API that provide this (or similar) functionality?
I've checked Apple documentation but didn't find anything useful...May someone point me to the right direction?

Guddle answered 14/9, 2011 at 16:45 Comment(3)
Be careful with your terminology -- dictionary also frequently refers to the Foundation data type NSDictionary.Jetton
I know, I think I wrote the question as clear as possible...Guddle
Here is a related question: #23019609Analyzer
Q
13

I don't think there is a public API to access the embedded Dictionary. The closest is using UITextChecker but that may not satisfy your needs

As an alternative, you could find and embed a Word Dictionary in the Language you want? It may require some extra work

Queensland answered 14/9, 2011 at 16:57 Comment(2)
Yes I can but, as you said, it requires extra work. It would be helpful to access directly iOS's one. However thanks!Guddle
The link now redirects to the iOS developer home page. This is the new link: developer.apple.com/library/ios/documentation/UIKit/Reference/…Wynne
S
41

There is an API for bringing up the built-in dictionary UI (not the original question but raised in some of the responses), including checking for whether a given word is defined:

if ([UIReferenceLibraryViewController dictionaryHasDefinitionForTerm:@"word"]) {
    UIReferenceLibraryViewController* ref = 
        [[UIReferenceLibraryViewController alloc] initWithTerm:@"word"];
    [currentViewController presentViewController:ref animated:YES completion:nil];
}
Sansbury answered 23/7, 2013 at 20:47 Comment(0)
Q
13

I don't think there is a public API to access the embedded Dictionary. The closest is using UITextChecker but that may not satisfy your needs

As an alternative, you could find and embed a Word Dictionary in the Language you want? It may require some extra work

Queensland answered 14/9, 2011 at 16:57 Comment(2)
Yes I can but, as you said, it requires extra work. It would be helpful to access directly iOS's one. However thanks!Guddle
The link now redirects to the iOS developer home page. This is the new link: developer.apple.com/library/ios/documentation/UIKit/Reference/…Wynne
F
13

There is no API, but if all you need is a list of words you can read it from /usr/share/dict/words:

    NSString *allTheWords = [NSString stringWithContentsOfFile: @"/usr/share/dict/words"
                                                      encoding: NSUTF8StringEncoding
                                                         error: nil];
    for (NSString *line in [allTheWords componentsSeparatedByString:@"\n"]) {
        NSLog(@"%@",line);
    }

If you need an arbitrary list of words you can get it from several online resources, here is one. The README at /usr/share/dict/README says iOS word list uses ftp://ftp.ox.ac.uk/pub/wordlists

For instant search you'll need to load the words on a structure suitable for prefix search, like a trie. Here is a sample project where I did just that.

If you need a dictionary, get one online, then include a pointer to the word entry in the trie node. If loading takes too much time you can experiment loading on the background, or compressing, or serializing the trie.

Flatways answered 25/3, 2013 at 18:8 Comment(4)
Sadly when I tried this, it worked in the simulator but not on the device (iOS 6). Perhaps that file doesn't exist on the device?Upbraid
It's not available on iOS 5 or 6 :( I would zip the dict in OS X /usr/share/dict/web2 and add it to the app bundle. It's 2.5 MB uncompressed.Flatways
Thanks... I should have said: that's exactly what I did :)Upbraid
hi jano, you mentioned serialising the trie table, any chance of pointing us in the right direction to do so?Corr
B
3

There is no API to do what you are asking.

Brokerage answered 14/9, 2011 at 16:57 Comment(0)
G
3

I ran into a similar problem a long time ago and did what other people suggested: embedded an English dictionary inside my app. Since then I've turned my solution into a static library which is available at www.lexicontext.com (note that it costs a few bucks). Considering the number of developers who downloaded it, this seems to be a common problem...

Giovannagiovanni answered 7/3, 2012 at 10:8 Comment(6)
Beware that the lexicon text solution uses the WordNet open source dictionary that doesn't include determiners, prepositions, pronouns, conjunctions, and particles. So, it doesn't include words like "these" or "she".Ventriloquize
@Giovannagiovanni what about it - did you address the problem EWPArris mentions? Thanks. More than happy to pay the few dollars, cheers, but let us know on the problem there.Pansy
@JoeBlow Yes, this problem was solved a long time ago... :) Lexicontext extends WordNet with an auxiliary list of word definitions that includes a lot of determiners, prepositions, pronouns, conjunctions. This extension is provided as .plist file that the developer can further extend with more definitions if a definition that important to him is missing from the list. You can check it out in if you look into the resource bundle that is also included in the trial version.Giovannagiovanni
Hi Ori, great stuff, we'll buy a copy and give it a go. I'm glad I was able to draw to your attention the important (but old) comment here by EWP.Pansy
Well, yes, but. The plist only has 83 words in it and "these" is not included. It would be pretty hard to discover by trial and error which words are missing. Don't get me wrong Lexicontext is really great for most words but if you're building a Scrabble-like app you need to invest a more time and effort guaranteeing that all words are being checked. Also, the wordnet dictionary has some weird definitions in some cases. Anyway... just be prepared for the unexpected.Ventriloquize
I think this might be a dead project now (I don't see any way to "purchase" this), the original Github repo is gone but I found a mirror with some searching. For me personally I don't think this library would be a good fit for me (I'm looking for a way to supply my own dictionary, not use somebody else's), I'm just posting this link for other readers. Though if the creator is no longer interested in this, I would appreciate it if they released the source behind the .a file.Aventine
D
3

If you can use the private API you can use the following code to define a word:

_UIDictionaryManager *manager = [%c(_UIDictionaryManager) assetManager];
id definitions = [manager _definitionValuesForTerm:[self queryString]];
for(_UIDefinitionValue *item in definitions) {
        myModel = self;
        NSMutableAttributedString *def = [item definition];
        NSString *str = [def string];
        HBLogDebug(@"%@ = %@", [item localizedDictionaryName], str);
}
Delaine answered 6/6, 2016 at 17:27 Comment(0)
M
3

There is a PUBLIC API for this as @mrzzmr already stated - here the code in SWIFT:

if UIReferenceLibraryViewController.dictionaryHasDefinition(forTerm: "word") {
    var childVC = UIReferenceLibraryViewController(term: "word")
    someParentVC.present(childVC, animated: true, completion: nil)
}
Myron answered 30/11, 2020 at 21:24 Comment(0)
O
0

Even there is no API, it can be used automatically if user long-press the selected word.

Ortiz answered 11/11, 2011 at 2:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.