mirror of
https://github.com/SqrtMinusOne/reverso.el.git
synced 2025-12-10 15:53:02 +03:00
feat: synomyms API works
This commit is contained in:
parent
b6cb461a83
commit
dab776ec9d
1 changed files with 42 additions and 17 deletions
59
reverso.el
59
reverso.el
|
|
@ -50,7 +50,7 @@
|
||||||
japanese dutch polish portugese romanian
|
japanese dutch polish portugese romanian
|
||||||
russian swedish turkish ukrainian chinese))
|
russian swedish turkish ukrainian chinese))
|
||||||
(grammar . (english french))
|
(grammar . (english french))
|
||||||
(synonims . (arabic german english spanish french hebrew italian
|
(synonyms . (arabic german english spanish french hebrew italian
|
||||||
japanese dutch polish portugese romanian
|
japanese dutch polish portugese romanian
|
||||||
russian)))
|
russian)))
|
||||||
"Available languages for diferent operations.")
|
"Available languages for diferent operations.")
|
||||||
|
|
@ -194,7 +194,7 @@ that is called with the result."
|
||||||
(message "Error!: %S" error-thrown)))))
|
(message "Error!: %S" error-thrown)))))
|
||||||
|
|
||||||
(defun reverso--convert-string (dom)
|
(defun reverso--convert-string (dom)
|
||||||
"Convert html DOM from reverso API to fontified string.
|
"Convert html DOM from the reverso API to fontified string.
|
||||||
|
|
||||||
reverso.net uses tags to highlight relevant works, e.g. <em> for the
|
reverso.net uses tags to highlight relevant works, e.g. <em> for the
|
||||||
selected word in the context search. This function fontifies words
|
selected word in the context search. This function fontifies words
|
||||||
|
|
@ -215,7 +215,7 @@ that are in tags with `reverso-highlight-face'"
|
||||||
(rx (+ (syntax whitespace))) " ")))
|
(rx (+ (syntax whitespace))) " ")))
|
||||||
|
|
||||||
(defun reverso--convert-string-html (html)
|
(defun reverso--convert-string-html (html)
|
||||||
"Convert HTML string from reverso API to fontified string."
|
"Convert HTML string from the reverso API to fontified string."
|
||||||
(with-temp-buffer
|
(with-temp-buffer
|
||||||
(insert "<html><body>" html "<body></html>")
|
(insert "<html><body>" html "<body></html>")
|
||||||
(reverso--convert-string
|
(reverso--convert-string
|
||||||
|
|
@ -225,7 +225,7 @@ that are in tags with `reverso-highlight-face'"
|
||||||
'body)))))
|
'body)))))
|
||||||
|
|
||||||
(defun reverso--translate-parse (response)
|
(defun reverso--translate-parse (response)
|
||||||
"Convert RESPONSE from reverso into an alist."
|
"Convert RESPONSE from the reverso translation API into an alist."
|
||||||
(setq my/test2 response)
|
(setq my/test2 response)
|
||||||
(let ((corrected-text (alist-get 'correctedText response))
|
(let ((corrected-text (alist-get 'correctedText response))
|
||||||
(language-from (alist-get 'from response))
|
(language-from (alist-get 'from response))
|
||||||
|
|
@ -304,6 +304,9 @@ DATA is an html string."
|
||||||
(target . ,(reverso--convert-string trg))))))))
|
(target . ,(reverso--convert-string trg))))))))
|
||||||
|
|
||||||
(defun reverso--get-synomyms (text language cb)
|
(defun reverso--get-synomyms (text language cb)
|
||||||
|
"Get synomyms for TEXT in LANGUAGE.
|
||||||
|
|
||||||
|
CB is a function that is called with the result."
|
||||||
(unless (alist-get language reverso--language-mapping-1)
|
(unless (alist-get language reverso--language-mapping-1)
|
||||||
(error "Wrong language: %s" language))
|
(error "Wrong language: %s" language))
|
||||||
(request (concat (alist-get 'synomyms reverso--urls)
|
(request (concat (alist-get 'synomyms reverso--urls)
|
||||||
|
|
@ -323,25 +326,47 @@ DATA is an html string."
|
||||||
(message "Error!: %S" error-thrown)))))
|
(message "Error!: %S" error-thrown)))))
|
||||||
|
|
||||||
(defun reverso--get-synomyms-parse (html)
|
(defun reverso--get-synomyms-parse (html)
|
||||||
(setq my/test1 html)
|
"Parse the reverso synomyms page.
|
||||||
|
|
||||||
|
HTML is a string."
|
||||||
(let* ((dom (with-temp-buffer
|
(let* ((dom (with-temp-buffer
|
||||||
(insert html)
|
(insert html)
|
||||||
(libxml-parse-html-region (point-min) (point-max)))))
|
(libxml-parse-html-region (point-min) (point-max)))))
|
||||||
(cl-loop
|
(cl-loop
|
||||||
for child in (dom-non-text-children (dom-by-id dom "synomyms"))
|
for child in (dom-non-text-children
|
||||||
if (string-match-p (dom-attr child 'class) "wrap-hold-prop")
|
(dom-by-id (dom-by-tag dom 'body) "synonyms"))
|
||||||
|
if (string-match-p "wrap-hold-prop" (or (dom-attr child 'class) ""))
|
||||||
collect
|
collect
|
||||||
((kind . ,(car (dom-text (dom-by-class child "words-options"))))
|
`((kind . ,(string-trim (dom-texts (dom-by-class child "words-options"))))
|
||||||
(synonyms
|
(synonyms
|
||||||
. ,(cl-loop
|
. ,(cl-loop
|
||||||
for synonym in (dom-non-text-children
|
for synonym in (dom-non-text-children
|
||||||
(dom-by-class (car (dom-by-class child "word-opt"))
|
(dom-by-class (car (dom-by-class child "word-opt"))
|
||||||
"word-box"))
|
"word-box"))
|
||||||
collect (dom-text synonym)))))))
|
for a = (car (dom-by-tag synonym 'a))
|
||||||
|
collect
|
||||||
|
`((synonym . ,(string-trim (dom-texts synonym)))
|
||||||
|
(relevant
|
||||||
|
. ,(and (string-match-p "relevant" (or (dom-attr a 'class) "")) t)))))
|
||||||
|
(examples
|
||||||
|
. ,(cl-loop
|
||||||
|
for example in (dom-non-text-children
|
||||||
|
(dom-by-class child "phrases-examples"))
|
||||||
|
for span = (car (dom-by-tag example 'span))
|
||||||
|
if span
|
||||||
|
collect (reverso--convert-string span)))
|
||||||
|
(antonyms
|
||||||
|
. ,(cl-loop
|
||||||
|
for antonym in (dom-non-text-children
|
||||||
|
(dom-by-class (car (dom-by-class child "antonyms-wrapper"))
|
||||||
|
"word-box"))
|
||||||
|
for a = (car (dom-by-tag antonym 'a))
|
||||||
|
for text = (string-trim (dom-texts antonym))
|
||||||
|
unless (string-match-p (rx "...") text)
|
||||||
|
collect
|
||||||
|
`((antonym . ,text))))))))
|
||||||
|
|
||||||
;; (reverso--get-synomyms "Believe" 'english (lambda (data) (setq my/test2 data)))
|
;; (reverso--get-synomyms "Color" 'english (lambda (data) (setq my/test2 data)))
|
||||||
|
|
||||||
;; (reverso--get-synomyms-parse my/test1)
|
|
||||||
|
|
||||||
|
|
||||||
(provide 'reverso)
|
(provide 'reverso)
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue