Emacs

From Apertium
(Redirected from Dix-mode)
Jump to: navigation, search

Contents

Info on using Emacs for Apertium-related tasks.

[edit] Quickstart

There is an init file in SVN that will give your Emacs some useful Apertium-related packages and settings, including:

  • dix-mode, for XML dictionary and transfer editing
  • cg-mode, for Constraint Grammar rule editing and testing
  • hfst-mode, for lexc/twol syntax highlighting
  • C++ settings to match the indentation settings most used in Apertium
  • tab-completion on words you've used a lot

To get that set up, simply check out https://svn.code.sf.net/p/apertium/svn/trunk/apertium-tools/emacs somewhere, and put

(load "/PATH/TO/apertium-tools/emacs/init-apertium.el")

in the file ~/.emacs.d/init.el (you may have to mkdir ~/.emacs.d first). Then start up Emacs, and it will download some new packages on first startup.

If you ever want to update your installed Emacs packages, you do M-x list-packages, then U x.


Full example:

cd
svn co https://svn.code.sf.net/p/apertium/svn/trunk/apertium-tools apertium-tools-emacs
mkdir -p ~/.emacs.d
echo '(load "~/apertium-tools-emacs/init-apertium.el")' >  ~/.emacs.d/init.el
emacs


The rest of this page gives some documentation of the various modes.

[edit] Mac OS X

If you're on Mac, the built-in emacs is ancient. Don't use that. Instead, get https://emacsformacosx.com/

You can make an alias to start this emacs from the command line with e.g.

alias em="open -a /Applications/Emacs.app"

(If you prefer having non-GUI emacs, change that to alias em="open -a /Applications/Emacs.app -nw".)

[edit] Validation slow?

The above init-apertium.el turns on on-the-fly XML validation, which can be slow on old computers. If editing large .dix files seems too slow, try turning off one or both of the validators by putting

(add-hook 'nxml-mode-hook (lambda () (rng-validate-mode 0)) 'append)
(add-hook 'dix-mode-hook (lambda () (flycheck-mode 0)) 'append)

in your ~/.emacs.d/init.el

[edit] XML (dix, transfer, …) editing

[edit] nxml-mode

Emacs has a nice xml editing mode (included as of version 23) called nXML, with syntax highlighting, movement commands to navigate through the XML (out of, into, across elements, etc.). It also has validation, and can auto-complete using the XML schema if a schema file is available.

init-apertium.el turns on nxml-mode for the common Apertium XML file extensions.

[edit] keybindings

init-apertium.el also turns on the variable nxml-sexp-element-flag, which lets you use the following handy keys:

  • C-M-f to move forward one element (e.g. from <e> to </e>)
  • C-M-b to move backward one element (e.g. from </e> to <e>)
  • C-M-d to move into one element (e.g. from <e> to <p>)
  • M-S-d (meta-shift-d) to move into one element backwards (e.g. from after </e> to after </p>)
  • C-M-u to move out of one element (e.g. from <p> to <e>)
  • C-M-k to kill (cut) one element

and nxml-slash-auto-complete-flag which lets you type

  • </ to write the end tag of whatever element you're in (e.g. after typing <e><p>…</p></, it'll complete with e>)

[edit] dix-mode

(thumbnail)
Screenshot of an older version of dix.el in Aquamacs (fullscreen). Upper left window has output from dix-view-pardef, lower left shows rng schema completion. There is a red underline since a p can't be an empty element, as noted by the message in the minibuffer

dix.el is a minor mode under nxml-mode which gives some handy Apertium-related functions for XML editing. It is installed and turned on for the relevant file extensions by init-apertium.el.

There are some short screencasts here showing off some usage.

I use Apertium-dixtools-formatted dix with one line per <e>, not all functions have been tested in more verbose formats, but I've tried to make the functions use XML-movements so mostly they should work no matter how you format your files.

When you open emacs (after adding the above lines to ~/.emacs) and load a .dix-file, you should see a menu named dix. Most of the functions added by dix-mode are shown in this menu (which also shows their keyboard shortcuts). Hovering over a menu-item might give a little popup-help. The Help for dix-mode entry will show all the user functions defined by dix-mode. The keyboard shortcuts are in general a lot more useful than the menu bar, which is mostly there in case you forget which buttons to press... Remember: C is Control, S is Shift, M is alt (well, M stands for Meta, but that's typically alt).


Some useful functions in dix-mode:

  • Movement and editing:
    • The space bar inserts a <b/> in <r>, <l> or <i> elements; a _ in par/pardef names; otherwise a plain space. This works with the . (repeat) command as well, if you use the vim keybindings.
    • M-n and M-p move to the next and previous "important bits" of <e>-elements (just try it!).
  • Copying elements and adding restrictions:
    • C-c C just creates a copy of the current <e> element, putting it below the current one
    • C-c L and C-c R also make a copy of the current <e> element, but with an LR or RL restriction
    • C-TAB cycles between the restriction possibilities LR, RL or none for the current <e> element
    • C-S-TAB, used with elements that have the slr/srl attribute, will swap the sense translation of this <e> with the <e> above
  • Creating elements from plain text:
    • C-c g in a monodix guesses the pardef for a word based on the suffix. Write a word in the bottom of a dix files, place point somewhere in the middle of the word, and hit C-c g, it'll try to find words earlier in the file that have the same ending (characters after point)
    • C-c x in a monodix or bidix turns a word-list into <e> entries using the above <e> entry as a template. Words should be written one per line. You can use it in a bidix by writing the left-side, then a colon (:) then the right-side. Assumes that the entry used as a template is written all on one line.
  • Pardef viewing and manipulation:
    • C-c G will go to the pardef of the nearest <par>
      • the place you left is saved in the standard emacs fashion, so you can go back by pressing C-u C-SPACE
    • C-c V will show the pardef of the nearest <par> in another window
    • C-c S will sort a pardef by its right-hand-side, <r>.
      • You can also do M-x dix-sort-e-by-l to sort the selected <e;> elements by the contents of their <l> element
    • C-c D (in a pardef or an <e>) will print a list of all pardefs which have the same suffixes as this one (where a 'suffix' is the contents of an <l>-element), useful for finding duplicates. Note: it ignores the tags
    • Inside a pardef, C-c A shows all usages of that pardef within the dictionaries represented by the variable `dix-dixfiles'


Note: capital letters means you have to press shift. If you fancy other keyboard shortcuts, copy the relevant define-key entries from the bottom of dix.el, put them in your ~/.emacs, e.g. to add F12 as an alternative to C-c V:

(add-hook 'dix-mode-hook (lambda nil (define-key dix-mode-map (kbd "<f12>") 'dix-view-pardef)))

(the whole add-hook thing is needed since dix-mode is not loaded until the first .dix-file is loaded)


Also, if you like having all <i> elements aligned at eg. column 25, select a region and do M-x align to achieve that (this also aligns <p> to 10 and <r> to 44, for bidix). These numbers are customizable with M-x customize-group RET dix. (Ie. there's no extra indentation function, but then, nxml already has that.)

[edit] dix-mode for transfer rules

(thumbnail)
Useful in transfer mode too!

There are some transfer-specific functions in dix-mode that make it worth turning on in transfer mode files too, e.g. C-c n, which lets you enter a rule number to go to (useful when tracing with apertium-transfer -t). The .emacs in the Quickstart section will turn on nxml-mode and dix-mode in transfer files (ie. all files with the suffix .t1x, .t2x, .t3x, etc.).

M-n and M-p (go to next/previous useful position) should also Do What You Mean in transfer files.

[edit] Validation (Relax NG-schemas)

nxml-mode uses Compact Relax NG schemas (.rnc files) for validation (without these, XML is only checked for well-formedness by nxml-mode).

dix.el should find the .rnc's installed by lttoolbox/apertium if you've installed with packages (or even with "sudo make install" to /usr/local). If not, you'll have to copy the schemas.xml included with https://github.com/unhammer/dix into some folder, editing the paths to .rnc's in there, and put (add-to-list 'rng-schema-locating-files "/path/to/your/schemas.xml") in your ~/.emacs.d/init.el.

You can toggle validation using the XML menu at the top of the screen, or the keyboard shortcut C-c C-v.

It can be a bit slow with big files; a

(add-hook 'nxml-mode-hook (lambda () (rng-validate-mode 0)) 'append)

will turn it off by default (or just do C-c C-v to turn it off once).

Validation can also provide "intelligent" tab completion of elements and attributes (see the function nxml-complete).

[edit] Linting with flycheck

The package https://github.com/unhammer/flycheck-apertium/ (installed by init-apertium.el) gives on-the-fly linting of dix files and transfer files.

For dix files, it assumes /usr/share/lttoolbox/dix.xsd exists (ie. you've installed lttoolbox from packages.

For transfer files, it assumes you've got https://github.com/ggm/vm-for-transfer-cpp compiled and installed to your $PATH; this gives some extra info on transfer errors. If you've got the binary somewhere outside your $PATH, set it like this:

  (setq flycheck-apertium-transfervm-executable  "/home/me/src/vm-for-transfer-cpp/apertium-compile-transfer")

Note that the line numbers given by transfervm are at the end of the matching rule, not always at the exact line where the error occurred. But it's better than segfaults.

If any of the above files don't exist, the checker will just silently not run.

[edit] Yasnippet

Yasnippet is a snippet-expansion package for Emacs. It lets you write boilerplate faster. This section shows how to use the snippets made for dix-mode. There's a short screencast of it at https://asciinema.org/a/11192

To use, install yasnippet by doing M-x package-refresh-contents and M-x package-install RET yasnippet RET (assuming you've added melpa to your package-archives; this happens automatically when you add init-apertium.el).

Then put this into ~/.emacs.d/init.el to make the snippets available in dix-mode:

(eval-after-load 'yasnippet
  '(progn
     (setq yas-verbosity 1)
     (yas-reload-all)
     (remhash 'nxml-mode yas--tables) ; until https://github.com/AndreaCrotti/yasnippet-snippets/issues/41 is solved
     (add-to-list 'yas-key-syntaxes 'dix-yas-skip-backwards-to-key)
     ; The default is to use a point-and-click menu when there are several choices, I prefer ido:
     (setq yas-prompt-functions '(yas-ido-prompt yas-completing-prompt yas-dropdown-prompt yas-no-prompt))
  ))
(add-hook 'dix-mode-hook 'yas-minor-mode)

[edit] C++

See Emacs C style for Apertium hacking.

[edit] HFST

[edit] CG

(thumbnail)
Screenshot after running the grammar (C-c C-c) on an rlx file in cg.el

There is a CG-mode for emacs in the vislcg3 repository (see Constraint Grammar). It's installed by init-apertium.el.


You can use C-; (alternative keybinding M-#) to quickly comment/uncomment a rule (quick demo). C-M-a/e move back and forth full rules (alternatively, M-a/e moves back/forth by "sentences" which includes commented rules).


If you want to test the CG while you're working on it from within Emacs, you can add a line like

# -*- cg-pre-pipe: "apertium -d . nb-nn-morph|cg-conv -a 2>/dev/null" -*-

to the top of your CG file (replace nb-nn-morph for whatever mode that runs everything up until cg-proc in your regular mode, or just use something like lt-proc some.automorf.bin|cg-conv -a 2>/dev/null). Then close and re-open the file, and hit ! when you're asked whether you approve of the command (you only have to do this once).

Now you can do C-c C-i to type in some test text, then C-c C-c (either in that buffer or in the CG buffer) to test the CG on the text. You can do C-c c to toggle if you want to test the text for every change you do (some might find that annoying). You can click REMOVE, SELECT, MAP, ADD etc. in the output to go to the corresponding line, or use C-c C-n / C-c C-p to go back and forth between occurrences (also works for warnings and compile errors).

(thumbnail)
Screenshot after running the grammar (C-c C-c) and hitting u to hide everything but analyses containing det dem

If you have a lot of input sentences you want to test at once, you can hide all analyses, except ones matching some regex. Select the output buffer, then hit u and type in a regex for analyses you want to see (e.g. vblex, or \b\(sg\|pl\)\b to match pl or sg but not the string "place"). Now you should see only the wordforms in the output buffer, except for analyses containing your exceptions. Type h to toggle between a full view and hiding (click a word when hiding and press h to ensure you're scrolled into the analysis of that word). See also the variable cg-sent-tag which is used to keep linebreaks after certain tags; if you use a non-Apertium sentence tag you may want to put in your ~/.emacs something like (setq cg-sent-tag "\\bpunct\\b") (if your sentence tag was punct).

[edit] IRC

Do M-x erc to start the IRC client. See http://www.emacswiki.org/emacs/ErcBasics and http://emacs-fu.blogspot.com/2009/06/erc-emacs-irc-client.html for more info.

[edit] See also

Personal tools