Guest User

Untitled

a guest
Jun 6th, 2020
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lisp 0.77 KB | None | 0 0
  1. (defun chunk (n list &optional (acc '()))
  2.     (if (> n (length list))
  3.     acc
  4.     (chunk n
  5.            (subseq list 1)
  6.            (cons (subseq list 0 n)
  7.              acc))))
  8.  
  9. (defun hash-set (hash key value)
  10.   (setf (gethash key hash) value))
  11.  
  12. (defun hash-count-incr (hash key count)
  13.   (let ( (value (gethash key hash)) )
  14.     (if (not value)
  15.     (hash-set hash key count)
  16.     (hash-set hash key (+ count value)))))
  17.  
  18. ;; inifinite loop...?
  19.  
  20. (defun file->n-grams (n file-name)
  21.   (let ((n-gram-table (make-hash-table :test #'equal)))
  22.     (with-open-file (stream file-name)
  23.       (loop :for line := (read-line stream nil nil)
  24.         :while line :do
  25.        (mapcar (lambda (c) (hash-count-incr n-gram-table c 1))
  26.            (chunk n (str:split " " (string-downcase (string line)))))))
  27.     n-gram-table))
Advertisement
Add Comment
Please, Sign In to add comment