1 ;;; wordnet.el --- an interface for Word Net
3 ;; Copyright (C) 2004, 2007 William Xu
5 ;; Author: William Xu <william.xwl@gmail.com>
6 ;; Created: 2004/10/21 19:44:23
8 ;; Keywords: convenience
9 ;; Url: http://xwl.appspot.com/ref/wordnet.el
10 ;; Last updated: 2007/09/10 17:04:39
12 ;; This program is free software; you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 2, or (at your option)
17 ;; This program is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with this program; if not, write to the Free Software
24 ;; Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
28 ;; A simple interface for the dictionary - Word Net
30 ;; Put this file into your load-path and the following into your ~/.emacs:
33 ;; Recommended key binding:
34 ;; (global-set-key (kbd "M-s") 'wordnet-search)
38 ;; wordnet.el is an interface for the dictionary -- Word Net, which's
39 ;; very wonderful! Although there is already a dictionary.el, which is
40 ;; used as an interface for dictd, the dictionary.el has not made full
41 ;; use of Word Net! It only uses wordnet's "-overview" option. The other
42 ;; options like "-syns, -hypo, ants, ..." are all ignored!
44 ;; I've also found "Thomas Link AKA samul AT web DOT de" 's wordnet.el
45 ;; before i wrote this, but it won't work, which kept on complaining
46 ;; some functions not found. It was based on XEmacs, but i'm using GNU
47 ;; Emacs. Having tried to hack wordnet.el for a while, i gave up. :(
48 ;; And I wrote this simple one myself.
58 (defcustom wordnet-command
"wn"
59 "Shell command for wordnet."
63 (defcustom wordnet-mode-hook nil
64 "Normal hook run after entering wordnet mode."
68 (defvar wordnet-options
69 "-antsn -antsv -antsa -antsr\
73 -synsn -synsv -synsa -synsr\
86 -domnn -domnv -domna -domnr\
87 -domtn -domtv -domta -domtr\
88 -famln -famlv -famla -famlr\
94 -grepn -grepv -grepa -grepr\
99 (defun wordnet-quit ()
100 "Bury Word Net buffer."
102 (delete-windows-on (buffer-name)))
104 (defun wordnet-next-sense ()
108 (search-forward-regexp "^Sense\\ [0-9]\\|^[0-9]" nil t
)
111 (defun wordnet-prev-sense ()
112 "Goto previous Sense."
115 (search-backward-regexp "^Sense\\ [0-9]\\|^[0-9]" nil t
)
118 (defun wordnet-antonyms ()
119 "Goto -ants{n|v|a|r}."
121 (goto-char (point-min))
122 (search-forward-regexp "^Antonyms\\ of" nil t
)
125 (defun wordnet-synonyms ()
126 "Goto -syns{n|v|a|r}."
128 (goto-char (point-min))
129 (search-forward-regexp "^Synonyms\\ of\\|^Synonyms/Hypernyms\\|Similarity\\ of" nil t
)
132 (defun wordnet-hyponyms ()
133 "Goto -hypo{n|v}, -tree{n|v}."
135 (goto-char (point-min))
136 (search-forward-regexp "^Hyponyms\\ of" nil t
)
139 (defun wordnet-overview ()
142 (goto-char (point-min))
143 (search-forward-regexp "^Overview\\ of" nil t
)
146 (defvar wordnet-font-lock-keywords
148 (regexp-opt '("Antonyms" "Synonyms" "Hyponyms" "Member"
149 "Substance" "Part" "Meronyms" "Holonyms"
150 "Attributes" "Derived" "Domain" "Familiarity"
151 "Coordinate" "Grep" "Overview" "Similarity"
152 "Pertainyms" "Troponyms" "Entailment"))
154 (0 font-lock-keyword-face t t
))
155 "Keywords to highlight in wordnet mode.")
157 (defvar wordnet-mode-map
158 (let ((map (make-sparse-keymap)))
159 (define-key map
(kbd "q") 'wordnet-quit
)
160 (define-key map
(kbd "n") 'wordnet-next-sense
)
161 (define-key map
(kbd "p") 'wordnet-prev-sense
)
162 (define-key map
(kbd "a") 'wordnet-antonyms
)
163 (define-key map
(kbd "s") 'wordnet-synonyms
)
164 (define-key map
(kbd "h") 'wordnet-hyponyms
)
165 (define-key map
(kbd "o") 'wordnet-overview
)
168 (define-derived-mode wordnet-mode nil
"WordNet"
169 "Major mode for WordNet dictionary search.
170 \\{wordnet-mode-map}"
171 (setq font-lock-defaults
'(wordnet-font-lock-keywords))
172 (run-hooks 'wordnet-mode-hook
))
174 (defun wordnet-search (word)
175 "Search the WORD with WordNet if given.
176 It presents the word at point as default input and allows editing it."
177 (interactive (list (read-string "Wordnet: " (current-word))))
179 (setq word
(read-string "Wordnet: ")))
180 (let ((buf (get-buffer-create "*WordNet*")))
181 (with-current-buffer buf
182 (unless (eq major-mode
'wordnet-mode
)
184 (setq buffer-read-only t
)
185 (let ((inhibit-read-only t
))
188 (shell-command-to-string
189 (format "%s %s %s" wordnet-command word wordnet-options
)))
191 ;; switch to *WordNet* buffer
192 (unless (eq (current-buffer) buf
)
193 (unless (cdr (window-list))
194 (split-window-vertically))
196 (switch-to-buffer buf
))))
198 (defalias 'wordnet
'wordnet-search
)
202 ;;; wordnet.el ends here