⬆️ ⬇️

Practical use of Dictionary Services in OS X 10.5

I want to talk about the service of reference books and dictionaries that comes with OS X. You can verify the existence of this service through the “Loop up in dictionary” option in almost any pop-up menu. It is enough to highlight the word of interest, bring up the menu and select this item to quickly and conveniently obtain such information as the meaning of the word, its part of speech, the words 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 setup program of this service. I, for example, set up a Russian-language wiki for myself.



For an advanced user, this service provides a set of interfaces, so by applying the knowledge of Python or Ruby to this, the user can do a different set of analytical and statistical things over the text, be it a definition of the author’s vocabulary based on his books and articles, search for archaisms as well as analysis of sentences on the part of the speech "noun + ch + 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 lists the words in the text, sorts them in order of repetition rate and finds a translation for the most frequently encountered. Printing out this sheet of words, reading articles in the subway or in the train becomes less tedious. To filter out simple words, words that you know - just apply to this list the set of words of your vocabulary, your vocabulary, which can be generated without much difficulty. This way you can constantly update your vocabulary, monitor its growth and your progress.

')

Below is a collection of functions provided by DictionaryServices.



Search dictionaries:



* DCSGetTermRangeInString

* DCSCopyTextDefinition



Results display:



* HIDictionaryWindowShow



I will not analyze each method separately and will pay attention only to the most useful, in my opinion - DCSCopyTextDefinition



CFStringRef DCSCopyTextDefinition (

DCSDictionaryRef dictionary,

CFStringRef textString,

CFRange range

);




Options:

dictionary - This parameter is reserved for future use, so specify as NULL (search takes place in all active dictionaries)

textString - The text that contains the word or phrase you want to find in the dictionary

range - Range that indicates the position of a word or phrase in a textString. If the textString contains only one word, then the range will be [0, length (word)]

Return Value - Represents the result of the execution, which is represented as CFStringRef



Below is a small example that demonstrates calling this method in Python.



>>> 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'

>>>




Another simple example showing how you can find the combination of “verb + preposition” in the text, their sorting and display on the screen.



# 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:

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 will also find this service useful, as I found it.

Source: https://habr.com/ru/post/30542/



All Articles