-6

I am doing project in c# and I am looking for the code which can help me to check either sentence is positive or negative or vague on the basis of emoticons.

For Example:

  1. I love my country :) - (Positive) because it contain happy smiley
  2. I love my country :( - (negative) because it contain sad smiley
  3. weather is good :( :) -(vague) because it contain two smileys so it is vague to tell either it is positive or negative.
  4. I don't want to go to College :( :) :) - (positive)because it contain two happy smileys and one sad.

My area of project is sentiment analysis.

  • python and java have libraries. you may need to write your own, see this q for ref: http://stackoverflow.com/questions/4199441/best-algorithmic-approach-to-sentiment-analysis – MatthewMartin Apr 07 '13 at 14:33
  • 3
    This isn't a real programming question, you're just asking how to do something as opposed to how to resolve an issue etc; have you tried solving this yourself? Have you even looked at any search results for "how to search a string"? – Clint Apr 07 '13 at 14:33
  • You could count the ')' and the '(' in order to estimate the positive factor. – Casperah Apr 07 '13 at 14:36

2 Answers2

2

Another regex ;)

string input = "I don't want to go to College :( :) :) ";

var score = Regex.Matches(input, @"(?<a>:\))|(?<b>:\()")
                 .Cast<Match>()
                 .Select(m => m.Groups["a"].Success ? 1 : -1)
                 .Sum();
I4V
  • 34,891
  • 6
  • 67
  • 79
1

Use Regex.Matches

var upScore = Regex.Matches(input, @":\)").Count;
var downScore = Regex.Matches(input, @":\(").Count;
var totalScore = upScore - downScore;

Although it's bad practice to use side effects in a MatchEvaluator, you could also use Regex.Replace to make a single pass through the string:

var score = 0;
MatchEvaluator match = m =>
{
    score += m.Value[1] == ')' ? 1 : -1;
    return m.Value;
};
Regex.Replace(input, ":[()]", match);
p.s.w.g
  • 146,324
  • 30
  • 291
  • 331