Mais umas quantas funções para geração de frases aleatórias

Funções em Emacs Lisp para geração de frases aleatórias
(defun sum (x)
  (cond (x
         (+ (car x) (sum (cdr x))))
        (t 0)))

(defun sum-cum (x)
  (setq y (reverse x))
  (reverse (cond (y
                  (cons (sum y) (sum-cum-aux (cdr y)))))))

(defun pos-search-string (str1 str2)
  "Lista de posições do fim da str1 na str2"
  (let ((i (- (length str2) 1))
        (l '())
        (j 1)
        (lf '()))
     (while (and (<= 0 i) (search str1 str2))
       (setq l (cons (search str1 str2) l)
             i (- i 1)
             str2 (break-string str2 (+ (car l) (length str1)))))
     (setq laux (sum-cum (nreverse l)))
     (while laux
       (setq lf (cons (+ (cond ((null laux) 0) (t (car laux)))
                         (* (length str1) j)) lf))
       (setq j (+ j 1))
       (setq laux (cdr laux)))
     (reverse lf)))

(defun break-string (l-str &optional n)
  "Retira N termos iniciais à L-STR."
  (when (null n)
    (setq n 0))
  (let ((i (- (length l-str) 1)) (l '()))
      (while (<= 0 i)
        (setq l (cons (aref l-str i) l)
              i (- i 1)))
      (concat (nthcdr n l))))

(defun string-to-l-strings (str)
    "Convert a string into a list of strings."
    (let ((i (- (length str) 1)) (l '()))
      (while (<= 0 i)
        (setq l (cons (aref str i) l)
              i (- i 1)))
      (mapcar (lambda (x) (char-to-string x)) l)))

(defun white-space-remove (v)
  (cond ((eq  (string-to-char '" ") (string-to-char v)) "")
        (t v)))

(defun white-space-rm-string (str)
  (join-string (mapcar (lambda (x) (white-space-remove x)) (string-to-strings str))))

(defun join-string (xs &optional sep)
  (cond ((null xs) "")
        ((null (cdr xs)) (car xs))
        (t (concat (car xs) (or sep "") (join-string (cdr xs) sep)))))

(defun rand-int (n)
  (floor (random n)))

(defun rchar (str context)
  "Returns a random character from a sample space, the set of all characters in CONTEXT, following STR."
  (setq context-nil-ws (white-space-rm-string context))
  (char-to-string (aref context-nil-ws
                        (nth (rand-int (length (pos-search-string str context-nil-ws)))
                             (pos-search-string str context-nil-ws)))))

(defun map-rchar (l-str context)
  (mapcar (lambda (x) (rchar x context)) l-str))

Ainda hoje mais qualquer coisa...

Palavras chave/keywords: Emacs, Lisp, frases aleatórias

Última actualização/Last updated: 2014-02-20 [14:38]


1999-2014 (ç) Tiago Charters de Azevedo

São permitidas cópias textuais parciais/integrais em qualquer meio com/sem alterações desde que se mantenha este aviso.

Verbatim copying and redistribution of this entire page are permitted provided this notice is preserved.