Advertisement
Guest User

spell

a guest
Dec 16th, 2020
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lisp 4.27 KB | None | 0 0
  1. (defun read-file (filename)
  2.   (with-open-file (o filename :direction :input)
  3.     (loop for line = (read-line o nil nil)
  4.           while line
  5.           collect line)))
  6.  
  7. ;; (defvar *dict* (make-hash-table :test #'eql))
  8.  
  9. (defvar *root* nil)
  10.  
  11. (defstruct node
  12.   state (children nil))
  13.  
  14. (defun has-childern (state)
  15.   (not (null (node-children state))))
  16.  
  17. (defmacro last-child (state)
  18.   `(car (node-children ,state)))
  19.  
  20. ;;(defun set-last-child (state newstate)
  21. ;;  (setf (cdr (last-child state)) newstate))
  22.  
  23. (defun null-string-p (string)
  24.   (string= "" string))
  25.  
  26. (defun theta (state char)
  27.   (cdr (assoc char
  28.               (node-children state)
  29.               :test #'eql)))
  30.  
  31. (defun theta* (state word)
  32.   (if (null word)
  33.       state
  34.       (let ((st (theta state (car word))))
  35.         (and st (theta* st (cdr word))))))
  36.  
  37. (defun explode (word)
  38.   (coerce word 'list))
  39.  
  40. (defun implode (list)
  41.   (coerce list 'string))
  42.  
  43. (defun common-prefix (word)
  44.   (labels ((aux (state word prefix)
  45.              (if (null word)
  46.                  (nreverse prefix)
  47.                  (let ((st (theta state (car word))))
  48.                    (if st
  49.                        (aux st (cdr word) (cons (car word) prefix))
  50.                        (nreverse prefix))))))
  51.     (aux *root* (explode word) nil)))
  52.  
  53. (defvar *register-a*)
  54. (defvar *register-b*)
  55.  
  56. (defun add-word (word)
  57.   (let* ((common-prefix (common-prefix word))
  58.          (last-state (theta* *root* common-prefix))
  59.          (current-suffix (explode (subseq word (length common-prefix)))))
  60.     (when (has-childern last-state)
  61.       (replace-or-register last-state))
  62.     (add-suffix last-state current-suffix)))
  63.  
  64. (defun node-labels (node)
  65.   (mapcar #'car (node-children node)))
  66.  
  67. (defun find-in-register (node)
  68.   (gethash (node-children node)
  69.            (if (node-state node) *register-a* *register-b*)
  70.            nil))
  71.  
  72. (defun regi (node)
  73.   (setf (gethash (node-children node)
  74.                  (if (node-state node) *register-a* *register-b*))
  75.         node))
  76.  
  77. (defun replace-or-register (state)
  78.   (let ((child (cdr (last-child state))))
  79.     (when (has-childern child)
  80.       (replace-or-register child))
  81.     (let ((q (find-in-register child)))
  82.       (if q
  83.           (setf (cdr (last-child state)) q)
  84.           (regi child)))))
  85.  
  86. (defun build-suffix (sfx)
  87.   (let* ((sfx (nreverse sfx))
  88.          (fst (cons (car sfx) (make-node :state t))))
  89.     (loop for c in (cdr sfx)
  90.           do (setf fst (cons c (make-node :state nil :children (list fst)))))
  91.     fst))
  92.  
  93. (defun add-suffix (last suffix)
  94.   (push (build-suffix suffix) (node-children last)))
  95.  
  96.  
  97. (defun hash-set (assoc)
  98.   (apply #'logxor (mapcar #'sxhash assoc)))
  99. #|
  100. (defun hash-set (assoc)
  101.   (let ((seed 1009)
  102.         (factor 9176))
  103.     (loop for p in assoc
  104.           do (setf seed (+ (sxhash p) (* seed factor))))
  105.     seed))
  106. |#
  107. #|
  108. (defun set-eq (a b)
  109.   (and (= (list-length a) (list-length b))
  110.        (loop for x in a
  111.              for y in b
  112.              always (and (eql (car x) (car y)) (eq (cdr x) (cdr y))))))
  113. |#
  114. (defun set-eq (a b)
  115.   (do ((x a (cdr x))
  116.        (y b (cdr y)))
  117.       ((or (null x) (null y)) t)
  118.     (let ((p (car x))
  119.           (q (car y)))
  120.       (if (not (and (eq (cdr p) (cdr q)) (eql (car p) (car q))))
  121.           (return nil)))))
  122.  
  123.  
  124. (defun main ()
  125.   (setf *root* (make-node)
  126.         *register-a*
  127.         (make-hash-table :test 'set-eq :hash-function 'hash-set :size 30000)
  128.         *register-b*
  129.         (make-hash-table :test 'set-eq :hash-function 'hash-set :size 150000))
  130.   (let ((words (read-file "words.linux"))
  131.         (count 0))
  132.     (loop for w in words
  133.           do (progn
  134.                (incf count)
  135.                (when (> count 1000)
  136.                  (princ "*")
  137.                  (setf count 0))
  138.                (add-word w)))
  139.     (replace-or-register *root*))
  140.   nil)
  141.  
  142. (defun check (word)
  143.   (let ((a (theta* *root* (explode word))))
  144.     (and a (node-state a))))
  145.  
  146. (defun test ()
  147.   (let ((words (read-file "words.linux")))
  148.     (time
  149.      (loop for w in words
  150.           always (check w)))))
  151.  
  152. (defun test2 ()
  153.   (let ((words (read-file "words.linux"))
  154.         (d (make-hash-table :test 'equal)))
  155.     (loop for w in words
  156.           do (setf (gethash w d) t))
  157.     (time
  158.      (loop for w in words
  159.            always (gethash w d)))))
  160.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement