Files
japonca/util.lisp
2025-02-11 23:03:13 +03:00

40 lines
1.4 KiB
Common Lisp
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
(in-package :japonca)
(defun ırlıklı-rastgele (options weights &optional length)
;; (declare (type simple-array options) (type simple-array weights))
(let ((len (or length (length weights))))
(loop :with rand := (* (random 1.0) (aref weights (- len 1)))
:for i :from 0 :below (length weights)
:when (> (aref weights i) rand)
:do (return (aref options i)))))
(defun ırlık-ata (weights &optional length)
(let ((len (or length (length weights))))
(loop :for i :from 1 :below len
:do (incf (aref weights i)
(aref weights (- i 1))))
weights))
(defun dosya-metni-oku (dosya)
(let* ((uzunluk 0)
(metin
(with-output-to-string (out)
(with-open-file (in dosya :external-format :utf-8)
(loop :with buffer := (make-array 8192 :element-type 'character)
:for n := (read-sequence buffer in)
:while (< 0 n)
:do (incf uzunluk n)
(write-sequence buffer out :start 0 :end n))))))
(values metin uzunluk)))
;; (defun random-test (options weigths)
;; (let ((opts options)
;; (weights (set-weights-array weigths))
;; (times (make-array 3 :initial-element 0)))
;; (loop :for i :from 0 :below 1000
;; :for choice := (weighted-random opts weights)
;; :do (incf (aref times choice)))
;; times))