From 8da3378ae9d4c39c0cb4ac5d5a935bef006092d7 Mon Sep 17 00:00:00 2001 From: Sergio Flores Date: Tue, 26 Sep 2017 01:20:11 +0100 Subject: [PATCH 1/2] SentimentIntensityAnalyzer can now be initialized from custom file instead of manifest resource. Removed unnecessary static modifiers. --- .../VaderSharp/SentimentIntensityAnalyzer.cs | 41 ++++++++++++++----- 1 file changed, 31 insertions(+), 10 deletions(-) diff --git a/VaderSharp/VaderSharp/SentimentIntensityAnalyzer.cs b/VaderSharp/VaderSharp/SentimentIntensityAnalyzer.cs index 5348976..4b10ac3 100644 --- a/VaderSharp/VaderSharp/SentimentIntensityAnalyzer.cs +++ b/VaderSharp/VaderSharp/SentimentIntensityAnalyzer.cs @@ -16,26 +16,47 @@ public class SentimentIntensityAnalyzer private const double QuesIncrSmall = 0.18; private const double QuesIncrLarge = 0.96; - private static Dictionary Lexicon { get; } - private static string[] LexiconFullFile { get; } + private Dictionary Lexicon = null; + private string[] LexiconFullFile = null; - static SentimentIntensityAnalyzer() + public SentimentIntensityAnalyzer() { - Assembly assembly; + if (Lexicon == null) + { + Assembly assembly; #if NET_35 assembly = typeof(SentimentIntensityAnalyzer).Assembly; #else - assembly = typeof(SentimentIntensityAnalyzer).GetTypeInfo().Assembly; + assembly = typeof(SentimentIntensityAnalyzer).GetTypeInfo().Assembly; #endif - using (var stream = assembly.GetManifestResourceStream("VaderSharp.vader_lexicon.txt")) - using(var reader = new StreamReader(stream)) + using (var stream = assembly.GetManifestResourceStream("VaderSharp.vader_lexicon.txt")) + using (var reader = new StreamReader(stream)) + { + LexiconFullFile = reader.ReadToEnd().Split('\n'); + Lexicon = MakeLexDic(); + } + } + } + + public SentimentIntensityAnalyzer(string fileName) + { + if (Lexicon == null) { - LexiconFullFile = reader.ReadToEnd().Split('\n'); - Lexicon = MakeLexDic(); + if (!File.Exists(fileName)) + { + throw new Exception("Lexicon file not found"); + } + + using (var stream = new FileStream(fileName, FileMode.Open)) + using (var reader = new StreamReader(stream)) + { + LexiconFullFile = reader.ReadToEnd().Split('\n'); + Lexicon = MakeLexDic(); + } } } - private static Dictionary MakeLexDic() + private Dictionary MakeLexDic() { var dic = new Dictionary(); foreach (var line in LexiconFullFile) From 69e4733979398da768d7141acb54b81adcfbb3b3 Mon Sep 17 00:00:00 2001 From: Sergio Flores Date: Tue, 26 Sep 2017 01:21:06 +0100 Subject: [PATCH 2/2] Lexicon dictionary loading failed in non-english systems due to CultureInfo differences --- VaderSharp/VaderSharp/SentimentIntensityAnalyzer.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/VaderSharp/VaderSharp/SentimentIntensityAnalyzer.cs b/VaderSharp/VaderSharp/SentimentIntensityAnalyzer.cs index 4b10ac3..9ab5972 100644 --- a/VaderSharp/VaderSharp/SentimentIntensityAnalyzer.cs +++ b/VaderSharp/VaderSharp/SentimentIntensityAnalyzer.cs @@ -62,7 +62,7 @@ private Dictionary MakeLexDic() foreach (var line in LexiconFullFile) { var lineArray = line.Trim().Split('\t'); - dic.Add(lineArray[0], Double.Parse(lineArray[1])); + dic.Add(lineArray[0], Double.Parse(lineArray[1], System.Globalization.CultureInfo.InvariantCulture)); } return dic; }