-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCoreNLPSentiment.cs
More file actions
272 lines (190 loc) · 10.4 KB
/
CoreNLPSentiment.cs
File metadata and controls
272 lines (190 loc) · 10.4 KB
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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Drawing;
using PluginContracts;
using OutputHelperLib;
using System.ComponentModel;
using System.Text;
using System.IO;
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.util;
using System.Linq;
namespace CoreNLPSentiment
{
public class CoreNLPSentiment : Plugin
{
public string[] InputType { get; } = { "String" };
public string OutputType { get; } = "OutputArray";
public Dictionary<int, string> OutputHeaderData { get; set; } = new Dictionary<int, string>() { {0, "SentNumber"},
{1, "Classification"},
{2, "Class_Prob"},
{3, "Class_Number"},
{4, "Prob_VeryNeg"},
{5, "Prob_Neg"},
{6, "Prob_Neut"},
{7, "Prob_Pos"},
{8, "Prob_VeryPos"},
{9, "SentenceText"} };
public bool InheritHeader { get; } = false;
#region Plugin Details and Info
public string PluginName { get; } = "CoreNLP Sentiment Analysis";
public string PluginType { get; } = "Sentiment Analysis";
public string PluginVersion { get; } = "1.1.01";
public string PluginAuthor { get; } = "Ryan L. Boyd (ryan@ryanboyd.io)";
public string PluginDescription { get; } = "Built around Stanford's CoreNLP for .NET (v3.9.1, English model). Uses a trained RNN model to classify sentences from \"very negative\" to \"very positive\". Produces sentence-level scores as output" + Environment.NewLine + Environment.NewLine +
"Manning, Christopher D., Mihai Surdeanu, John Bauer, Jenny Finkel, Steven J. Bethard, and David McClosky. 2014. The Stanford CoreNLP Natural Language Processing Toolkit In Proceedings of the 52nd Annual Meeting of the Association for Computational Linguistics: System Demonstrations, pp. 55-60.";
public bool TopLevel { get; } = false;
public string PluginTutorial { get; } = "Coming Soon";
public Icon GetPluginIcon
{
get
{
return Properties.Resources.icon;
}
}
#endregion
private bool includeSentenceText { get; set; } = true;
private bool useBuiltInSentenceSplitter { get; set; } = true;
#region Setup Tagger Details
public static string jarRoot = Path.Combine(Path.GetDirectoryName(AppDomain.CurrentDomain.BaseDirectory),
"Plugins" + Path.DirectorySeparatorChar +
"Dependencies" + Path.DirectorySeparatorChar + @"stanford-corenlp-full-2018-02-27" + Path.DirectorySeparatorChar);
private StanfordCoreNLP pipeline { get; set; }
#endregion
public void ChangeSettings()
{
using (var form = new SettingsForm_CoreNLPSentiment(builtInSplitter: useBuiltInSentenceSplitter, textInOutput: includeSentenceText))
{
form.Icon = Properties.Resources.icon;
var result = form.ShowDialog();
if (result == DialogResult.OK)
{
useBuiltInSentenceSplitter = form.useBuiltInSentenceSplitter;
includeSentenceText = form.includeSentenceText;
}
}
}
public Payload RunPlugin(Payload Input)
{
Payload pData = new Payload();
pData.FileID = Input.FileID;
bool trackSegmentID = false;
if (Input.SegmentID.Count > 0)
{
trackSegmentID = true;
}
else
{
pData.SegmentID = Input.SegmentID;
}
for (int i = 0; i < Input.StringList.Count; i++)
{
//seems to prematurely exit sometimes. checking to see what might cause that -- maybe blank docs?
if (!string.IsNullOrEmpty(Input.StringList[i]) && !string.IsNullOrWhiteSpace(Input.StringList[i])) {
Annotation annotation = new edu.stanford.nlp.pipeline.Annotation();
ArrayList sentences = new ArrayList();
List<double> SentimentValues = new List<double>();
annotation = new edu.stanford.nlp.pipeline.Annotation(Input.StringList[i]);
pipeline.annotate(annotation);
sentences = annotation.get(new CoreAnnotations.SentencesAnnotation().getClass()) as ArrayList;
int SentenceCount = 0;
foreach (CoreMap sentence in sentences)
{
SentenceCount++;
Tree tree = sentence.get(new SentimentCoreAnnotations.SentimentAnnotatedTree().getClass()) as Tree;
//add this sentence to our overall list of sentiment scores
SentimentValues.Add(RNNCoreAnnotations.getPredictedClass(tree));
string[] OutputString_SentenceLevel = new string[10] { "", "", "", "", "", "", "", "", "", ""};
string Classification = GetClassification((double)RNNCoreAnnotations.getPredictedClass(tree));
//this pulls out the prediction probabilites for each class
string Predictions = RNNCoreAnnotations.getPredictionsAsStringList(tree).ToString();
string[] Predictions_Split = Predictions.Replace("[", "").Replace("]", "").Split(',');
if (useBuiltInSentenceSplitter)
{
OutputString_SentenceLevel[0] = SentenceCount.ToString();
}
else
{
//if we're using an external sentence tokenizer, then every segment is
//going to be treated as its own sentence.
OutputString_SentenceLevel[0] = (i + 1).ToString();
}
OutputString_SentenceLevel[1] = Classification;
OutputString_SentenceLevel[2] = RNNCoreAnnotations.getPredictedClassProb(tree.label()).ToString();
OutputString_SentenceLevel[3] = RNNCoreAnnotations.getPredictedClass(tree).ToString();
OutputString_SentenceLevel[4] = Predictions_Split[0];
OutputString_SentenceLevel[5] = Predictions_Split[1];
OutputString_SentenceLevel[6] = Predictions_Split[2];
OutputString_SentenceLevel[7] = Predictions_Split[3];
OutputString_SentenceLevel[8] = Predictions_Split[4];
if (includeSentenceText) OutputString_SentenceLevel[9] = sentence.ToString();
pData.StringArrayList.Add(OutputString_SentenceLevel);
pData.SegmentNumber.Add(Input.SegmentNumber[i]);
if (trackSegmentID)
{
pData.SegmentID.Add(Input.SegmentID[i]);
}
}
}
else
{
pData.StringArrayList.Add(new string[10] { "", "", "", "", "", "", "", "", "", "" });
pData.SegmentNumber.Add(Input.SegmentNumber[i]);
if (trackSegmentID)
{
pData.SegmentID.Add(Input.SegmentID[i]);
}
}
}
return (pData);
}
public void Initialize()
{
var props = new java.util.Properties();
props.setProperty("annotators", "tokenize, ssplit, parse, sentiment");
//if we're using an external sentence segmentation strategy, then this is how we're going to do it
//https://stackoverflow.com/a/28017131
if (!useBuiltInSentenceSplitter) props.put("ssplit.isOneSentence", "true");
props.setProperty("sutime.binders", "0");
Directory.SetCurrentDirectory(jarRoot);
pipeline = new StanfordCoreNLP(props);
}
public Payload FinishUp(Payload Input)
{
return (Input);
}
private string GetClassification(double y)
{
if (y < 0.8) return "Very Negative";
else if (y < 1.6) return "Negative";
else if (y < 2.4) return "Neutral";
else if (y < 3.2) return "Positive";
else if (y <= 4) return "Very Positive";
else return "";
}
public bool InspectSettings()
{
return true;
}
#region Import/Export Settings
public void ImportSettings(Dictionary<string, string> SettingsDict)
{
includeSentenceText = Boolean.Parse(SettingsDict["IncludeSentenceText"]);
useBuiltInSentenceSplitter = Boolean.Parse(SettingsDict["useBuiltInSentenceSplitter"]);
}
public Dictionary<string, string> ExportSettings(bool suppressWarnings)
{
Dictionary<string, string> SettingsDict = new Dictionary<string, string>();
SettingsDict.Add("IncludeSentenceText", includeSentenceText.ToString());
SettingsDict.Add("useBuiltInSentenceSplitter", useBuiltInSentenceSplitter.ToString());
return (SettingsDict);
}
#endregion
}
}