Bash completion

From Apertium
Revision as of 12:00, 22 September 2015 by Unhammer (talk | contribs) (fmt wuz here)
Jump to navigation Jump to search

If you want “intelligent” bash completion on TAB for apertium, lttoolbox, vislcg3 and hfst, do e.g.:

$ git clone ~/apertium-completion

and add this to your ~/.bashrc:

if ! shopt -oq posix && \
   [[ ( -z "$INSIDE_EMACS" || "$EMACS_BASH_COMPLETE" = "t" ) ]]; then
    for f in ~/apertium-completion/completions/*; do
        [[ -f $f ]] && source "$f"

Alternatively, if your OS sources files from e.g. /etc/bash_completion_d automatically, just sudo cp ~/apertium-completion/completions/* /etc/bash_completion_d/ (then you don’t have to add anything to ~/.bashrc).

Now open a new terminal and try typing

  • "apertium" and press TAB twice, you should get a list of installed language pairs
  • "apertium -d ." and press TAB twice should show you a list of the possible translation modes in this directory
  • ."/" and press TAB twice and you should get --with-langN=../../languages/apertium-foo (if the pair has monolingual dependencies)

A little recording showing off the tips on this page:

apertium -d . takes too long to type

PROTIP: If you add

"\e\C-d": "apertium -d . \t\t"

to ~/.inputrc you can simply type "alt+ctrl+d" and your terminal will fill out "apertium -d . " and press TAB twice (showing the list of modes of your current directory).

I checked out all of SVN – how do I quickly cd to apertium-lol-wat?

Save to e.g. ~/src/, then put "source ~/src/" in ~/.bashrc

Now you can type "ca nno" to go to languages/apertium-nno and "ca kir kaz" to go to nursery/apertium-kaz-kir – even though you typed it in the other order.

I'm sicking of typing "cd ../../languages/apertium-lol; svn up; cd ../apertium-wat; svn up; cd ../../incubator/apertium-lol-wat"

If you've got a language pair that depends on monolingual languages-modules, you can put the following function in your ~/.bashrc, open a new terminal and then just type "up" in the language pair to update the dependents as well:

up () 
    grep ^AP_SRC config.log 2> /dev/null | while IFS='=' read -r var dir; do
        printf "%s\t" "${var}";
	( [[ -z "$1" ]] && cd "${dir//\'}" && up last )
    if svn info &> /dev/null; then
        printf "%s\t%s\t" svn "$(pwd)";
        svn up;
        if git config --get svn-remote.svn.fetch &> /dev/null; then
            printf "%s\t%s\t" git-svn "$(pwd)";
            git svn rebase;
            if git config --get core.bare &> /dev/null; then
                printf "%s\t%s\t" git "$(pwd)";
                git pull;
                echo "No repo found" 1>&2;

This script works even if you used git or git-svn for any of the dependents :-)

Can I get syntax highlighting of apertium output?


Ap colour.png

? Sure. Put the following in your ~/.bashrc

__ap_colour () {
    gawk '
    BEGIN {
        $0 = gensub(/#([^ <]+)/, redbg"#\\1"none, "g")
        $0 = gensub(/(^| )@([^ <]+)/, "\\1"yellowbg"@\\2"none, "g")
        $0 = gensub(/[*]([^ <]+)/, white"*\\1"none, "g")
        $0 = gensub(/([^>])<([^>]+)>/, "\\1<"GREEN"\\2"none">", "g")
        $0 = gensub(/><(@[^>]+)/, "><"yellow"\\1"none, "g")
        $0 = gensub(/ (@[^ ]+)/, " "yellow"\\1"none, "g")
        $0 = gensub(/><([^>]+)/, "><"green"\\1"none, "g")
        $0 = gensub(/[<>{}^$\/]/, grey"\\0"none, "g")

apertium () {
    if [[ ${TERM} != dumb && -t 1 ]]; then
        command apertium "$@" | __ap_colour
        command apertium "$@"

It's a bash function that wraps the apertium script and calls the regular apertium program, appending syntax highlighting if we're outputting to an interactive terminal.

If you want to colour stuff that's already been through apertium, just append | __ap_colour. For example, grep stuff processed_corpus.txt | fmt -120 | __ap_colour.

External links