Skip to content

Instantly share code, notes, and snippets.

@jirkamarsik
jirkamarsik / fix_adjunctiono.clj
Created December 18, 2012 00:07
An example of a non-trivial core.logic predicate.
(defn ^::rule fix-adjunctiono
"A rule which takes a left-most or right-most child A, which is an
adjunct, and puts the A on a separate level from the other children,
i.e. (R A ...) -> (R A (R ...)) or (R ... A) -> (R (R ...) A).
This solves the general case when modifiers and adjuncts end up as
children of the consituent they modify (e.g. in VPs)."
[in-tree out-tree]
(fresh [root children other-children adjunct head-child]
(conso root children in-tree)
@jirkamarsik
jirkamarsik / poly_appendo.clj
Created December 17, 2012 23:52
A variadic appendo for core.logic.
(defn poly-appendo
"Like clojure.core.logic/appendo, but works with arbitrary number of
args, like clojure.core/concat."
([x y]
(== x y))
([x y & more]
(fresh [z]
(apply poly-appendo z more)
(appendo x y z))))
@jirkamarsik
jirkamarsik / simple_rule.clj
Created December 17, 2012 23:23
Tree-rewriting rules using unification.
(defn simple-rule [in-tree]
(if-let [res (seq (run 1 [out-tree]
(fresh [X Y Z W]
(== in-tree ['VP X Y ['PP Z W]])
(== out-tree ['VP ['VP X Y] ['PP Z W]]))))]
(first res)
in-tree))
;=> #'user/simple-rule
(simple-rule '[VP [VB plays] [NP piano] [PP [IN for] [NP fun]]])
@jirkamarsik
jirkamarsik / preprocess.clj
Created December 17, 2012 22:42
Normalizing Stanford constituency trees as part of my "Applications of NLP" project.
(ns grook.preprocess
(:refer-clojure :exclude [==])
(:use clojure.core.logic
[clojure.pprint :only [pprint]])
(:require [clojure.walk :as walk]
[clojure.java.io :as io]))
;; Running this cleans any previously defined rules from the namespace.
(dorun (for [[sym var] (ns-interns *ns*)
:when (::rule (meta var))]