You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
47 lines
1.8 KiB
47 lines
1.8 KiB
(require 'json) |
|
(require 'thingatpt) |
|
|
|
(setq wordnik-api-key "fa815fa92045b7fa23699014d0a01c081b3d9c3c761292bf3") |
|
|
|
(defun wordnik-get-syn-buffer-for-word (word) |
|
(url-retrieve-synchronously (concat "http://api.wordnik.com//v4/word.json/" word "/relatedWords?relationshipTypes=synonym&limitPerRelationshipType=25&api_key=" wordnik-api-key))) |
|
|
|
(defun thesaurus-process-http-headers () |
|
"In the buffer created by `url-retrieve-synchronously', |
|
there are HTTP headers, and content. This fn removes the headers |
|
from the buffer, parsing the Content-Length header to verify that |
|
a substantive response was received. |
|
|
|
This implementation deletes each line until finding a blank line, |
|
which in correctly-formatted HTTP messages signals the end of the |
|
headers and the beginning of the message content. |
|
" |
|
(let ((clength -1)) |
|
(while (/= (point) (line-end-position)) |
|
(when (and (< clength 0) |
|
(re-search-forward "^[Cc]ontent-[Ll]ength ?: *\\(.*\\)$" (line-end-position) t)) |
|
(setq clength (string-to-number (match-string 1))) |
|
(goto-char (line-beginning-position))) |
|
(delete-region (point) (line-end-position)) |
|
(delete-char 1)) |
|
(delete-char 1) |
|
clength)) |
|
|
|
(defun wn/synonym-list (word) |
|
"Look up synonyms for (word) on wordnik and return them as an array of strings" |
|
(let ((buf (wordnik-get-syn-buffer-for-word word))) |
|
(if buf |
|
(progn |
|
(with-current-buffer buf |
|
(goto-char (point-min)) |
|
(thesaurus-process-http-headers) |
|
(let ((json-object-type 'alist)) |
|
(cdr (assoc 'words (elt (json-read-from-string (buffer-substring-no-properties (point) (line-end-position))) 0))))))))) |
|
|
|
(defun wn/synonym-at-point () |
|
(interactive) |
|
(let ((word (word-at-point))) |
|
(message "Synonyms for %s: %S" word (wn/synonym-list word)))) |
|
|
|
(provide 'wordnik-synonym) |
|
|
|
|