/
StanfordNLPDemo.clj
37 lines (31 loc) · 1.07 KB
/
StanfordNLPDemo.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
;; This is a demonstration of a simple StanfordCoreNLP
;; Running this file returns part of speech tags for the first sentence below
;; (PRP MD VB PRP IN DT NNP NNP .)
;; (c) JT Wolohan, 2018, GPL 3.0
(ns wolohan.coyotetuts.corenlp
(:gen-class)
(:require [clj-json.core :as json])
(:import [edu.stanford.nlp.pipeline StanfordCoreNLP]))
(defn -main
[& args]
(defn CoreNLP
"StanfordCoreNLP class builder"
[props]
(let [ps (java.util.Properties.)]
(. ps setProperty "annotators" props)
(StanfordCoreNLP. ps)))
(defn pipe2JSON
"Runs text through a CoreNLP pipeline"
[pipeline t]
(let [txt (.process pipeline t)]
;; (. pipeline annotate txt)
(let [buffer (java.io.StringWriter.)]
(.jsonPrint pipeline txt buffer)
(json/parse-string (.toString buffer)))))
;; create nlp pipeline
(def nlpipe (CoreNLP "tokenize, ssplit, pos, lemma"))
;; call lein run --
(let [a (pipe2JSON nlpipe "I'll meet you by the Sample Gates. Do you know where that is?") ]
(println (map (fn [x] (get x "pos"))
(get (get (get a "sentences") 0) "tokens"))))
)