;;; 05-tatoeba.el --- Tatoeba utilities. -*- no-byte-compile: t; lexical-binding: t; -*- ;;; Description: ;; This file contains my utilities for the tatoeba website. ;;; Code: (defun tatoeba--request-word-data (word) "Gets raw senteces from the tatoeba api" (request-response-data (request (format "https://tatoeba.org/eng/api_v0/search?from=deu&has_audio=&list=&native=&original=&orphans=no&query=%s&sort=relevance&sort_reverse=&tags=&to=eng&trans_filter=limit&trans_has_audio=&trans_link=&trans_orphan=no&trans_to=&trans_unapproved=no&trans_user=&unapproved=no&user=&word_count_max=&word_count_min=2" word) :parser 'json-read :sync t))) (defun tatoeba--get-eng-translations (translations) "Converts the translations list into the (id . translation) form" (let* ((translation-list (append (aref translations 0) (aref translations 1) '()))) (mapcar (lambda (x) (if (equal (cdr (assoc 'lang x)) "eng") (cons (cdr (assoc 'id x)) (cdr (assoc 'text x))))) translation-list))) (defun tatoeba--get-sentence-list (word) "Requests sentences and transforms them into an easy to use form." (let* ((raw-data (tatoeba--request-word-data word)) (results (cdadr raw-data))) (mapcar (lambda (x) (list (assoc 'id x) (cons 'user (cdadr (assoc 'user x))) (assoc 'text x) (cons 'translations (tatoeba--get-eng-translations (cdr (assoc 'translations x)))))) results))) (defun tatoeba-get-capture-template () "Returns a template for the org-capture" (let* ((word (read-string "Word: ")) (sentence-list (tatoeba--get-sentence-list word))) (with-output-to-string (princ (format "\n** %s\n" word)) (dolist (sentence sentence-list) (if (not (eq (cdadr (assoc 'translations sentence)) nil)) (princ (format "*** %s\n%s\n\n" (cdr (assoc 'text sentence)) (cdadr (assoc 'translations sentence))))))))) ;;; 05-tatoeba.el ends here