|
|
@ -12,6 +12,7 @@
|
|
|
|
ParserFactory)
|
|
|
|
ParserFactory)
|
|
|
|
(opennlp.tools.cmdline.parser ParserTool)))
|
|
|
|
(opennlp.tools.cmdline.parser ParserTool)))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(comment tb2/phrases)
|
|
|
|
(def tokenize (nlp/make-tokenizer (io/resource "models/en-token.bin")))
|
|
|
|
(def tokenize (nlp/make-tokenizer (io/resource "models/en-token.bin")))
|
|
|
|
(def get-sentences (nlp/make-sentence-detector (io/resource "models/en-sent.bin")))
|
|
|
|
(def get-sentences (nlp/make-sentence-detector (io/resource "models/en-sent.bin")))
|
|
|
|
(def parse (tb/make-treebank-parser (io/resource "models/en-parser-chunking.bin")))
|
|
|
|
(def parse (tb/make-treebank-parser (io/resource "models/en-parser-chunking.bin")))
|
|
|
@ -231,17 +232,23 @@
|
|
|
|
|
|
|
|
|
|
|
|
Porcelain. If you have the simple tree data structure
|
|
|
|
Porcelain. If you have the simple tree data structure
|
|
|
|
returned by `parse-to-simple-tree`, then you can just
|
|
|
|
returned by `parse-to-simple-tree`, then you can just
|
|
|
|
pass that directly to `zip/seq-zip`."
|
|
|
|
pass that directly to `zip/seq-zip`.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Returns nil if something can't be parsed. This will be
|
|
|
|
|
|
|
|
the case for empty strings."
|
|
|
|
[text]
|
|
|
|
[text]
|
|
|
|
(let [tree (->> text
|
|
|
|
(try
|
|
|
|
tokenize
|
|
|
|
(let [tree (->> text
|
|
|
|
(string/join " ")
|
|
|
|
tokenize
|
|
|
|
vector
|
|
|
|
(string/join " ")
|
|
|
|
parse
|
|
|
|
vector
|
|
|
|
first
|
|
|
|
parse
|
|
|
|
tb/make-tree
|
|
|
|
first
|
|
|
|
unmake-tree)]
|
|
|
|
tb/make-tree
|
|
|
|
(zip/seq-zip tree)))
|
|
|
|
unmake-tree)]
|
|
|
|
|
|
|
|
(doall (zip/seq-zip tree)))
|
|
|
|
|
|
|
|
(catch Exception e
|
|
|
|
|
|
|
|
nil)))
|
|
|
|
|
|
|
|
|
|
|
|
(comment
|
|
|
|
(comment
|
|
|
|
;; Here is a demo of zipping through a parse tree and changing
|
|
|
|
;; Here is a demo of zipping through a parse tree and changing
|
|
|
@ -1102,10 +1109,14 @@
|
|
|
|
(defn breadth-first
|
|
|
|
(defn breadth-first
|
|
|
|
[zipper]
|
|
|
|
[zipper]
|
|
|
|
(letfn [(zip-children [loc]
|
|
|
|
(letfn [(zip-children [loc]
|
|
|
|
(when-let [first-child (zip/down loc)]
|
|
|
|
(try
|
|
|
|
(take-while
|
|
|
|
(when-let [first-child (zip/down loc)]
|
|
|
|
(comp not nil?)
|
|
|
|
(take-while
|
|
|
|
(iterate zip/right first-child))))]
|
|
|
|
(complement nil?)
|
|
|
|
|
|
|
|
(iterate zip/right first-child)))
|
|
|
|
|
|
|
|
(catch Exception e
|
|
|
|
|
|
|
|
(println (zip/root loc))
|
|
|
|
|
|
|
|
(throw e))))]
|
|
|
|
(loop [result []
|
|
|
|
(loop [result []
|
|
|
|
queue (conj clojure.lang.PersistentQueue/EMPTY zipper)]
|
|
|
|
queue (conj clojure.lang.PersistentQueue/EMPTY zipper)]
|
|
|
|
(if (seq queue)
|
|
|
|
(if (seq queue)
|
|
|
@ -1135,27 +1146,44 @@
|
|
|
|
(map first))])))
|
|
|
|
(map first))])))
|
|
|
|
(remove (comp nil? second))))
|
|
|
|
(remove (comp nil? second))))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(defn parts-of-speech-trie-entries
|
|
|
|
|
|
|
|
"Given a zipper of a treebank parse tree, returns a sequence of
|
|
|
|
|
|
|
|
key-value pairs where the key is a sequence of parts-of-speech
|
|
|
|
|
|
|
|
to traverse down the tree and the values are the children
|
|
|
|
|
|
|
|
in the parse tree at that path.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
This can be plugged into a Trie with frequency data to
|
|
|
|
|
|
|
|
give you the following kind of info:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
{'(TOP)
|
|
|
|
|
|
|
|
{'(S) {:freq 534
|
|
|
|
|
|
|
|
'(NP VB) {:freq 233}
|
|
|
|
|
|
|
|
'(NP ADJP VB {:freq 210})
|
|
|
|
|
|
|
|
,,,}
|
|
|
|
|
|
|
|
'(SBARQ) {:freq 110}
|
|
|
|
|
|
|
|
'(SQ) {:freq 23}}}
|
|
|
|
|
|
|
|
"
|
|
|
|
|
|
|
|
[zipper]
|
|
|
|
|
|
|
|
(try
|
|
|
|
|
|
|
|
(->> (breadth-first zipper)
|
|
|
|
|
|
|
|
(filter (comp symbol? zip/node))
|
|
|
|
|
|
|
|
(map zip/prev)
|
|
|
|
|
|
|
|
(filter zip/branch?)
|
|
|
|
|
|
|
|
(mapv (fn [loc]
|
|
|
|
|
|
|
|
[(->> (zip/next loc)
|
|
|
|
|
|
|
|
(zip/path)
|
|
|
|
|
|
|
|
(map first)
|
|
|
|
|
|
|
|
(filter symbol?))
|
|
|
|
|
|
|
|
(let [child (zip/next (zip/next loc))]
|
|
|
|
|
|
|
|
(if (zip/branch? (zip/next child))
|
|
|
|
|
|
|
|
(map first (zip/node child))
|
|
|
|
|
|
|
|
(zip/node child)))])))
|
|
|
|
|
|
|
|
(catch Exception e
|
|
|
|
|
|
|
|
(println (zip/node zipper))
|
|
|
|
|
|
|
|
(throw e))))
|
|
|
|
|
|
|
|
|
|
|
|
(comment
|
|
|
|
(comment
|
|
|
|
(->> (zip/vector-zip [1 [2 [3]]])
|
|
|
|
(parts-of-speech-trie-entries
|
|
|
|
(iterate zip/next)
|
|
|
|
|
|
|
|
(take 6)
|
|
|
|
|
|
|
|
last
|
|
|
|
|
|
|
|
zip/path
|
|
|
|
|
|
|
|
(map first))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(->> (breadth-first
|
|
|
|
|
|
|
|
(zip/seq-zip
|
|
|
|
|
|
|
|
'(TOP
|
|
|
|
|
|
|
|
((S
|
|
|
|
|
|
|
|
((NP
|
|
|
|
|
|
|
|
((NP ((NN ("Everything")))) (PP ((IN ("of")) (NP ((NN ("today"))))))))
|
|
|
|
|
|
|
|
(VP ((VBZ ("is")) (VP ((VBG ("falling"))))))
|
|
|
|
|
|
|
|
(. ("."))))))))
|
|
|
|
|
|
|
|
(map loc-children)
|
|
|
|
|
|
|
|
(filter seq?)
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(part-of-speech-children
|
|
|
|
|
|
|
|
(zip/seq-zip
|
|
|
|
(zip/seq-zip
|
|
|
|
'(TOP
|
|
|
|
'(TOP
|
|
|
|
((S
|
|
|
|
((S
|
|
|
@ -1163,25 +1191,19 @@
|
|
|
|
((NP ((NN ("Everything")))) (PP ((IN ("of")) (NP ((NN ("today"))))))))
|
|
|
|
((NP ((NN ("Everything")))) (PP ((IN ("of")) (NP ((NN ("today"))))))))
|
|
|
|
(VP ((VBZ ("is")) (VP ((VBG ("falling"))))))
|
|
|
|
(VP ((VBZ ("is")) (VP ((VBG ("falling"))))))
|
|
|
|
(. ("."))))))))
|
|
|
|
(. ("."))))))))
|
|
|
|
|
|
|
|
;; => ([(TOP) (S)]
|
|
|
|
(->> (zip/seq-zip
|
|
|
|
;; [(TOP S) (NP VP .)]
|
|
|
|
'(TOP
|
|
|
|
;; [(TOP S NP) (NP PP)]
|
|
|
|
((S
|
|
|
|
;; [(TOP S VP) (VBZ VP)]
|
|
|
|
((NP
|
|
|
|
;; [(TOP S .) (".")]
|
|
|
|
((NP ((NN ("Everything")))) (PP ((IN ("of")) (NP ((NN ("today"))))))))
|
|
|
|
;; [(TOP S NP NP) (NN)]
|
|
|
|
(VP ((VBZ ("is")) (VP ((VBG ("falling"))))))
|
|
|
|
;; [(TOP S NP PP) (IN NP)]
|
|
|
|
(. (".")))))))
|
|
|
|
;; [(TOP S VP VBZ) ("is")]
|
|
|
|
(zip/next)
|
|
|
|
;; [(TOP S VP VP) (VBG)]
|
|
|
|
(zip/next)
|
|
|
|
;; [(TOP S NP NP NN) ("Everything")]
|
|
|
|
(zip/next)
|
|
|
|
;; [(TOP S NP PP IN) ("of")]
|
|
|
|
(zip/next)
|
|
|
|
;; [(TOP S NP PP NP) (NN)]
|
|
|
|
(zip/next)
|
|
|
|
;; [(TOP S VP VP VBG) ("falling")]
|
|
|
|
(zip/node)
|
|
|
|
;; [(TOP S NP PP NP NN) ("today")])
|
|
|
|
#_#_(loc-children)
|
|
|
|
|
|
|
|
(map first))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
)
|
|
|
|
)
|
|
|
|
(comment
|
|
|
|
|
|
|
|
(defn part-of-speech-n-grams
|
|
|
|
|
|
|
|
[zipper]
|
|
|
|
|
|
|
|
(letfn [(fn step [path []])])))
|
|
|
|
|
|
|
|