convert.lisp 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. (ql:quickload '(:dexador :nyaml ))
  2. (defun dehash (value)
  3. (typecase value
  4. (list (mapcar 'dehash value))
  5. (string value)
  6. (array (map 'list 'dehash value))
  7. (hash-table (loop for k being the hash-keys in value using (hash-value v)
  8. collect (cons k (dehash v))))
  9. (t value)))
  10. (defparameter +base-uri+ "https://github.com/OpenCageData/address-formatting/raw/master/conf/")
  11. (defun write-sexp (path value)
  12. (with-open-file (s path
  13. :direction :output
  14. :if-exists :supersede
  15. :if-does-not-exist :create
  16. :external-format :utf-8)
  17. (write value :stream s)))
  18. (defun convert (file &optional multi)
  19. (let* ((url (concatenate 'string +base-uri+ file))
  20. (name (subseq file (1+ (or (position #\/ file :from-end t) -1))
  21. (position #\. file :from-end t)))
  22. (data (nyaml:parse (dex:request url) :multi-document-p t))
  23. (alist (dehash (cdr data))))
  24. (write-sexp (format nil "~a.sexp" name) (if multi alist (car alist)))
  25. (format t "done~%")))
  26. (defparameter +abbrev-uri+ "https://github.com/OpenCageData/address-formatting/raw/master/conf/abbreviations/~a.yaml")
  27. (defparameter +langs+ '("ca" "cs" "da" "de" "en" "es" "et" "eu" "fi" "fr" "hu" "it" "nl"
  28. "no" "pl" "pt" "ro" "ru" "se" "sk" "tr" "uk" "vn"))
  29. (defun convert-abbrevs ()
  30. (write-sexp "abbrevs.sexp"
  31. (loop for lang in +langs+
  32. for uri = (format nil +abbrev-uri+ lang)
  33. do (format t "~a.." lang)
  34. collect (cons lang (dehash (nyaml:parse (dex:request uri))))))
  35. (terpri))
  36. (defun convert-all ()
  37. (convert-abbrevs)
  38. (loop for file in '("countries/worldwide.yaml"
  39. "country2lang.yaml"
  40. "county_codes.yaml"
  41. "state_codes.yaml")
  42. do (format t "Processing ~a.." file)
  43. do (convert file))
  44. (convert "components.yaml" t))