This repository has been archived by the owner on Mar 30, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 188
/
Copy pathtok-file.lisp
187 lines (164 loc) · 7.22 KB
/
tok-file.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
;;;; Copyright 2018 Reddit, Inc.
;;;;
;;;; Permission is hereby granted, free of charge, to any person obtaining a copy of
;;;; this software and associated documentation files (the "Software"), to deal in
;;;; the Software without restriction, including without limitation the rights to
;;;; use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
;;;; of the Software, and to permit persons to whom the Software is furnished to do
;;;; so, subject to the following conditions:
;;;;
;;;; The above copyright notice and this permission notice shall be included in all
;;;; copies or substantial portions of the Software.
;;;;
;;;; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
;;;; IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
;;;; FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
;;;; AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
;;;; LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
;;;; OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
;;;; SOFTWARE.
(in-package :reddit)
;;TODO - write a pathname macro such that it'll define these functions
;;with an &optional parameter for the pathname
;;TODO - lock resources that will be accessed by multiple threads -
;;user data and site data
(defparameter *base-dir* #p"/home/reddit/data/")
(defparameter *max-users* 2)
(defparameter *max-sites* 100)
(defvar *site-tokens* (make-hash-table))
(defvar *user-tokens* (make-hash-table))
(defun site-pathname (articleid)
"Returns the pathname for a site datafile."
(merge-pathnames (make-pathname :name (format nil "site~a" articleid))
*base-dir*))
(defun user-pathname (userid type)
"Returns the pathname for userid's file. Type should be :good
or :bad."
(merge-pathnames (make-pathname :name (format nil "user~a~a" userid
(case type
(:good "g")
(:bad "b")
(t (error "good or bad" )))))
*base-dir*))
(defun write-site-tokens (articleid tokens)
"Writes the list of tokens to a file."
(with-open-file (out (site-pathname articleid)
:direction :output
:if-exists :supersede
:if-does-not-exist :create)
(when out
(print tokens out))))
(defun read-site-tokens (articleid)
"Reads the list of tokens from a file."
(with-open-file (in (site-pathname articleid)
:direction :input
:if-does-not-exist nil)
(when in
(read in))))
(defun download-site (id url)
(ignore-errors
(write-site-tokens id (tokens-url url))))
(defun write-hash-table (path ht)
"Writes ht to path with a newline between keys and values."
(with-open-file (out path
:direction :output
:if-exists :supersede
:if-does-not-exist :create)
(when out
(loop for key being the hash-keys in ht using (hash-value val) do
(print key out)
(print val out)))))
(defun read-hash-table (path)
"Reads alternating keys and vals from path and returns a
hashtable."
(with-open-file (in path
:direction :input
:if-does-not-exist nil)
(when in
(let ((ht (make-hash-table)))
(loop
for key = (read in nil nil)
for val = (read in nil nil)
while (and key val) do
(setf (gethash key ht) val))
ht))))
(defun write-user-tokens (userid goodht badht)
"Writes the user's good/bad tokens to their respective files."
(write-hash-table (user-pathname userid :good) goodht)
(write-hash-table (user-pathname userid :bad) badht))
(defun read-user-tokens (userid)
"Reads a user's good/bad tokens from their respective files."
(list
(or (read-hash-table (user-pathname userid :good)) (make-hash-table))
(or (read-hash-table (user-pathname userid :bad)) (make-hash-table))))
;;TODO functions to write for a specific user, or write all users
(defmacro max-size-hash (name maxsize read-fn &optional write-fn)
"Defines a closure keeps a hashtable under a max size,
populating it with read-fn when required."
`(let ((max ,maxsize)
(fn ,read-fn)
(wfn ,write-fn)
(viewed nil)
(ht (make-hash-table)))
(defun ,name (key)
(multiple-value-bind (data exists) (or (gethash key ht) (funcall fn key) )
(when data
(setf viewed (cons key (remove key viewed)))
(when (> (length viewed) max)
(let ((l (car (last viewed))))
;;write user data
(when wfn
(let ((udata (gethash l ht)))
(funcall wfn l (first udata) (second udata))))
(remhash l ht)
(setf viewed (remove l viewed :from-end t))))
(unless exists
(setf (gethash key ht) data))
data)))))
(max-size-hash user-data 10 #'read-user-tokens #'write-user-tokens)
(max-size-hash site-data 100 #'read-site-tokens)
(defun flag-site (userid articleid goodbad)
"Adds a site's tokens to a user's good/bad table."
(let ((userht (case goodbad
(:good (first (user-data userid)))
(:bad (second (user-data userid)))
(t (error "good or bad"))))
(sdata (site-data articleid)))
(loop for token in sdata do
(setf (gethash token userht) (1+ (gethash token userht 0))))))
(defun token-prob (token goodht badht)
"The probability that a token is good. .01 == max bad, .99 ==
max good."
(let ((g (gethash token goodht 0))
(b (gethash token badht 0))
(gsize (max 1 (hash-table-count goodht)))
(bsize (max 1 (hash-table-count badht))))
(if (>= (+ g b) 5)
(max .01
(min .99 (float (/ (min 1 (/ g gsize))
(+ (min 1 (/ g gsize))
(min 1 (/ b bsize)))))))
.5)))
(defun most-interesting (tokens goodht badht)
"Returns a list of the 15 most interesting (token . prob) pairs
where interesting is the distance from .5 of prob."
(let ((probs (sort
(mapcar #'(lambda (token)
(cons token (token-prob token goodht badht)))
tokens)
#'> :key #'(lambda (x) (abs (- .5 (cdr x)))))))
(subseq probs 0 (min 15 (length probs)))))
(defun combine-probs (probs)
"Returns the combination of probabilities in prob."
(max .01
(min .99
(let ((prod (apply #'* probs)))
(/ prod (+ prod (apply #'* (mapcar #'(lambda (x)
(- 1 x))
probs))))))))
(defun site-prob (userid articleid)
"Returns the probability that userid thinks articleid is good."
(let ((sdata (site-data articleid))
(udata (user-data userid)))
(combine-probs (mapcar #'(lambda (x) (cdr x))
(most-interesting sdata (first udata) (second udata))))))