using System.Collections.Generic;
using Microsoft.VisualStudio.TestTools.UnitTesting;
public class SentimentAnalyzer
public static string ClassifyText(string text, Dictionary<string, Dictionary<string, int>> trainingData)
var vocabulary = new HashSet<string>();
foreach (var sentimentData in trainingData.Values)
foreach (var word in sentimentData.Keys)
int vocabularySize = vocabulary.Count;
var sentiments = trainingData.Keys.ToList();
var priorProbabilities = new Dictionary<string, double>();
var totalSentimentCounts = new Dictionary<string, int>();
foreach (var sentiment in sentiments)
totalSentimentCounts[sentiment] = trainingData[sentiment].Values.Sum();
priorProbabilities[sentiment] = (double)trainingData[sentiment].Count / trainingData.Values.Sum(x => x.Count);
var textWords = text.Split(' ');
var logProbabilities = new Dictionary<string, double>();
foreach (var sentiment in sentiments)
logProbabilities[sentiment] = Math.Log(priorProbabilities[sentiment]);
foreach (var word in textWords)
if (trainingData[sentiment].ContainsKey(word))
double wordProbability = (double)(trainingData[sentiment][word] + 1) / (totalSentimentCounts[sentiment] + vocabularySize);
logProbabilities[sentiment] += Math.Log(wordProbability);
string predictedSentiment = "positive";
if (logProbabilities["negative"] > logProbabilities["positive"])
predictedSentiment = "negative";
return predictedSentiment;
public static void Main()
Console.WriteLine("š Discovering and Running Tests...\n");
int totalPassed = 0, totalFailed = 0;
var testClasses = Assembly.GetExecutingAssembly()
.Where(t => t.GetCustomAttribute<TestClassAttribute>() != null);
foreach (var testClass in testClasses)
Console.WriteLine($"š Running tests in: {testClass.Name}");
(int passed, int failed) = RunTests(testClass);
Console.WriteLine("\nš FINAL SUMMARY:");
Console.WriteLine($"ā
Total Passed: {totalPassed}");
Console.WriteLine($"ā Total Failed: {totalFailed}");
Console.WriteLine($"š Total Tests: {totalPassed + totalFailed}");
Console.WriteLine("\nā
Test execution completed.");
private static (int passed, int failed) RunTests(Type testClassType)
object testInstance = Activator.CreateInstance(testClassType);
MethodInfo setupMethod = testClassType
.GetMethods(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic)
.FirstOrDefault(m => m.GetCustomAttribute<TestInitializeAttribute>() != null);
MethodInfo[] testMethods = testClassType
.GetMethods(BindingFlags.Instance | BindingFlags.Public)
.Where(m => m.GetCustomAttribute<TestMethodAttribute>() != null)
int passed = 0, failed = 0;
foreach (var method in testMethods)
setupMethod?.Invoke(testInstance, null);
method.Invoke(testInstance, null);
Console.WriteLine($" ā
{method.Name} PASSED");
catch (TargetInvocationException ex)
Console.WriteLine($" ā {method.Name} FAILED: {ex.InnerException?.Message ?? ex.Message}");
Console.WriteLine($" ā ļø {method.Name} ERROR: {ex.Message}");
public class SentimentAnalyzerTests
private Dictionary<string, Dictionary<string, int>> _trainingData;
_trainingData = new Dictionary<string, Dictionary<string, int>>
"positive", new Dictionary<string, int>
"negative", new Dictionary<string, int>
public void ClassifyText_PositiveText_ReturnsPositive()
string text = "this is a good and great movie";
string sentiment = SentimentAnalyzer.ClassifyText(text, _trainingData);
Assert.AreEqual("positive", sentiment);
public void ClassifyText_NegativeText_ReturnsNegative()
string text = "this is a bad and terrible movie";
string sentiment = SentimentAnalyzer.ClassifyText(text, _trainingData);
Assert.AreEqual("negative", sentiment);
public void ClassifyText_NegativeText2_ReturnsNegative()
string text = "this movie is awful";
string sentiment = SentimentAnalyzer.ClassifyText(text, _trainingData);
Assert.AreEqual("negative", sentiment);
public void ClassifyText_UnknownWords_ReturnsPositive()
string text = "unknown words test";
string sentiment = SentimentAnalyzer.ClassifyText(text, _trainingData);
Assert.AreEqual("positive", sentiment);
public void ClassifyText_MixedWords_ReturnsPositive()
string text = "good bad unknown";
string sentiment = SentimentAnalyzer.ClassifyText(text, _trainingData);
Assert.AreEqual("positive", sentiment);
public void ClassifyText_EmptyText_ReturnsPositive()
string sentiment = SentimentAnalyzer.ClassifyText(text, _trainingData);
Assert.AreEqual("positive", sentiment);
public void ClassifyText_TextWithOnlySpaces_ReturnsPositive()
string sentiment = SentimentAnalyzer.ClassifyText(text, _trainingData);
Assert.AreEqual("positive", sentiment);