Introduction

During my days behind the screen I frequently need to translate from one language to another. I regularly look up word definitions, synonyms and dig into Wikipedia for details.

This comes from being a polyglot - if speaking more than two languages makes one. Then, I confess to having a serious language fetish. I binge daily on dictionaries, thesauruses and encyclopedias in both oflline and online form, sometimes deep into the night.

Opening a browser just to find a meaning of word dungaree seems heavy. With a few bash functions I made this quicker and easier. The wisdom of dictionaries is now available at a few quick keystrokes.

Tools

There are countless open source tools for querying dictionaries, thesauruses, wikipedia and alikes. The following ones are sufficient for my purposes:

I have installed them on my Linux and MacOS machines using brew package manager:

brew install dict translate-shell wiki

Yes, you can use brew on Linux just like on a Mac. There are many good reasons for using it, hopefully one day I’ll write an article about it.

Bash Functions

Each of the installed tools has a myriad of options. Since I only need a few of them, I’ve defined a few bash commands which do exactly what I need. Now I can do my queries using much simpler syntax as described below. The source code of these functions is found in the next chapter.

Translating Texts

Custom command t translates texts between languages using translate-shell tool. For example:

t "What's up" fr en

will print

Quoi de neuf

phrase
  Qu'est-ce qu'il y a?
    What's the matter?, What's up?, What's happened?
  Quoi de neuf?
    What's new?, What's up?
  Qu'est-ce qui s'est passé?
    What's happened?, What's up?

Inside the command I have defined the most often used source and target language - in my case it’s from English to French. If I need to translate an English text to French, I simply type:

t "What's up"

Dict tool can even speak the translation for you. This requires presence of a command-line sound player on your system, such as mplayer, mpg123 or mpv:

t "Goddamit!" en fr say

Finding Word Definitions

Custom command define finds definition of a word using dict tool and The Collaborative International Dictionary of English back-end:

define superfluous

will print

From The Collaborative International Dictionary of English v.0.48 [gcide]:

  Superfluous \Su*per"flu*ous\, a. [L. superfluus overflowing;
    super over, above + fluere to flow. See {Super-}, and {Fluent}.]
    More than is wanted or is sufficient; rendered unnecessary by
    superabundance; unnecessary; useless; excessive; as, a
    superfluous price.
      ...

Finding Word Synonyms

Custom command synonym finds synonyms of a word using dict tool and Moby Thesaurus back-end:

synonym superfluous

will print

From Moby Thesaurus II by Grady Ward, 1.0 [moby-thesaurus]:
73 Moby Thesaurus words for "superfluous":

accessory, accidental, additional, adscititious, adventitious, aimless, appurtenant, ascititious, auxiliary, casual, circumstantial, collateral, contingent, de trop, dispensable, excess, excessive, expendable, expletive, extra, feckless, fortuitous, gratuitous, impotent, in excess, incidental, ineffective, ineffectual, inessential, left, leftover, meaningless, needless, net, no go, nonessential, odd, of no use, outstanding, over, pleonastic, pointless, prolix, purposeless, redundant, remaining, remanent, secondary, spare, subsidiary, superabundant, superadded, supererogatory, supernumerary, supervenient, supplemental, supplementary, surplus, surviving, tautologic, tautologous, to spare, unasked, unavailing, uncalled-for, unconsumed, unessential, unnecessary, unneeded, unused, unwanted, useless, verbose

Looking up Wikipedia definitions

Custom command wiki finds definitions on Wikipedia using wiki tool and http://wikipedia.org back-end:

wiki underdog

will print the following:

An underdog is a person or group in a competition, usually in sports and creative works, who is popularly expected to lose. The party, team, or individual expected to win is called the favorite or top dog. In the case where an underdog wins, the outcome is an upset. An "underdog bet" is a bet on the underdog or outsider for which the odds are generally higher.

The first recorded uses of the term occurred in the second half of the 19th century; its first meaning was "the beaten dog in a fight".In British and American culture, underdogs are highly regarded. This harkens to core Judeo-Christian stories, such as that of David and Goliath, and also ancient British legends such as Robin Hood and King Arthur, and reflects the ideal behind the American dream, where someone who is poor and/or weak can use hard work to achieve victory. Underdogs are most valorized in sporting culture, both in real events, such as the Miracle on Ice, and in popular culture depictions of sports, where the trope is omnipresent. The idea is so common that even when teams are evenly matched, spectators and commentators are drawn to establishing one side as the underdog. Historian David M. Potter explained that underdogs are appealing to Americans not because they simply beat the odds, but overcome an injustice that explains those odds - such as the game being unfairly rigged due to privilege and power.In a story, the Fool is often an underdog if they are the main character. Their apparent ineptitude leads to people underestimating their true abilities, and they are able to win either through luck or hidden wisdom against a more powerful, "establishment" villain. An example in film is The Tramp portrayed by Charlie Chaplin.

Read more: [https://en.wikipedia.org/wiki/Underdog]()

The link at the bottom comes handy - you can easily navigate to the full Wikipedia article if your curiosity is not fully satisfied.

To search Wikipedia in another language, specify language code before the term, for example:

wiki pl "Radon"

to see some fun facts about a certain noble gas, in Polish.

Source Code

All commands are defined as custom bash functions. To install them, first make sure you’ve installed the three tools: dict, wiki and translate-shell. Then copy and paste the following inside your .bashrc or .bash_profile or wherever else you keep your custom bash code.

After you’ve added the code, restart the terminal to make the new commands available.

On my own PCs have a file ~/.bash_functions which is loaded by ~/.bashrc with source ~/.bash_functions command, just like I also have ~/.bash_aliases and ~/.bash_prompt for other shell customizations. This helps me keep my .bashrc clean and all customizations easily portable. You can find here a .bash_functions file which contains all the described functions.

Command t

# Wrapper around translate-shell
t () {
  # Default languages
  DEFAULT_FROM="en"
  DEFAULT_TO="fr"

  if [ $# -eq 0 ]; then
    echo "Command-line translator"
    echo "Based on translate-shell, see: https://github.com/soimort/translate-shell"
    echo
    echo "Usage:"
    echo "      t <text> <from> <to>"
    echo "i.e."
    echo "  English to French:"
    echo "      t \"Hello!\" en fr"
    echo "  German to Polish:"
    echo "      t \"Herzlich wilkommen!\" de pl"
    echo "  Polish to Simplified Chinese:"
    echo "      t \"Jak się masz?\" pl ZH-CN"
    echo
    echo "Default languages, used when <from> or <to> are not specified:"
    echo "       from: $DEFAULT_FROM"
    echo "       to:   $DEFAULT_TO"
    echo "i.e."
    echo "  Default source language ($DEFAULT_FROM) to default target language ($DEFAULT_TO)"
    echo "      t \"Good morning!\""
    echo "  Specified language to default target language ($DEFAULT_TO)"
    echo "      t \"Guten morgen!\" ru"
    echo
    echo "Other options:"
    echo "      -h Show list of supported languages"
    echo "      -b Show a brief translation"
    echo "      -p Speak out the translation (requires mplayer, mpg123 or mpv)"
    echo
  elif [ "$1" = "-h" ] || [ "$1" = "--help"  ]; then
    echo "Command line translator"
    echo "Supported languages:"
    trans -R

  else
    # determine text to translate, source and target languages
    TEXT="$1"
    FROM="${2:-$DEFAULT_FROM}"
    TO="${3:-$DEFAULT_TO}"
    SAY="" && [[ "$4" = "say" ]] && SAY="-p"
    # translate
    trans "$TEXT" -s "$FROM" -t "$TO" "$SAY" "$5" "$6" "$7" "$8" "$9"
  fi
}

Command define

# Word definition search using `dict`
define () {
  if [ $# -eq 0 ]; then
    echo "Word definition search"
    echo "Uses dict and The Collaborative International Dictionary of English database (gcide)"
    echo "Syntax:"
    echo "     dict <word>"
  else
    dict -d gcide "$1" | tail -n+2
  fi
}

Command synonym

# Thesaurus search using `dict`
synonym () {
  if [ $# -eq 0 ]; then
    echo "Thesaurus search"
    echo "Uses dict and Moby Thesaurus database (moby-thesaurus)"
    echo "Syntax:"
    echo "     dict <word>"
  else
    dict -d moby-thesaurus "$1" | tail -n+2
  fi
}

Command wiki

# Wiki search
wiki () {
  # Default search language
  DEFAULT_LANGUAGE="en"

  if [ $# -eq 0 ]; then
    echo "Wikipedia search"
    echo "Uses wiki tool from https://github.com/walle/wiki"
    echo "Syntax:"
    echo "     wiki <term>"
    echo "     wiki <language> <term>"

  elif [ "$1" = "-h" ] || [ "$1" = "--help" ]; then
    command wiki -h

  elif [ $# -eq 1 ]; then
    command wiki "$1"
  else
    LANGUAGE="${1-:$DEFAULT_LANGUAGE}"
    command wiki -l "$LANGUAGE" "$2" "$3" "$4" "$5" "$6"
  fi
}

LICENSE

Copyright 2020, Tomasz Waraksa

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.