@Override public String toString() { if (m_model == null) { return "No model built yet"; } if (m_namespace == null || m_namespace.length() == 0) { return "No namspace for Clojure classifier specified."; } ClassLoader prevCL = Thread.currentThread().getContextClassLoader(); Thread.currentThread().setContextClassLoader(ClojureClassifier.class.getClassLoader()); if (!m_clojureRequire) { Clojure.require(m_namespace); m_clojureRequire = true; } if (Clojure.eval("(resolve (symbol \"" + m_namespace + "/model-to-string\"))") != null) { IFn stringFn = (IFn) Clojure.eval(m_namespace + "/model-to-string"); String result = stringFn.invoke(m_model, m_header).toString(); Thread.currentThread().setContextClassLoader(prevCL); return result; } // just return the default if the clojure classifier does not have // a model-to-string function return m_model.toString(); }
@Override public double[] distributionForInstance(Instance inst) throws Exception { if (m_model == null) { throw new Exception("No model built yet!"); } if (m_namespace == null || m_namespace.length() == 0) { throw new Exception("No namespace for Clojure classifier specified!"); } ClassLoader prevCL = Thread.currentThread().getContextClassLoader(); Thread.currentThread().setContextClassLoader(ClojureClassifier.class.getClassLoader()); if (!m_clojureRequire) { Clojure.require(m_namespace); m_clojureRequire = true; } if (m_distributionForInstanceFunction == null) { if (Clojure.eval("(resolve (symbol \"" + m_namespace + "/distribution-for-instance\"))") == null) { throw new Exception( "Namespace " + m_namespace + " does not have a distribution-for-instance function!"); } m_distributionForInstanceFunction = (IFn) Clojure.eval(m_namespace + "/distribution-for-instance"); } Object result = m_distributionForInstanceFunction.invoke(inst, m_model); if (!(result instanceof double[])) { throw new Exception("distribution-for-instance function should return an array of doubles"); } Thread.currentThread().setContextClassLoader(prevCL); return (double[]) result; }
@Override public void buildClassifier(Instances insts) throws Exception { if (m_namespace == null || m_namespace.length() == 0) { throw new Exception("No namespace for Clojure classifier specified!"); } m_header = new Instances(insts, 0); ClassLoader prevCL = Thread.currentThread().getContextClassLoader(); Thread.currentThread().setContextClassLoader(ClojureClassifier.class.getClassLoader()); Clojure.require(m_namespace); m_clojureRequire = true; if (Clojure.eval("(resolve (symbol \"" + m_namespace + "/learn-classifier\"))") == null) { throw new Exception( "Namespace " + m_namespace + " does not have a learn-classifier function!"); } IFn learnFn = (IFn) Clojure.eval(m_namespace + "/learn-classifier"); m_model = learnFn.invoke(insts, m_schemeOptions); Thread.currentThread().setContextClassLoader(prevCL); }