-
Notifications
You must be signed in to change notification settings - Fork 2
/
solr.lisp
147 lines (135 loc) · 5.88 KB
/
solr.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
(in-package :wordnet)
(defparameter *solr* (make-instance 'solr:solr :uri "http://localhost:8983/solr/wn"))
(defparameter *solr-pointers* (make-instance 'solr:solr :uri "http://localhost:8983/solr/pointers"))
(defparameter *debug-load-pointers* nil)
(defun synset-to-alist (addr &key (suffix "en") (data nil) (plan nil))
(labels ((tostr (apart regex replacement)
(multiple-value-bind (val type extra)
(upi->value apart)
(declare (ignore val extra))
(if (equal 0 type)
(cl-ppcre:regex-replace regex (part->string apart :format :concise) replacement)
(cl-ppcre:regex-replace regex (part->value apart) replacement))))
(adj-name (name)
(cond
((cl-ppcre:scan "^word" name)
(intern (format nil "~a_~a" name suffix) :keyword))
((cl-ppcre:scan "example" name)
(intern (format nil "example_~a" suffix) :keyword))
((cl-ppcre:scan "gloss" name)
(intern (format nil "gloss_~a" suffix) :keyword))
(t (intern name :keyword)))))
(let* ((synset (resource (concatenate 'string "synset-" addr)
(concatenate 'string "wn30" suffix)))
(words (mapcar (lambda (w) (part->value (car w)))
(sparql:run-sparql plan
:with-variables `((?synset . ,synset))
:engine :sparql-1.1 :results-format :lists)))
(query (get-triples :s synset)))
(do ((res (or data (list (cons :id addr))))
(a-triple (cursor-next-row query)
(cursor-next-row query)))
((null a-triple)
(if words (push (cons (adj-name "word") words) res))
(push (cons (adj-name "word_count") (length words)) res)
res)
(cond
((part= (predicate a-triple) !owl:sameAs)
(setf res (synset-to-alist addr :suffix "pt" :data res :plan plan)))
((not (member (predicate a-triple) (list !skos:inScheme !wn30:containsWordSense) :test #'part=))
(let ((key (adj-name (tostr (predicate a-triple) ":" "_")))
(val (tostr (object a-triple) "^wn30(en|br)?:(synset-)?" "")))
(push (cons key val) res))))))))
(defun nomlex-to-alist (addr)
(labels ((tostr (apart regex replacement)
(multiple-value-bind (val type extra)
(upi->value apart)
(declare (ignore val extra))
(if (equal 0 type)
(cl-ppcre:regex-replace regex (part->string apart :format :concise) replacement)
(cl-ppcre:regex-replace regex (part->value apart) replacement)))))
(let ((query (get-triples :s addr)))
(do ((res (list (cons :id (part->string addr :format :concise))))
(a-triple (cursor-next-row query)
(cursor-next-row query)))
((null a-triple)
res)
(let ((key (intern (tostr (predicate a-triple) ":" "_") :keyword)))
(if (member (predicate a-triple) '(!nomlex:noun !nomlex:verb) :test #'part=)
(let ((val (object (get-triple :s (object a-triple) :p !wn30:lexicalForm))))
(push (cons key (part->value val)) res))
(push (cons key (tostr (object a-triple) "^nomlex:" "")) res)))))))
(defun load-nomlex-solr (blocksize)
(let* ((current 0)
(total 0)
(block-tmp nil)
(query (get-triples :p !rdf:type :o !nomlex:Nominalization)))
(do* ((a-triple (cursor-next-row query)
(cursor-next-row query)))
((null a-triple)
(solr:solr-add* *solr* block-tmp :commit t))
(format *debug-io* "Processing ~a [~a/~a ~a]~%"
(part->string (subject a-triple)) current blocksize total)
(push (remove-duplicates (nomlex-to-alist (subject a-triple)) :test #'equal)
block-tmp)
(setf current (1+ current))
(if (> current blocksize)
(progn
(solr:solr-add* *solr* block-tmp :commit t)
(setf total (+ total current)
current 0
block-tmp nil))))))
(defun load-synsets-solr (blocksize)
(let* ((current 0)
(total 0)
(block-tmp nil)
(plan-words (sparql:parse-sparql (query-string "synset-words.sparql")))
(synsets (sparql:run-sparql (sparql:parse-sparql (query-string "all-synsets.sparql"))
:results-format :lists)))
(dolist (p synsets)
(let ((id (cl-ppcre:regex-replace "^wn30en:synset-"
(part->string (car p) :format :concise) "")))
(format *debug-io* "Processing ~a [~a/~a ~a]~%"
id current blocksize total)
(push (remove-duplicates (synset-to-alist id :plan plan-words) :test #'equal) block-tmp)
(setf current (1+ current))
(if (> current blocksize)
(progn
(solr:solr-add* *solr* block-tmp :commit t)
(setf total (+ total current)
current 0
block-tmp nil)))))
(solr:solr-add* *solr* block-tmp :commit t)))
(defun link-to-alist (params &key type)
(case type
(:synset
(pairlis '(:source_synset :pointer :target_synset)
(mapcar #'part->value params)))
(:wordsense
(pairlis '(:source_synset :source_word :pointer :target_synset :target_word)
(mapcar #'part->value params)))
(:nomlex
(pairlis '(:source_word :pointer :target_word)
(mapcar #'part->value params)))))
(defun load-pointers (blocksize)
(let* ((current 0)
(total 0)
(block-tmp nil)
(queries (pairlis '(:synset :wordsense :nomlex)
'("all-synset-links.sparql" "all-wordsense-links.sparql" "all-nomlex-links.sparql"))))
(dolist (q queries)
(let ((result (sparql:run-sparql
(sparql:parse-sparql (query-string (cdr q))
(alexandria:alist-hash-table (collect-namespaces)))
:results-format :lists)))
(dolist (l result)
(when *debug-load-pointers* (format *debug-io* "Processing (~{~a~^,~}) [~a/~a ~a]~%" l current blocksize total))
(push (link-to-alist l :type (car q)) block-tmp)
(setf current (1+ current))
(if (> current blocksize)
(progn
(solr:solr-add* *solr-pointers* block-tmp :commit t)
(setf total (+ total current)
current 0
block-tmp nil))))
(solr:solr-add* *solr-pointers* block-tmp :commit t)))))