Use external tool `w3m' for dumping html to plain texts.
[xwl-elisp.git] / wordnet.el
blob50db3c17b50ef52c6a58c78687929602448116b4
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
7 ;; Version: 2.0
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)
15 ;; any later version.
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.
26 ;;; Commentary:
28 ;; A simple interface for the dictionary - Word Net
30 ;; Put this file into your load-path and the following into your ~/.emacs:
31 ;; (require 'wordnet)
33 ;; Recommended key binding:
34 ;; (global-set-key (kbd "M-s") 'wordnet-search)
36 ;; History
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.
50 ;;; Code:
52 ;;; Variables:
54 (defgroup wordnet nil
55 "Wordnet interface."
56 :group 'wordnet)
58 (defcustom wordnet-command "wn"
59 "Shell command for wordnet."
60 :type 'string
61 :group 'wordnet)
63 (defcustom wordnet-mode-hook nil
64 "Normal hook run after entering wordnet mode."
65 :type 'hook
66 :group 'wordnet)
68 (defvar wordnet-options
69 "-antsn -antsv -antsa -antsr\
70 -hypen -hypev\
71 -hypon -hypov\
72 -entav\
73 -synsn -synsv -synsa -synsr\
74 -smemn\
75 -ssubn\
76 -sprtn\
77 -membn\
78 -subsn\
79 -partn\
80 -meron\
81 -holon\
82 -causv\
83 -perta -pertr\
84 -attrn -attra\
85 -derin -deriv\
86 -domnn -domnv -domna -domnr\
87 -domtn -domtv -domta -domtr\
88 -famln -famlv -famla -famlr\
89 -framv\
90 -coorn -coorv\
91 -simsv\
92 -hmern\
93 -hholn\
94 -grepn -grepv -grepa -grepr\
95 -over")
97 ;;; Functions:
99 (defun wordnet-quit ()
100 "Bury Word Net buffer."
101 (interactive)
102 (delete-windows-on (buffer-name)))
104 (defun wordnet-next-sense ()
105 "Goto next Sense."
106 (interactive)
107 (end-of-line)
108 (search-forward-regexp "^Sense\\ [0-9]\\|^[0-9]" nil t)
109 (beginning-of-line))
111 (defun wordnet-prev-sense ()
112 "Goto previous Sense."
113 (interactive)
114 (beginning-of-line)
115 (search-backward-regexp "^Sense\\ [0-9]\\|^[0-9]" nil t)
116 (beginning-of-line))
118 (defun wordnet-antonyms ()
119 "Goto -ants{n|v|a|r}."
120 (interactive)
121 (goto-char (point-min))
122 (search-forward-regexp "^Antonyms\\ of" nil t)
123 (beginning-of-line))
125 (defun wordnet-synonyms ()
126 "Goto -syns{n|v|a|r}."
127 (interactive)
128 (goto-char (point-min))
129 (search-forward-regexp "^Synonyms\\ of\\|^Synonyms/Hypernyms\\|Similarity\\ of" nil t)
130 (beginning-of-line))
132 (defun wordnet-hyponyms ()
133 "Goto -hypo{n|v}, -tree{n|v}."
134 (interactive)
135 (goto-char (point-min))
136 (search-forward-regexp "^Hyponyms\\ of" nil t)
137 (beginning-of-line))
139 (defun wordnet-overview ()
140 "Goto -overview."
141 (interactive)
142 (goto-char (point-min))
143 (search-forward-regexp "^Overview\\ of" nil t)
144 (beginning-of-line))
146 (defvar wordnet-font-lock-keywords
147 `(,(concat "^\\("
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"))
153 "\\).*")
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)
166 map))
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))))
178 (unless 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)
183 (wordnet-mode))
184 (setq buffer-read-only t)
185 (let ((inhibit-read-only t))
186 (erase-buffer)
187 (insert
188 (shell-command-to-string
189 (format "%s %s %s" wordnet-command word wordnet-options)))
190 (wordnet-overview)))
191 ;; switch to *WordNet* buffer
192 (unless (eq (current-buffer) buf)
193 (unless (cdr (window-list))
194 (split-window-vertically))
195 (other-window 1)
196 (switch-to-buffer buf))))
198 (defalias 'wordnet 'wordnet-search)
200 (provide 'wordnet)
202 ;;; wordnet.el ends here