Permalink
Browse files

Split off the example code from the aq-algorithm code.

  • Loading branch information...
1 parent 6bfb5da commit 7a334eed4b8fb2adacbdb9d90567f69ef3e15ee5 @arthurschreiber committed Apr 12, 2011
View
18 clojure/example/example.clj
@@ -0,0 +1,18 @@
+(ns example
+ (:use [clojure.string :only (split)])
+ (:use [clojure.java.io :only (reader)]))
+(load "src/aq_algorithm")
+
+(let [ examples (map (fn [line] (split line #";")) (rest (line-seq (reader "example/input.csv"))))
+ vs (version-space/gen-version-space (count (first examples)))
+ groups (group-by (fn [example] (last example)) examples) ]
+
+ (doseq [key (keys groups)]
+ (let [ positives (aq-algorithm/get-positive-examples groups key)
+ negatives (aq-algorithm/get-negative-examples groups key) ]
+
+ (println "K für" key)
+ (doseq [k (aq-algorithm/gen-concept-space positives negatives)] (println k))
+ )
+ )
+)
View
0 clojure/input.csv → clojure/example/input.csv
File renamed without changes.
View
43 clojure/src/example.clj → clojure/src/aq_algorithm.clj
@@ -1,21 +1,20 @@
-(ns example
- (:use [clojure.java.io :only (reader)])
- (:use [clojure.string :only (split)])
+(ns aq-algorithm
+ (:require clojure.contrib.greatest-least)
+ (:use [clojure.contrib.greatest-least :only (least-by)])
)
(load "src/version_space")
+(defn normalize-example [example]
+ (rest (butlast example))
+)
(defn get-positive-examples [groups key]
- (map (fn [example]
- (rest (butlast example))
- ) (get groups key))
+ (map normalize-example (get groups key))
)
(defn get-negative-examples [groups key]
- (map (fn [example]
- (rest (butlast example))
- ) (reduce concat '() (vals (dissoc groups key))))
+ (map normalize-example (reduce concat '() (vals (dissoc groups key))))
)
(defn gen-star [pos_example negatives]
@@ -29,9 +28,17 @@
)
)
+(defn select-best-generalization [gen_star]
+ ; (first gen_star)
+ (apply least-by (fn [g]
+ (reduce (fn [acc item]
+ (if (= item :*) (+ acc 1) acc)
+ ) 0 g)
+ ) gen_star)
+)
+
(defn gen-best-generalization [pos_example negatives]
- ; TODO "bessere" generalisierung wählen
- (first (gen-star pos_example negatives))
+ (select-best-generalization (gen-star pos_example negatives))
)
(defn gen-concept-space-helper [positives negatives k]
@@ -49,18 +56,4 @@
(defn gen-concept-space [positives negatives]
(gen-concept-space-helper positives negatives '())
-)
-
-(let [ examples (map (fn [line] (split line #";")) (rest (line-seq (reader "input.csv"))))
- vs (version-space/gen-version-space (count (first examples)))
- groups (group-by (fn [example] (last example)) examples) ]
-
- (doseq [key (keys groups)]
- (let [ positives (get-positive-examples groups key)
- negatives (get-negative-examples groups key) ]
-
- (println "K für" key)
- (doseq [k (gen-concept-space positives negatives)] (println k))
- )
- )
)
View
16 clojure/test/aq_algorithm_test.clj
@@ -0,0 +1,16 @@
+(ns aq-algorithm-test (:use [clojure.test]))
+
+(load "src/aq_algorithm")
+
+(deftest test-select-best-generalization
+ (testing "should select the least general generalization"
+ (is
+ (=
+ (aq-algorithm/select-best-generalization '(("a" :* :*) ("b" :* :*) ("a" "b" :*)))
+ '("a" "b" :*)
+ )
+ )
+ )
+)
+
+(run-tests)

0 comments on commit 7a334ee

Please sign in to comment.