Saturday, August 31, 2013

How to calculate sentiment score of content faster in c#

Open the connection outside of the foreach




MySqlCommand cmd = new MySqlCommand("select * from avg_scores where Word='" + word + "'", con);
con.Open();
MySqlDataReader r = cmd.ExecuteReader();
foreach (string word in postContent)
{
if (r.Read())
{
this.TempDiff = Math.Abs(Convert.ToDouble(r["Pos"]) - Convert.ToDouble(r["Neg"]));
double tempPos = Convert.ToDouble(r["Pos"]);
double tempNeg = Convert.ToDouble(r["Neg"]);
if (!(tempPos==0.0 && tempNeg==0.0))
{
if (tempPos != 0.0 && tempNeg == 0.0)
{
this.PosScore = this.PosScore + tempPos;
this.PosWordCounter++;
}
if (tempPos == 0.0 && tempNeg != 0.0)
{
this.NegScore = this.NegScore + tempPos;
this.NegWordCounter++;
}
if (this.TempDiff>0.1)
{

if (tempPos>tempNeg)
{
this.PosScore = this.PosScore + tempPos;
this.PosWordCounter++;
}
else
{
this.NegScore = this.PosScore + tempNeg;
this.NegWordCounter++;
}
}
else
{
this.PosScore = this.PosScore + tempPos;
this.NegScore = this.NegScore + tempNeg;
this.ContradictoryWordsCounter++;
if (tempPos > 0.1)
this.PosWordCounter++;
if (tempNeg > 0.1)
this.NegWordCounter++;
}
}

}

}
con.Close();


Avoid use the * on the sql query, use the name of fields;





João Antonio Marques





No comments:

Post a Comment