using System.Collections.Generic;
using Microsoft.VisualStudio.TestTools.UnitTesting;
public class SentimentAnalyzer
public static string ClassifyText(string text, Dictionary<string, Dictionary<string, int>> trainingData)
if (string.IsNullOrEmpty(text))
throw new ArgumentException("Text cannot be null or empty.", nameof(text));
if (trainingData == null || trainingData.Count == 0)
throw new ArgumentException("Training data cannot be null or empty.", nameof(trainingData));
if (!trainingData.ContainsKey("positive") || !trainingData.ContainsKey("negative"))
throw new ArgumentException("Training data must contain 'positive' and 'negative' sentiments.", nameof(trainingData));
double positiveProbability = CalculateSentimentProbability("positive", text, trainingData);
double negativeProbability = CalculateSentimentProbability("negative", text, trainingData);
return positiveProbability >= negativeProbability ? "positive" : "negative";
private static double CalculateSentimentProbability(string sentiment, string text, Dictionary<string, Dictionary<string, int>> trainingData)
double logProbability = Math.Log(0.5);
string[] words = text.Split(' ');
foreach (string word in words)
if (trainingData[sentiment].ContainsKey(word))
int wordCountInSentiment = trainingData[sentiment][word];
int totalWordsInSentiment = trainingData[sentiment].Values.Sum();
logProbability += Math.Log((double)wordCountInSentiment / totalWordsInSentiment);
public static void Main(string[] args)
var trainingData = new Dictionary<string, Dictionary<string, int>>
"positive", new Dictionary<string, int>
"negative", new Dictionary<string, int>
string text1 = "this is a good and great movie";
string sentiment1 = ClassifyText(text1, trainingData);
Console.WriteLine($"'{text1}' is classified as: {sentiment1}");
string text2 = "this is a bad and terrible movie";
string sentiment2 = ClassifyText(text2, trainingData);
Console.WriteLine($"'{text2}' is classified as: {sentiment2}");
public class SentimentAnalyzerTests
private readonly Dictionary<string, Dictionary<string, int>> _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_MixedText_ReturnsCorrectSentiment()
string text = "this is a good bad and awesome movie";
string sentiment = SentimentAnalyzer.ClassifyText(text, _trainingData);
Assert.AreEqual("positive", sentiment);
public void ClassifyText_UnknownWords_ReturnsCorrectSentiment()
string text = "this movie is amazing";
string sentiment = SentimentAnalyzer.ClassifyText(text, _trainingData);
Assert.AreEqual("positive", sentiment);
[ExpectedException(typeof(ArgumentException))]
public void ClassifyText_EmptyText_ThrowsArgumentException()
SentimentAnalyzer.ClassifyText("", _trainingData);
[ExpectedException(typeof(ArgumentException))]
public void ClassifyText_NullText_ThrowsArgumentException()
SentimentAnalyzer.ClassifyText(null, _trainingData);
[ExpectedException(typeof(ArgumentException))]
public void ClassifyText_NullTrainingData_ThrowsArgumentException()
SentimentAnalyzer.ClassifyText("good movie", null);
[ExpectedException(typeof(ArgumentException))]
public void ClassifyText_EmptyTrainingData_ThrowsArgumentException()
SentimentAnalyzer.ClassifyText("good movie", new Dictionary<string, Dictionary<string, int>>());
[ExpectedException(typeof(ArgumentException))]
public void ClassifyText_MissingPositiveSentiment_ThrowsArgumentException()
var trainingData = new Dictionary<string, Dictionary<string, int>>
"negative", new Dictionary<string, int>
SentimentAnalyzer.ClassifyText("good movie", trainingData);
[ExpectedException(typeof(ArgumentException))]
public void ClassifyText_MissingNegativeSentiment_ThrowsArgumentException()
var trainingData = new Dictionary<string, Dictionary<string, int>>
"positive", new Dictionary<string, int>
SentimentAnalyzer.ClassifyText("good movie", trainingData);