-
Notifications
You must be signed in to change notification settings - Fork 5
/
utils.lisp
87 lines (71 loc) · 2.3 KB
/
utils.lisp
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
(in-package :cl-conllu)
;; alternatives
;; http://stackoverflow.com/questions/3672880/more-generic-lisp-code-to-generate-combinations-of-pairs
;; http://stackoverflow.com/questions/28504467/combining-list-of-list
;; https://groups.google.com/forum/#!topic/comp.lang.lisp/NrMe2NmcCLU
;; https://goo.gl/KjclLT
(defun mappend (function list)
"Receives a function and a list of lists and returns the appended
result of the aplication of the function to each list."
(apply #'append (mapcar function list)))
(defun range (a b)
(assert (and (integerp a) (integerp b)))
(cond ((= a b) (list a))
((< a b)
(cons a (range (+ a 1) b)))
((> a b)
(cons a (range (- a 1) b)))))
(defun levenshtein (s1 s2 &key (test #'string-equal))
(let* ((width (1+ (length s1)))
(height (1+ (length s2)))
(d (make-array (list height width))))
(dotimes (x width)
(setf (aref d 0 x) x))
(dotimes (y height)
(setf (aref d y 0) y))
(dotimes (x (length s1))
(dotimes (y (length s2))
(setf (aref d (1+ y) (1+ x))
(min (1+ (aref d y (1+ x)))
(1+ (aref d (1+ y) x))
(+ (aref d y x)
(if (funcall test (aref s1 x) (aref s2 y))
0
1))))))
(aref d (1- height) (1- width))))
(defun diff (sentences-a sentences-b &key test key)
(let* ((dim (list (length sentences-a)
(length sentences-b)))
(distances (make-array dim :initial-element -1)))
(dotimes (line (car dim) distances)
(format t "Computing line [~a]~%" line)
(dotimes (column (cadr dim))
(setf (aref distances line column)
(funcall test
(funcall key (nth line sentences-a))
(funcall key (nth column sentences-b))))))))
(defun print-diff (matriz)
(mapcar (lambda (line)
(cons (sentence-meta (car line))
(mapcar (lambda (pair)
(list (sentence-meta (car pair))
(cadr pair)))
(cdr line))))
matriz))
(defun find-min (list &key (min nil))
(cond
((null list)
min)
((or (null min)
(< (caddr (car list))
(caddr min)))
(find-min (cdr list) :min (car list)))
(t (find-min (cdr list) :min min))))
(defun insert-at (lst index newelt)
(if (equal index 0)
(cons newelt lst)
(progn
(push newelt (cdr (nthcdr (1- index) lst)))
lst)))
(defun blank-line? (line)
(cl-ppcre:scan "^[\t ]*$" line))