kt.kthapar

Q: Using dictionary from command line

Is it possible I could see the definition of a word for eg. 'Apple' in terminal itself without opening the dictionary app?

I work in the terminal most of the times and I am just wondering if I could get all the information in terminal itself.

Any ideas? Thanks!

Message was edited by: kt.kthapar

iMac 27' Quadcore, Mac OS X (10.6.4)

Posted on Dec 11, 2010 6:04 AM

Close

Q: Using dictionary from command line

  • All replies
  • Helpful answers

Previous Page 2 of 3 last Next
  • by Phil Stokes,

    Phil Stokes Phil Stokes Mar 24, 2014 7:29 AM in response to Tony T1
    Level 2 (360 points)
    Mar 24, 2014 7:29 AM in response to Tony T1

    Tony T1 wrote:

    So, if you don't have an internet connection, you wouldn't be reading this

     

    If you only own a desktop workstation I can appreciate that you'd miss the point. However, by far the majority of units that Apple sell are portables. Unlike air, the internet is not everywhere, and that's an important consideration for notebook users (I'll forget that you totally overlooked the response time issue...).

     

    Anyway, a better note is not to use any scripts that you don't understand

     

    Why are scripts a special case from any other software? If people were required to understand how every utility they use on their computer actualy works, then they'd be able to use very little, and software developers wouldn't have much of a market.

     

    There's no reason why every user has to understand how every script on their computer works. What matters is at best that the user understands what is needed to make it work and what its major limitations are. In this case, as I pointed out, that's the internet and response time, respectively.

  • by Tony T1,

    Tony T1 Tony T1 Mar 24, 2014 8:19 AM in response to Phil Stokes
    Level 6 (9,249 points)
    Mac OS X
    Mar 24, 2014 8:19 AM in response to Phil Stokes

    Phil Stokes wrote:

    If you only own a desktop workstation I can appreciate that you'd miss the point.

    There's no reason why every user has to understand how every script on their computer works.

     

    The point was that before using a script posted in any forum, it's best understand what it does,  Capisce?  (that an Internet connection is needed for curl is sort of useless information if your not using a script from a trusted source)

  • by Phil Stokes,

    Phil Stokes Phil Stokes Mar 24, 2014 8:36 AM in response to Tony T1
    Level 2 (360 points)
    Mar 24, 2014 8:36 AM in response to Tony T1

    To be honest, I've no idea what you're getting so irate about and I care even less at this point. But to get back to the topic, at least tangentially, capisce isn't in the Apple installed dictionary. How about your dict.org Curl script?

     

    Screen Shot 2014-03-24 at 22.28.15.png

     

    (An interesting experiment would be to compare how fast both methods take to return a 'not found' result).

  • by Tony T1,

    Tony T1 Tony T1 Mar 24, 2014 8:43 AM in response to Phil Stokes
    Level 6 (9,249 points)
    Mac OS X
    Mar 24, 2014 8:43 AM in response to Phil Stokes

    Try Google (Note that this requires an internet connection to work )

  • by Phil Stokes,

    Phil Stokes Phil Stokes Mar 24, 2014 8:55 AM in response to Tony T1
    Level 2 (360 points)
    Mar 24, 2014 8:55 AM in response to Tony T1

    Great. All you need to do now is tell the OP how to do that in the command line (you might want to review the thread title there)...

     

    Here, I got you started:

     

    http://stackoverflow.com/questions/1617152/using-google-as-a-dictionary-lookup-v ia-bash-how-can-one-grab-the-first-definit

     

    Still no word on comparative response times...maybe you have Google fiber and no data cap.

  • by Tony T1,

    Tony T1 Tony T1 Mar 24, 2014 9:30 AM in response to Phil Stokes
    Level 6 (9,249 points)
    Mac OS X
    Mar 24, 2014 9:30 AM in response to Phil Stokes

    Phil Stokes wrote:

     

    Great. All you need to do now is tell the OP how to do that in the command line

     

    etresoft already gave the best answer to the OP's question in the 3rd post above.

    Capisce?

  • by Phil Stokes,

    Phil Stokes Phil Stokes Mar 24, 2014 9:36 AM in response to Tony T1
    Level 2 (360 points)
    Mar 24, 2014 9:36 AM in response to Tony T1

    No I don't "capisce" cos we're not in an episode of the Sopranos.

     

    However, at last we do agree on something. Indeed, etresoft's was the best answer because its a command line utility that uses local resources and doesn't rely on a possibly non-existent or flakey internet connection.

     

    I've just been toying with your pride since then.

  • by KcidKcus,

    KcidKcus KcidKcus Oct 21, 2015 10:53 PM in response to kt.kthapar
    Level 1 (0 points)
    Oct 21, 2015 10:53 PM in response to kt.kthapar

    Here is the way I like the most.  It uses Princeton University's Wordnet(https://wordnet.princeton.edu/) database:

     

    1) If you don't have it already, get MacPorts(https://www.macports.org/)

    2) afterwards, get the port wn and its dependencies - sudo port install wn

    3) then, just type 'wn <word you want to look up> -over' in the terminal and then press enter

    4) you will then have something like this in the terminal:

       

    Overview of adj ambiguous

     

    The adj ambiguous has 3 senses (first 3 from tagged texts)

                                          

    1. (9) equivocal, ambiguous -- (open to two or more interpretations; or of uncertain nature or significance; or (often) intended to mislead; "an equivocal statement"; "the polling had a complex and equivocal (or ambiguous) message for potential female candidates"; "the officer's equivocal behavior increased the victim's uneasiness"; "popularity is an equivocal crown"; "an equivocal response to an embarrassing question")

    2. (4) ambiguous -- (having more than one possible meaning; "ambiguous words"; "frustrated by ambiguous instructions, the parents were unable to assemble the toy")

    3. (1) ambiguous -- (having no intrinsic or objective meaning; not organized in conventional patterns; "an ambiguous situation with no frame of reference"; "ambiguous inkblots")

  • by Pierre L.,

    Pierre L. Pierre L. Oct 22, 2015 12:48 PM in response to kt.kthapar
    Level 5 (4,484 points)
    Oct 22, 2015 12:48 PM in response to kt.kthapar

    Is it possible I could see the definition of a word for eg. 'Apple' in terminal itself without opening the dictionary app?

     

    My two cents… On my old MacBook Pro mid-2010, running El Capitan, I can use three fingers to tap a word to look it up in the dictionary, which does not open the dictionary.

    Image capture.png

  • by KcidKcus,

    KcidKcus KcidKcus Oct 23, 2015 12:38 PM in response to KcidKcus
    Level 1 (0 points)
    Oct 23, 2015 12:38 PM in response to KcidKcus

    Oh, did I forget to mention that wn can be used as a thesaurus?  Well, it most certainly can!  In the terminal, type 'wn <word you want to look up> -antsa'

     

    *type '-ants(n)oun | (v)erb | (a)djective | (r)adverb' to look up antonyms of a word

    *type '-syns(n)oun | (v)erb | (a)djective | (r)adverb' to look up synonyms of a word

     

    For more information about what you can do with wn, type 'man wn' in the terminal.  Have fun! 

  • by SGIII,

    SGIII SGIII Oct 24, 2015 11:42 AM in response to kt.kthapar
    Level 6 (10,710 points)
    Mac OS X
    Oct 24, 2015 11:42 AM in response to kt.kthapar

    Time and technology march on.  On my MBP with force touch trackpad all I have to do is one click (with a little more force).

    Screen Shot 2015-10-24 at 14.37.24.png

     

    No internet connection, no Dictionary app, no shell scripts.

     

    SG

  • by Roote,

    Roote Roote Oct 31, 2015 11:53 AM in response to kt.kthapar
    Level 2 (417 points)
    Oct 31, 2015 11:53 AM in response to kt.kthapar

    Copy and paste the following Python script as plain text into a new TextEdit document. Save it as dict.py to your Scripts folder located at /Users/<username>/Library/Scripts/. Adjust the textwrap column width (width=76), and the initial_indent and  subsequent_indent spacings to your preference:

    #!/usr/bin/env python2.7
    
    import sys
    import DictionaryServices
    import textwrap
    
    def main():
        try:
            search_term = sys.argv[1]
        except IndexError:
            print 'No search term was entered.'
            sys.exit()
        search_result = DictionaryServices.DCSCopyTextDefinition(
                None, search_term, (0, len(search_term)))
        if not search_result:
         print '"%s" was not found in Dictionary.' % (search_term)
        else:
            term_definition = search_result.replace(
                    u'\u25B6', '').replace('ORIGIN', ' ORIGIN')
         single_spacing = ' '.join(term_definition.split())
            wrapper = textwrap.TextWrapper(width=76,
                                           initial_indent='      ',
                                           subsequent_indent='      ')
         print wrapper.fill(single_spacing)
    
    if __name__ == '__main__':
        main()
    

     

    Make the script executable with the following command in Terminal, substituting your username for <username>:

     chmod u+x /Users/<username>/Library/Scripts/dict.py
    

     

    If using the default bash shell in Terminal, place the following line in your ~/.bash_profile, substituting your username for <username>. If using a different shell, such a zsh, place it in that shell's profile. For zsh that would be ~/.zprofile:

    alias dict='/Users/<username>/Library/Scripts/dict.py'
    

     

    Then simply use the "dict" command with the search term you wish defined.

    dict.png

    If you prefer, use the following Python script to display the definition in a border. Adjust the textwrap column width (width=76) to your preference and the border will resize automatically:

    #!/usr/bin/env python2.7
    
    import sys
    import DictionaryServices
    import textwrap
    
    def main():
        try:
            search_term = sys.argv[1]
        except IndexError:
            print 'No search term was entered.'
            sys.exit()
        search_result = DictionaryServices.DCSCopyTextDefinition(
                None, search_term, (0, len(search_term)))
        if not search_result:
         print '"%s" was not found in Dictionary.' % (search_term)
        else:
            term_definition = search_result.replace(
                    u'\u25B6', '').replace('ORIGIN', ' ORIGIN')
         single_spacing = ' '.join(term_definition.split())
            border_text = textwrap.fill(single_spacing, width=76).splitlines()
            max_length = max(len(s) for s in border_text)
            column_width = max_length + 2
            print '+' + '-' * column_width + '+'
            for s in border_text:
                print '| %-*.*s |' % (max_length, max_length, s)
            print '+' + '-' * column_width + '+'
    
    if __name__ == '__main__':
        main()
    

    dictbox.png

    The elapsed time to return a definition is acceptable, generally less than a third of a second (on my 2008 MacBook Pro).

    time.png

  • by VikingOSX,

    VikingOSX VikingOSX Oct 31, 2015 12:33 PM in response to Roote
    Level 7 (21,061 points)
    Mac OS X
    Oct 31, 2015 12:33 PM in response to Roote

    You should understand that even if you have syntax checked your Python code, and there are absolutely no PEP errors indicated, the Jive hosting software in the Support Community will jack your indentation around and copy/pasting it will result in syntax errors while running it in the Terminal after its visit to TextEdit. The latter is not a programmer's editor and it may actually introduce additional code indentation and encoding issues.

     

    The same code, when copied and pasted from your post into SublimeText 3 using its Paste and Indent menu item, fixed all but two indentation errors caused by Jive. Since I use the Anaconda syntax checking package for Sublime Text 3, I can see all of the Python PEP syntax errors that are flagged in the code with English language reason, and watch them disappear as I fix them. After running your code through the Sublime Text 3 editor, it now works without any errors on El Capitan 10.11.1.

     

    You do not need to explicitly refer to Python 2.7. Just use Python and the most recently installed version will be used. As of El Capitan, Apple does not provide Python 3. It would be useful to include those versions of OS X where you have tested your code.

  • by Roote,

    Roote Roote Oct 31, 2015 12:52 PM in response to Roote
    Level 2 (417 points)
    Oct 31, 2015 12:52 PM in response to Roote

    Looks like the indenting in the above Python scripts didn't survive entirely intact when posting and will throw a "IndentationError: unindent does not match any outer indentation level" message. Indent scripts as shown below:

     

    dictpy.png

    dictboxpy.png

  • by Roote,

    Roote Roote Oct 31, 2015 1:11 PM in response to VikingOSX
    Level 2 (417 points)
    Oct 31, 2015 1:11 PM in response to VikingOSX

    Thanks VikingOSX. I'll keep your advice in mind in future. What is the functionally ideal way to post Python code, for example, in Apple Support Communities?

Previous Page 2 of 3 last Next