Skip to content

Instantly share code, notes, and snippets.

View raek's full-sized avatar

Rasmus Bondesson raek

View GitHub Profile
(ns my-file-utils
(:import java.io.File))
;; Java methods cannot be used as Clojure functions,
;; so you have to wrap them in functions.
;; anonymous function, map, and filter version
(defn list-files [d]
(map (fn [f] (.getName f))
(filter (fn [f] (.isDirectory f))
@raek
raek / gist:1165399
Created August 23, 2011 15:16 — forked from Netpilgrim/gist:1165353
Analyzing Android Pattern Lock
(def pattern_graph
[[0 1 0 1 1 1 0 1 0]
[1 0 1 1 1 1 1 0 1]
[0 1 0 1 1 1 0 1 0]
[1 1 1 0 1 0 1 1 1]
[1 1 1 1 0 1 1 1 1]
[1 1 1 0 1 0 1 1 1]
[0 1 0 1 1 1 0 1 0]
[1 0 1 1 1 1 1 0 1]
[0 1 0 1 1 1 0 1 0]])
@raek
raek / tf-idf.clj
Created June 28, 2011 09:51 — forked from ithayer/tf-idf.clj
Simple tf-idf in 30 lines of clojure. Inspired by a nice simple scala implementation: https://github.com/felipehummel/TinySearchEngine/blob/master/scala/tinySearch.scala and matches as closely as possible the computation.
(ns ignacio.tfidf (:require [clojure.contrib.string :as string])) ;; Simple tfidf in clojure, for fun.
(def *stopwords* (set (string/split #"\n" (slurp "./stopwords.txt"))))
(defn tokenize [raw-text] ;; Lowercases and splits on non-letters, non-numbers.
(remove *stopwords* (string/split #"[^a-z0-9äöüáéíóúãâêîôûàèìòùçñ]+" (string/lower-case raw-text))))
(defn idf2 [n-docs match] (Math/pow (Math/log (/ n-docs (count (keys match)))) 2))
(defn index-one [fname] ;; Index for one file. Given an fname, returns a map of token -> map of (fname, count)
(defmacro create-object [name props funcs]
(let [m (reduce #(conj %1 [%2 nil]) {} props)
d (symbol (str "dispatcher-" name))
msg-sym (gensym "msg")
params-sym (gensym "params")]
`(defn ~d [~msg-sym &~params-sym]
(cond
~@(for [f funcs]
[(= ~msg-sym ~(first f))
(apply ~(second f) ~m ~params-sym)])))))