Database Reference
In-Depth Information
How to do it…
For this recipe, we'll irst deine a function and macro that will greatly facilitate writing wrapper
functions around Weka classes and processes. Then we'll use that macro for the irst time to
wrap the weka.clusters.SimpleKMeans class:
1. First, we'll deine a function to generate a random seed from the current time.
By default, Weka always uses 1 as the random seed. This function will allow us to
use the current time by specifying nil as the seed:
(defn random-seed [seed]
(if (nil? seed)
(.intValue (.getTime (java.util.Date.)))
seed))
2.
Most of the Weka analyses follows the same pattern. We'll take keyword parameters
and turn them into a command-line-style string array. We'll create a Weka object and
pass it the options array. This is the kind of boilerplate that the Lisp and Lisp macros
are particularly good at abstracting away.
The most complicated part of this will involve parsing a sequence of vectors into
the wrapper function's parameter list and the options array. Each vector will list
the option, a variable name for it, and a default value. Optionally, they may also
contain a keyword indicating how the value is converted to an option and under
what circumstances it's included. The highlighted comments in the code show
some examples of these near the place where they're passed. Here's the function
to parse an options vector into the code to pass to ->options :
(defn analysis-parameter [parameter]
(condp = (count parameter)
;; [option-str variable-name default-value]
;; ["-N" k 2]
3 '[~(first parameter) ~(second parameter)]
;; [option-str variable-name default-value flag]
;; ["-V" verbose false :flag-true]
4 (condp = (last parameter)
:flag-true '[(when ~(second parameter)
~(first parameter))]
:flag-false '[(when-not ~(second parameter)
~(first parameter))]
:not-nil '[(when-not
(nil? ~(second parameter))
[~(first parameter)
 
Search WWH ::




Custom Search