Skip to content

Instantly share code, notes, and snippets.

@DQNEO
Last active January 27, 2023 02:54
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DQNEO/18fceb444097c447001d28eddf576226 to your computer and use it in GitHub Desktop.
Save DQNEO/18fceb444097c447001d28eddf576226 to your computer and use it in GitHub Desktop.
Python script to lookup word definitions from MacOS's builtin dictionary app
#!/usr/bin/env python3
#
# LICSENCE: BSD
#
# Thanks to: https://www.lifewithpython.com/2016/07/python-use-mac-dictionary-app.html
#
import sys
from DictionaryServices import DCSGetTermRangeInString, DCSCopyTextDefinition
def lookup(word):
try:
word_range = DCSGetTermRangeInString(None, word, 0)
defn = DCSCopyTextDefinition(None, word, word_range)
return defn
except IndexError:
return 'NOT FOUND'
# Read each word from standard input, look up the definition, and print it
for word in sys.stdin:
w = word.strip()
if not w:
break
desc = lookup(w)
print([w,desc])
@DQNEO
Copy link
Author

DQNEO commented Jan 25, 2023

Usage

Prepare a words list as a text file in advance.

$ cat words.txt
oppress
suppress
repress

Pass the contents to my script.

$ cat words.txt | ./dict.py
['oppress', 'oppress op·press | əˈprɛs | verb [with object] keep (someone) in subservience and hardship, especially by the unjust exercise of authority: a system that oppressed working people. • cause (someone) to feel distressed, anxious, or uncomfortable: he was oppressed by some secret worry. ORIGIN late Middle English: from Old French oppresser, from medieval Latin oppressare, from Latin oppress- ‘pressed against’, from the verb opprimere.']
['suppress', 'suppress sup·press | səˈprɛs | verb [with object] forcibly put an end to: the uprising was savagely suppressed. • prevent the development, action, or expression of (a feeling, impulse, idea, etc.); restrain: she could not suppress a rising panic. • prevent the dissemination of (information): the report had been suppressed. • prevent or inhibit (a process or reaction): use of the drug suppressed the immune response. • partly or wholly eliminate (electrical interference). • Psychoanalysis consciously inhibit (an unpleasant idea or memory) to avoid considering it. DERIVATIVES suppressible | səˈprɛsəbəl | adjective suppressive | səˈprɛsɪv | adjective ORIGIN late Middle English: from Latin suppress- ‘pressed down’, from the verb supprimere, from sub- ‘down’ + premere ‘to press’.']
['repress', "repress re·press | rəˈprɛs | verb [with object] subdue (someone or something) by force: the uprisings were repressed. • restrain, prevent, or inhibit (the expression or development of something): Isabel couldn't repress a sharp cry of fear. • suppress (a thought, feeling, or desire) in oneself so that it becomes or remains unconscious: the thought that he had killed his brother was so terrible that he repressed it. • Biology prevent the transcription of (a gene). DERIVATIVES represser noun repressible | rəˈprɛsəb(ə)l | adjective ORIGIN Middle English (in the sense ‘keep back something objectionable’): from Latin repress- ‘pressed back, checked’, from the verb reprimere, from re- ‘back’ + premere ‘to press’."]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment