Practical Application of Dictionary Services on OS X 10.5

    I want to talk about the directory and dictionary service bundled with OS X. You can verify the existence of this service through the “Loop up in dictionary” item in almost any pop-up menu. It is enough to select the word of interest, call up the menu and select this item to quickly and conveniently receive information such as the meaning of the word, its part of speech, the word synonyms and the history of its origin. Data is collected from all available dictionaries (including from wikipedia) and displayed on one page. The number of dictionaries and other parameters are set through the configuration program of this service. For example, I set up a Russian-language wiki for myself.

    For an advanced user, this service provides a set of interfaces, therefore, applying to this knowledge of Python or Ruby, a user can do a different set of analytical and statistical things on the text, whether it’s determining the author’s vocabulary based on his books and articles, searching for archaisms and also analyzing sentences on the part of speech "noun + hl + preposition". For example, you can find out that on average women use verbs more than men, and the latter use nouns.

    Not knowing English, this service can serve as a translator. For example, before reading a new article or book, you can write a script that makes a list of words in the text, sorts them in order of repetition frequency, and finds a translation for the most common ones. Having printed this sheet of words, reading articles in the subway or in the train becomes less tiring. To filter out simple words, the words that you know - it’s enough to apply to this list a set of words of your vocabulary, your dictionary, which can be generated without any special effort. Thus, you can constantly update your vocabulary, monitor its growth and your progress.

    Below is a set of features provided by DictionaryServices.

    Dictionaries Search:

    * DCSGetTermRangeInString
    *DCSCopyTextDefinition

    Displaying the results:

    * HIDictionaryWindowShow

    I will not parse each method individually and pay attention only to the most, in my opinion, useful - DCSCopyTextDefinition Parameters: dictionary - This parameter is reserved for future use, so indicate as NULL (search occurs in all active dictionaries) textString - The text that contains the word or phrase you want to find in the dictionary range - The range that indicates the position of the word or phrase in textString. If textString contains only one word, then range will be [0, length (word)] Return Value

    CFStringRef DCSCopyTextDefinition (
    DCSDictionaryRef dictionary,
    CFStringRef textString,
    CFRange range
    );



      
      
      
       - This is the result of the execution, which is presented as CFStringRef

    Below is a small example demonstrating the call of this method in Python. Another simple example showing how you can find the combination of "verb + preposition" in the text, their sorting and display. # Copyright © 2008 __MyCompanyName__. All rights reserved. import DictionaryServices import re from operator import itemgetter file = open ('terry.txt', 'r') words = {} buf = [None, None] counter = -1 verbs_prepos = {} for line in file:   for word in re .split ('\ W +', line.lower ()):    counter + = 1    if word not in words:

    >>> import DictionaryServices
    >>> word = "sex"
    >>> meaning = DictionaryServices.DCSCopyTextDefinition(None, word, (0,len(word) ))
    >>> meaning
    u'noun \n1 (chiefly with reference to people) sexual activity, including specifically sexual intercourse \n2 either of the two main categories (male and female) into which humans and most other living things are divided on the basis of their reproductive functions \nverb \n1 determine the sex of \n2 ( sex someone up) arouse or attempt to arouse someone sexually. \n'
    >>>





















        words [word] = [1, DictionaryServices.DCSCopyTextDefinition (None, word, (0, len (word)))]
       else:
        words [word] [0] + = 1

       meaning = words [word] [1]

       if meaning is None or len (word) <2 or word in ["to", "as", "about", "so", "not", "for", "like"]:
        buf [counter% 2] = None
        continue

       buf [counter% 2] = [word, meaning]

       prev_word = buf [(counter-1)% 2]

       if prev_word is not None and re.search ("(^ adverb) | (^ preposition)", meaning [0: 15]) and re.search ("(^ past) | (^ verb)", prev_word [1] [0:15]):
        str = prev_word [0] + "" + word

        if str not in verbs_prepos:
         verbs_prepos [ str] = 1
        else:
         verbs_prepos [str] + = 1

    items = verbs_prepos.items ()
    items.sort (lambda x, y: cmp (y [1], x [1]))

    for word in items:
      print word [0], "-", word [1]

    Output:

    looked up - 50
    think of - 40
    let out - 39
    born with - 39
    thought of - 39
    look at - 37
    get out - 34
    came up - 20
    make out - 20
    asked in - 20
    rose up - 19

    I hope that someone like that But it will find this service useful, as I found it.

    Also popular now: