Skip to content

Commit

Permalink
document sample literals
Browse files Browse the repository at this point in the history
  • Loading branch information
hiredman committed Mar 24, 2013
1 parent 2867af5 commit d3bd025
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/data_readers.clj
@@ -0,0 +1 @@
{opennlp/sample opennlp.sample/read-document-sample}
46 changes: 46 additions & 0 deletions src/opennlp/sample.clj
@@ -0,0 +1,46 @@
(ns opennlp.sample
(:require [clojure.java.io :as io])
(:import (opennlp.tools.doccat DocumentSample)
(opennlp.tools.util ObjectStream)))

(defn print-sample [sample ^java.io.Writer w]
(.write w "#opennlp/sample {")
(.write w ":category ")
(binding [*out* w]
(prn (.getCategory sample)))
(.write w " :text ")
(binding [*out* w]
(prn (vec (.getText sample))))
(.write w "}"))

(defmethod print-method DocumentSample
[sample w]
(print-sample sample w))

(defmethod print-dup DocumentSample
[sample w]
(print-sample sample w))

(defn read-document-sample [{:keys [category text]}]
(DocumentSample. category (into-array String text)))

(defn clojure-document-sample-stream [in]
(let [i (java.io.PushbackReader. (io/reader in))
buf (atom [])
pos (atom 0)]
(reify
ObjectStream
(read [_]
(if (= @pos (count @buf))
(when-let [obj (read i false nil)]
(swap! buf conj obj)
(swap! pos inc)
obj)
(let [p @pos]
(swap! pos inc)
(nth @buf p))))
(close [_]
(.close i)
(.close in))
(reset [_]
(reset! pos 0)))))
24 changes: 24 additions & 0 deletions test/opennlp/test/sample.clj
@@ -0,0 +1,24 @@
(ns opennlp.test.sample
(:require [clojure.test :refer :all]
[opennlp.sample :refer [clojure-document-sample-stream]])
(:import (opennlp.tools.doccat DocumentSample)))

(deftest test-samples-round-trip
(let [d #opennlp/sample {:category "foo" :text ["bar"]}]
(is (= d (read-string (pr-str d))))))

(deftest test-clojure-document-sample-stream
(let [d #opennlp/sample {:category "foo" :text ["bar"]}
x (java.io.ByteArrayInputStream.
(.getBytes
(with-out-str
(prn d)
(prn d))))
s (clojure-document-sample-stream x)]
(is (= (.read s) d))
(is (= (.read s) d))
(is (nil? (.read s)))
(.reset s)
(is (= (.read s) d))
(is (= (.read s) d))
(is (nil? (.read s)))))

0 comments on commit d3bd025

Please sign in to comment.