-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path6.lisp
52 lines (43 loc) · 1.62 KB
/
6.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
;; 50
(ql:quickload :cl-ppcre)
(ql:quickload :split-sequence)
(use-package :split-sequence)
(defun read-file (file-name &optional (delimiter '(#\newline)))
(let ((ret "")
(next-line (coerce delimiter 'string)))
(with-open-file (in file-name)
(do ((line (read-line in nil nil) (read-line in nil nil)))
((null line) ret)
(setf ret (concatenate 'string ret line next-line))))))
(defparameter *nlp* (read-file "resource/nlp.txt"))
;; Like Automaton
(defun split-to-sentence (strings)
(labels ((next-capital (chars tmp acc)
(if chars
(if (upper-case-p (car chars))
(find-end-char chars nil (cons (reverse (cddr tmp)) acc))
(find-end-char (cdr chars) (cons (car chars) tmp) acc))
(cons (reverse (cdr tmp)) acc)))
(next-space (chars tmp acc)
(if chars
(if (char= (car chars) #\space)
(next-capital (cdr chars) (cons (car chars) tmp) acc)
(find-end-char (cdr chars) (cons (car chars) tmp) acc))
(cons (reverse tmp) acc)))
(find-end-char (chars tmp acc)
(if chars
(if (member (car chars) '(#\. #\; #\: #\? #\!))
(next-space (cdr chars) (cons (car chars) tmp) acc)
(find-end-char (cdr chars) (cons (car chars) tmp) acc))
(cons (reverse tmp) acc))))
(mapcar (lambda (x) (substitute #\space #\newline x))
(mapcar (lambda (x) (coerce x 'string))
(reverse (find-end-char (coerce strings 'list) nil nil))))))
(defparameter *sentences* (split-to-sentence *nlp*))
;; 51
(defun split-to-word (sentence)
(split-sequence #\space sentence))
(defparameter *words* (mapcar #'split-to-word *sentences*))
;; 52
;; I can't find lisp implementation.
;; 53