Created
January 3, 2016 19:26
-
-
Save warpech/3223abd038cf8140f8da to your computer and use it in GitHub Desktop.
Sentiment analysis that takes long time
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
using edu.stanford.nlp.ling; | |
using edu.stanford.nlp.neural.rnn; | |
using edu.stanford.nlp.pipeline; | |
using edu.stanford.nlp.sentiment; | |
using edu.stanford.nlp.trees; | |
using edu.stanford.nlp.util; | |
using java.io; | |
using java.text; | |
using java.util; | |
using Starcounter; | |
using Starcounter.Internal; | |
using System; | |
using System.Collections.Generic; | |
using System.IO; | |
using System.Linq; | |
namespace MyImporter { | |
public class StanfordNLPModelNotFoundExpection : Exception { | |
} | |
public class SentimentHelper { | |
private string JarRoot; | |
private StanfordCoreNLP pipeline; | |
public string LastError; | |
public SentimentHelper() { | |
var CurDir = Application.Current.WorkingDirectory; | |
//JarRoot = @"..\..\..\..\paket-files\nlp.stanford.edu\stanford-corenlp-full-2015-12-09\models"; | |
JarRoot = Path.Combine(CurDir, "stanford-corenlp-full-2015-12-09\\stanford-corenlp-3.6.0-models"); | |
if (Directory.Exists(JarRoot) == false) { | |
LastError = typeof(StanfordNLPModelNotFoundExpection).Name; | |
return; | |
} | |
// Annotation pipeline configuration | |
var props = new java.util.Properties(); | |
props.setProperty("annotators", "tokenize, ssplit, parse, sentiment"); | |
//props.setProperty("ner.useSUTime", "0"); | |
Directory.SetCurrentDirectory(JarRoot); | |
pipeline = new StanfordCoreNLP(props); | |
Directory.SetCurrentDirectory(CurDir); | |
} | |
public float Analyze(string text) { | |
var annotation = new edu.stanford.nlp.pipeline.Annotation(text); | |
pipeline.annotate(annotation); | |
List<int> sentiments = new List<int>(); | |
var sentences = annotation.get(new CoreAnnotations.SentencesAnnotation().getClass()) as ArrayList; | |
foreach (CoreMap sentence in sentences) { | |
Tree tree = sentence.get(new SentimentCoreAnnotations.SentimentAnnotatedTree().getClass()) as Tree; | |
int sentiment = RNNCoreAnnotations.getPredictedClass(tree); | |
sentiments.Add(sentiment); | |
} | |
return (float)sentiments.Average(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Could you please try to execute the code in 32 an 64 bit modes and post execution time here?