Created
September 19, 2016 08:55
-
-
Save monkieboy/bce68f5cfeca0625e3032f3cd778a48e to your computer and use it in GitHub Desktop.
Copying sample from: http://ntrush.blogspot.co.uk/2014/10/using-stanford-nlp-to-make-sentiment.html
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
open System | |
open System.IO | |
open edu.stanford.nlp.ling | |
open edu.stanford.nlp.neural.rnn | |
open edu.stanford.nlp.sentiment | |
open edu.stanford.nlp.trees | |
open edu.stanford.nlp.util | |
open java.util | |
open edu.stanford.nlp.pipeline | |
let classForType<'t> = | |
java.lang.Class.op_Implicit typeof<'t> | |
type SentimentPrediction = | |
| VeryNegative | |
| Negative | |
| Neutral | |
| Positive | |
| VeryPositive | |
let classToSentiment = function | |
| 0 -> VeryNegative | |
| 1 -> Negative | |
| 2 -> Neutral | |
| 3 -> Positive | |
| 4 -> VeryPositive | |
| _ -> failwith "unknown class" | |
let makeSentimentAnalyzer modelsDir = | |
let props = Properties() | |
props.setProperty("annotators", "tokenize, ssplit, pos, parse, sentiment") |> ignore | |
let currDir = Environment.CurrentDirectory | |
Directory.SetCurrentDirectory modelsDir | |
let pipeline = StanfordCoreNLP(props) | |
Directory.SetCurrentDirectory currDir | |
fun text -> | |
let proc = pipeline.``process`` text | |
let T = proc.get classForType<CoreAnnotations.SentencesAnnotation> | |
let arrayList = T :?> ArrayList | |
arrayList | |
|> Seq.cast<CoreMap> | |
|> Seq.map(fun cm -> | |
cm.get classForType<SentimentCoreAnnotations.AnnotatedTree>) | |
|> Seq.cast<Tree> | |
|> Seq.map (RNNCoreAnnotations.getPredictedClass >> classToSentiment) | |
|> Seq.toList | |
[<EntryPoint>] | |
let main argv = | |
let text = "awesome great this text is so exciting! this is disgusting sentence number two."; | |
let modelsDir = @"/Users/markgray/dev/NlpConsole/stanford-corenlp-full-2014-06-16/edu/stanford/nlp/models"; | |
let analyzer = makeSentimentAnalyzer modelsDir | |
printfn "%A" (analyzer text) | |
0 // return an integer exit code | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment