Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve dependency graph generation #1

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ string with:

### Stanford Dependencies

(dependency-graph "I like cheese.")
(dependency-graph (dependency-parse (tokenize "I like cheese.")))

will parse the sentence and return the dependency graph as a
[loom](https://github.com/jkk/loom) graph, which you can then traverse with
Expand Down
2 changes: 1 addition & 1 deletion project.clj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
:dependencies [[org.clojure/clojure "1.7.0"]
[edu.stanford.nlp/stanford-corenlp "3.5.2"]
[edu.stanford.nlp/stanford-corenlp "3.5.2" :classifier "models"]
[cc.artifice/loom "0.1.3"]]
[aysylu/loom "0.5.4"]]
:plugins [[lein-exec "0.3.5"]]
:url "https://github.com/arnaudsj/stanford-corenlp"
:license {:name "Eclipse Public License"
Expand Down
14 changes: 9 additions & 5 deletions src/corenlp.clj
Original file line number Diff line number Diff line change
Expand Up @@ -194,14 +194,18 @@

(defn dependency-graph [dp]
"Produce a loom graph from a DependencyParse record."
(let [[words tags edges] (map #(% dp) [:words :tags :edges])
g (apply digraph (map (partial take 2) edges))]
(let [dependencies (:edges dp)
g (apply digraph (map (partial take 2) dependencies))
node-vec (vec (remove neg? (nodes g)))
on-graph (fn [dp-keyword] (map (vec (map-indexed vector (dp-keyword dp))) node-vec))
graph-words (on-graph :words)
graph-tags (on-graph :tags)]
(reduce (fn [g [i t]] (add-attr g i :tag t))
(reduce (fn [g [i w]] (add-attr g i :word w))
(reduce (fn [g [gov dep type]]
(add-attr g gov dep :type type)) g edges)
(map-indexed vector words))
(map-indexed vector tags))))
(add-attr g gov dep :type type)) g dependencies)
graph-words)
graph-tags)))

(def dependency-parse nil)

Expand Down
9 changes: 8 additions & 1 deletion test/corenlp/parsing_test.clj
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
(ns corenlp.parsing-test
(:use clojure.test)
(:require [corenlp]))
(:require [corenlp]
[loom.graph :as lg]))



Expand All @@ -26,3 +27,9 @@
"Named Entity Recognition"
(is (= '("PERSON" "PERSON" "O" "O" "O" "O" "O" "LOCATION" "LOCATION" "O")
(corenlp/named-entities "Barack Obama is the president of the United States."))))

(deftest dependency-graph-test
"Loom dependency graph"
(let [dp (corenlp/dependency-parse (corenlp/tokenize "Me, and you, like cheese (a lot)."))]
(is (= #{0 -1 6 3 2 9 5 8}
(lg/nodes (corenlp/dependency-graph dp))))))