Last active
January 18, 2020 01:28
-
-
Save rplevy/1ba2eee181a853c3824d114f69b579d3 to your computer and use it in GitHub Desktop.
ticket-sentiment.clj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ns ticket-sentiment.core | |
(:require [clojure.data.csv :as csv] | |
[clojure.java.io :as io] | |
[clojure.pprint :refer [pprint]] | |
[damionjunk.nlp.cmu-ark :as ark] | |
[damionjunk.nlp.stanford :as nlp])) | |
(defn mean [coll] | |
(let [sum (apply + coll) | |
count (count coll)] | |
(if (pos? count) | |
(/ sum count) | |
0))) | |
(defn median [coll] | |
(let [sorted (sort coll) | |
cnt (count sorted) | |
halfway (quot cnt 2)] | |
(if (odd? cnt) | |
(nth sorted halfway) | |
(let [bottom (dec halfway) | |
bottom-val (nth sorted bottom) | |
top-val (nth sorted halfway)] | |
(mean [bottom-val top-val]))))) | |
(defn -main [& _] | |
(with-open [writer (io/writer "scored-output.csv") | |
reader (io/reader | |
(io/resource | |
"sql_runner_dr6tdfnkpwfxzh_2020-01-08_19-17-24.csv"))] | |
(csv/write-csv writer | |
(cons ["ID" "SENTIMENT" "FROM-MEMBER"] | |
(for [[id dt text email] (csv/read-csv reader)] | |
(let [sentence-sentiments (map :sentiment (nlp/sentiment-maps text)) | |
total-sentiment (apply max | |
[(double (mean sentence-sentiments)) | |
(double (median sentence-sentiments))]) | |
from-member? (and email | |
(not | |
(re-find #"@starcity.com$" email))) | |
row-out [id total-sentiment from-member?]] | |
(prn row-out) | |
row-out)))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment