forked from own-pt/cl-wnbrowser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
templates.lisp
226 lines (194 loc) · 8.21 KB
/
templates.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
;; -*- mode: common-lisp -*-
;; Copyright (c) 2015,2016 The OpenWordNet-PT project
;; This program and the accompanying materials are made available
;; under the terms described in the LICENSE file.
(in-package :cl-wnbrowser)
(defun pretty-print-iso-date (ms-since-epoch)
(smart-date
(local-time:timestamp-to-universal
(local-time:unix-to-timestamp (floor ms-since-epoch 1000)))))
;;; from https://github.com/smanek/common-lisp-utils/blob/master/date.lisp
(defun smart-date (date)
(let ((diff (- (get-universal-time) date)))
(cond ((< diff -604800) (format nil "~A week~:p from now" (floor (/ diff -604800))))
((< diff -86400) (format nil "~A day~:p from now" (floor (/ diff -86400))))
((< diff -3600) (format nil "~A hour~:p from now" (floor (/ diff -3600))))
((< diff -60) (format nil "~A minute~:p from now" (floor (/ diff -60))))
((< diff 0) (format nil "~A second~:p from now" (- 0 diff)))
((< diff 60) (format nil "~A second~:p ago" diff))
((< diff 3600) (format nil "~A minute~:p ago" (floor (/ diff 60))))
((< diff 86400) (format nil "~A hour~:p ago" (floor (/ diff 3600))))
(t (format nil "~A day~:p ago" (floor (/ diff 86400)))))))
(defun trim-comment(s &optional (max-length 68))
(if (< (length s) max-length)
s
(format nil "~a (...)" (subseq s 0 max-length))))
(defun get-vote-id (user votes)
(getf (car (member user votes :test #'(lambda (a b)
(equal (getf b :|user|) a)))) :|id|))
(defun already-voted (user votes)
(member user votes :test #'(lambda (a b)
(equal (getf b :|user|) a))))
(defun nice-action (action)
(cond ((string-equal "add-gloss-pt" action) "+gl")
((string-equal "remove-gloss-pt" action) "-gl")
((string-equal "add-word-pt" action) "+w")
((string-equal "remove-word-pt" action) "-w")
((string-equal "add-example-pt" action) "+ex")
((string-equal "remove-example-pt" action) "-ex")
(t "??")))
(defun is-self (logged user)
(if (and logged user)
(string-equal logged user)
nil))
(defun is-authorized-to-approve/reject (user)
(member user *approve-reject-authorized-accounts* :test #'string-equal))
(defun is-authorized-to-vote (user)
(member user *vote-authorized-accounts* :test #'string-equal))
(defun valid-status (status)
(not (string-equal status "committed")))
(defun get-doc-id (doc)
(getf doc :|id|))
(defun is-member (term list)
(if (member term list :test #'string-equal)
t
nil))
(defun checked (term list &key (test #'string=))
"Helper function to be called from the TEMPLATE code. It returns
CHECKED in case TERM belongs to LISP; nil otherwise. This is useful
in dealing with checkboxes."
(if (member term list :test test)
"checked"
nil))
(defun is-array (obj)
(listp obj))
;;; 1 is for nouns, 2 for verbs, 3 for adjectives and 4 for adverbs.
;;; 00449295-n
;;; 0123456789
(defun synset-id-to-sumo (synset-id)
(when synset-id
(let ((id (subseq synset-id 0 8))
(type (subseq synset-id 9 10))
(sumo-type "?"))
(cond ((equal "n" type) (setq sumo-type "1"))
((equal "v" type) (setq sumo-type "2"))
((equal "a" type) (setq sumo-type "3"))
((equal "r" type) (setq sumo-type "4")))
(format nil "~a~a" sumo-type id))))
(defun extract-links (txt index userid)
(let ((tmpl "<a target=\"sense-tagging-details\" href=\"sense-tagging-details?file=bosque.json&text=~a&word=~a&userid=~a\">~a</a>")
(offsets (mapcar (lambda (w) (getf w :|offset|))
(getf txt :|words|)))
(tags (mapcar (lambda (w) (getf w :|tag|))
(getf txt :|words|))))
(labels ((convert (txt offsets tags idx ref out)
(if (null offsets)
(format nil "~{~a~}" (reverse (cons txt out)))
(let* ((offset (car offsets))
(tag (car tags))
(start (- (car offset) ref))
(end (- (cadr offset) ref))
(lnk
(if (alexandria:starts-with-subseq "NC" tag)
(format nil tmpl index idx userid (subseq txt start end))
(subseq txt start end))))
(convert (subseq txt end)
(cdr offsets)
(cdr tags)
(1+ idx)
(cadr offset)
(cons lnk (cons (subseq txt 0 start) out)))))))
(convert (getf txt :|text|) offsets tags 0 0 nil))))
(defun in-suggestions (entry suggestions action)
(let ((trimmed-entry (trim entry))
(entries
(mapcan (lambda (s)
(when
(and
(not (string-equal "not-accepted" (getf s :|status|)))
(string-equal action (getf s :|action|)))
(list (trim (getf s :|params|)))))
suggestions)))
(member trimmed-entry entries :test #'string=)))
(defun convert-tag (str)
"Converts from the format stored in SOLR to a more human readable version."
(cond ((starts-with-subseq "HASH" str :test #'string=)
(format nil "#~a" (subseq str 4)))
((starts-with-subseq "AT" str :test #'string=)
(format nil "@~a" (subseq str 2)))
(t str)))
(defun get-sense-synset (s)
(car (split-sequence #\: s)))
(defun get-sense-weight (s)
(cadr (split-sequence #\: s)))
(defun is-synset-rel (rels)
(remove-if (lambda (x) (getf x :|source_word|))
rels))
(defparameter *rel-symbols* (alexandria:plist-hash-table '(:|wn30_en_antonymOf| "ant"
:|wn30_en_causes| "cause"
:|wn30_en_classifiedByRegion| "mr"
:|wn30_en_classifiedByTopic| "mt"
:|wn30_en_classifiedByUsage| "mu"
:|wn30_en_classifiesByRegion| "dr"
:|wn30_en_classifiesByTopic| "dt"
:|wn30_en_classifiesByUsage| "du"
:|wn30_en_derivationallyRelated| "drf"
:|wn30_en_participleOf| "pv"
:|wn30_en_pertainsTo| "pe"
:|wn30_en_property| "prp"
:|wn30_en_sameVerbGroupAs| "vg"
:|wn30_en_seeAlso| "see") :test #'equal))
(defun aux-filter-sense-rel (rel links word)
(loop for link in links
when (equal (getf link :|source_word|) word)
collect (cons :|symbol|
(cons (gethash rel *rel-symbols*)
link))))
(defun filter-sense-rel (synset word)
(let ((relations '(:|wn30_en_classifiedByRegion| :|wn30_en_classifiedByTopic|
:|wn30_en_classifiedByUsage| :|wn30_en_classifiesByRegion|
:|wn30_en_classifiesByTopic| :|wn30_en_classifiesByUsage|
:|wn30_en_sameVerbGroupAs| :|wn30_en_seeAlso|
:|wn30_en_derivationallyRelated| :|wn30_en_antonymOf|
:|wn30_en_pertainsTo| :|wn30_en_property|
:|wn30_en_participleOf| :|wn30_en_causes|)))
(loop for rel in relations
append (aux-filter-sense-rel rel (getf synset rel) word))))
(defun setup-templates ()
(closure-template:with-user-functions
(("issynset" #'is-synset)
("niceaction" #'nice-action)
("alreadyvoted" #'already-voted)
("sensesynset" #'get-sense-synset)
("senseweight" #'get-sense-weight)
("getvoteid" #'get-vote-id)
("insuggestions" #'in-suggestions)
("extractlinks" #'extract-links)
("urlencode" #'hunchentoot:url-encode)
("synsetidtosumo" #'synset-id-to-sumo)
("trimcomment" #'trim-comment)
("getdocid" #'get-doc-id)
("converttag" #'convert-tag)
("validstatus" #'valid-status)
("isauthorizedapprove" #'is-authorized-to-approve/reject)
("isauthorizedvote" #'is-authorized-to-vote)
("isself" #'is-self)
("prettydate" #'pretty-print-iso-date)
("checked" #'checked)
("nchecked" (lambda (term list) (checked term list :test #'eq)))
("isarray" #'is-array)
("ismember" #'is-member)
("synsetworden" #'get-synset-word-en)
("synsetword" #'get-synset-word)
("synsetgloss" #'get-synset-gloss)
("synsetrel" #'is-synset-rel)
("wordrel" #'filter-sense-rel))
(walk-directory
(merge-pathnames *templates-directory* *basedir*)
(lambda (f)
(closure-template:compile-template
:common-lisp-backend
(read-file-into-string f)))
:test (lambda (f)
(alexandria:ends-with-subseq ".tmpl" (namestring f))))))
(setup-templates)