~bendersteed/markov-chains-web-navigation

markov-chains-web-navigation/markov-chain-navigation.lisp -rw-r--r-- 1.8 KiB
b0516676 — Dimos Dimakakos Docs: update README 1 year, 11 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
(quicklisp:quickload :cl-ppcre)

(defun get-file (filename)
  (with-open-file (stream filename)
    (loop for line = (read-line stream nil)
       while line
       collect line)))

(defparameter paths
  (mapcar
   (lambda (x) (ppcre:split " " (string-right-trim '(#\^M) x)))
   (remove-if-not (lambda (x) (> (length x) 3)) (get-file "msnbc990928.seq"))))

(defparameter test
  (mapcar
   (lambda (x) (ppcre:split " " (string-right-trim '(#\^M) x)))
   (remove-if-not (lambda (x) (> (length x) 3)) (get-file "test-data/test_case_1"))))

(defun add-reset (path k states)
  (let ((front (loop :for i :from 1 :upto k
                     :collect (write-to-string (1+ states)))))
    (append front path (list (write-to-string(1+ states))))))

(defun fit-paths (paths k states)
  (let ((result (make-hash-table :test #'equal)))
    (loop :for i :in paths
          :do (let ((path (add-reset i k states)))
                (loop :for j :from k :upto (- (length path) 2)
                      :do (print (list (subseq path (- j k) j) (nth (1+ j) path)))
                      :do (if (gethash (subseq path (- j k) j) result)
                              (if (gethash (nth (1+ j) path) (gethash (subseq path (- j k) j) result))
                                  (setf (gethash (nth (1+ j) path) (gethash (subseq path (- j k) j) result))
                                        (1+ (gethash (nth (1+ j) path) (gethash (subseq path (- j k) j) result))))
                                  (setf (gethash (nth (1+ j) path) (gethash (subseq path (- j k) j) result))
                                        1))
                              (progn (setf (gethash (subseq path (- j k) j) result) (make-hash-table :test #'equal))
                               (setf (gethash (nth (1+ j) path) (gethash (subseq path (- j k) j) result))
                                     1))))))
    result))