-
Notifications
You must be signed in to change notification settings - Fork 2
/
parse.clj
252 lines (215 loc) · 7.42 KB
/
parse.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
(ns ^{:doc "Parse an utterance using the Stanford CoreNLP and the ClearNLP SRL.
This is the main client entry point to the package. A default out of the box
parser works that comes with components listed
in [[zensols.nlparse.config/all-components]].
If you want to customzie or add your own parser plug in, see
the [[zensols.nlparse.config]] namespace."
:author "Paul Landes"}
zensols.nlparse.parse
(:require [clojure.string :as str]
[clojure.tools.logging :as log]
[clojure.set :as set])
(:require [zensols.actioncli.util :refer (trunc with-timeout)])
(:require [zensols.nlparse.config :as conf]
[zensols.nlparse.stanford :as sp]
[zensols.nlparse.srl :as srl]))
(def sentimnet-labels
"All labels returned by [[sentiment-score-to-label]] in order of positive to
negative."
["very positive" "positive" "very negative" "negative" "neutral"])
(def penn-treebank-pos-tags
"Alphabetical list of part-of-speech tags used in the [Penn Treebank
Project](https://www.ling.upenn.edu/courses/Fall_2003/ling001/penn_treebank_pos.html)."
{"CC" "Coordinating conjunction"
"CD" "Cardinal number"
"DT" "Determiner"
"EX" "Existential there"
"FW" "Foreign word"
"IN" "Preposition or subordinating conjunction"
"JJ" "Adjective"
"JJR" "Adjective, comparative"
"JJS" "Adjective, superlative"
"LS" "List item marker"
"MD" "Modal"
"NN" "Noun, singular or mass"
"NNS" "Noun, plural"
"NNP" "Proper noun, singular"
"NNPS" "Proper noun, plural"
"PDT" "Predeterminer"
"POS" "Possessive ending"
"PRP" "Personal pronoun"
"PRP$" "Possessive pronoun"
"RB" "Adverb"
"RBR" "Adverb, comparative"
"RBS" "Adverb, superlative"
"RP" "Particle"
"SYM" "Symbol"
"TO" "to"
"UH" "Interjection"
"VB" "Verb, base form"
"VBD" "Verb, past tense"
"VBG" "Verb, gerund or present participle"
"VBN" "Verb, past participle"
"VBP" "Verb, non-3rd person singular present"
"VBZ" "Verb, 3rd person singular present"
"WDT" "Wh-determiner"
"WP" "Wh-pronoun"
"WP$" "Possessive wh-pronoun"
"WRB" "Wh-adverb"})
(defn pos-description
"Get a human readable description of *pos-tag*."
[pos-tag]
(get penn-treebank-pos-tags pos-tag))
;; POS tag adjust
(def ^:private noun-tags
#{"NN" "NNS" "NNP" "NNPS"})
(def ^:private verb-tags
#{"VB" "VBD" "VBG" "VBN" "VBP" "VBZ"})
(def ^:private adjective-tags
#{"JJ" "JJR" "JJS"})
(def ^:private adverb-tags
#{"RB" "RBR" "RBS" "WRB"})
(def ^:private pos-tag-list
#{"$" "#" "''" "," "-LRB-" "-RRB-" "." ":" "CC" "CD" "DT" "IN" "JJ" "MD"
"NN" "NNP" "NNPS" "NNS" "PDT" "POS" "PRP" "RB" "RP" "SYM"
"VB" "VBD" "VBG" "VBN" "VBP" "VBZ" "LS"})
(def ^:private wh-tags
#{"WDT" "WRB" "WP" "WP$"})
(defn pos-tags
"Get a sequence of POS tags based on **type** (if given) and all otherwise.
**type** is a symbol and one of `noun`, `verb`, `adjective`, `adverb`, and
`wh`.
See [[pos-tag-types]]."
([type]
(case type
noun noun-tags
verb verb-tags
adjective adjective-tags
adverb adverb-tags
wh wh-tags))
([]
(set/union (set (keys penn-treebank-pos-tags)) pos-tag-list)))
(defn pos-tag-types
"Return types of POS tags (i.e. `noun`, `verb`, etc).
See [[pos-tags]]."
[]
#{"noun" "verb" "adjective" "adverb" "wh"})
(defn pos-tag-type
"Return the POS tag type (ie. `verb` yeilds `VB`).
The return (function range) is the input to [[pos-tags]]."
[tag]
(->> (map (fn [type]
(if (contains? (pos-tags type) tag) type))
'(noun verb adjective adverb wh))
(remove nil?)
first))
(defn- parse-no-timeout [utterance]
(->> (conf/parse-functions)
(reduce (fn [last-res parse-fn]
(log/debugf "next parser: %s" parse-fn)
(parse-fn last-res))
utterance)))
(defn parse
"Parse natural language **utterance** returning a symbol expression tree of
it's meaning.
This returns a symbolic expression (map of maps). A definition of these and
structure of these annotations are [given
here](https://github.com/plandes/clj-nlp-parse/blob/master/doc/annotation-definitions.md).
See [test
token-regex](https://github.com/plandes/clj-nlp-parse/blob/v0.0.11/test-resources/token-regex.txt#L3)
for example of `entity-tag`."
[utterance]
(let [timeout-millis (conf/parse-timeout)]
(log/infof "parsing (t=%s): <%s>"
(or timeout-millis "inf") (trunc utterance))
(if timeout-millis
(with-timeout timeout-millis
(parse-no-timeout utterance))
(parse-no-timeout utterance))))
(defn tokens
"Get all tokens across all sentences."
[panon]
(->> panon :sents (map :tokens) (apply concat)))
(defn token
"Get the 0-based **index**th token."
[panon index]
(nth (tokens panon) index))
(defn sent
"Get the 0-based **index**th sentence."
[panon index]
(nth (:sents panon) index))
(defn root-dependency
"Get the text of the root node of the dependency tree."
[sent]
(->> sent
:dependency-parse-tree first :token-index dec (nth (:tokens sent))))
(defn overlap?
"Return whether two ranges overlap inclusively. Both parameters have the
form: `[start end]`."
[a b]
(let [[sa ea] a
[sb eb] b]
(and (<= sa eb) (<= sb ea))))
(defn in-range?
"Return whether **inner** range sits in inclusive range **outter**. Both
parameters have the form: `[start end]`."
[outter inner]
(let [[so eo] outter
[si ei] inner]
(and (>= si so) (<= ei eo))))
(defn token-in-range?
"Return whether token **tok** is in tuple **range**."
[token range]
(in-range? range (:token-range token)))
(defn mentions
"Get all mentions from parse annotation **panon**.
Keys
----
* **:mention-keys** top level keys of mentions to use and defaults to
`[:mentions :tok-re-mentions]`"
[panon & {:keys [mention-keys]
:or {mention-keys [:mentions :tok-re-mentions]}}]
(->> mention-keys
(map #(get panon %))
(apply concat)))
(defn mention-for-token
"Return a mention for token **tok** (if any)."
[panon token]
(->> (:mentions panon)
(filter #(->> % :token-range (token-in-range? token)))))
(defn tokens-by-sentence
"Return the tokens for **sent-index** in the **token-range**."
[panon sent-index token-range]
(let [sent (-> panon :sents (nth sent-index) :tokens)]
(->> sent
(drop (first token-range))
(take (- (second token-range) (first token-range))))))
(defn tokens-for-mention
"Return tokens for **mention**."
[panon mention]
(let [{:keys [token-range sent-index]} mention
sents (->> panon :sents)]
(when (and mention (< sent-index (count sents)))
(->> sents (#(nth % sent-index)) :tokens
(filter #(token-in-range? % (:token-range mention)))))))
(defn token-mentions
"Return mentions with a `:tokens` key that includes token maps from the
sentence level."
([panon]
(token-mentions panon (mentions panon)))
([panon mentions]
(->> mentions
(map (fn [mention]
(->> mention
(tokens-for-mention panon)
(assoc mention :tokens)))))))
(defn sentiment-score-to-label
"Create a human readable tag from the sentiment score.
See [[sentimnet-labels]]."
[sentiment-score]
(cond (nil? sentiment-score) nil
(> sentiment-score 1) "very positive"
(= sentiment-score 1) "positive"
(< sentiment-score -1) "very negative"
(= sentiment-score -1) "negative"
(= 0 sentiment-score) "neutral"))