Saturday, March 28, 2009

Find Slang Word and Highlight Them

Xml File :XMLAbuseFile.xml


"NewDataSet"
"tblGeneric"
"UTLName" toy "UTLName"
"/tblGeneric"
"tblGeneric"
"UTLName" utl "/UTLName"
"/tblGeneric"
"tblGeneric"
"UTLName" go "UTLName"
"/tblGeneric"
"/NewDataSet"


Class Module : clsSlangWord.cs
Namespace :
using System.Xml;
using System.Xml.XPath;
using System.Text.RegularExpressions;
using System.Text;

#region Find and HighLight Slang Word
public static string GetslangWord(string strInfo)
{
StringBuilder objsb = new StringBuilder();

XPathDocument doc = new XPathDocument(HttpContext.Current.Server.MapPath("UploadedFiles/XMLFIle/XMLAbuseFile.xml"));
XPathNavigator nav = doc.CreateNavigator();
// Compile a standard XPath expression
XPathExpression expr;
string[] strInfoArr = strInfo.Trim().Split(' ');
for (int arr = 0; arr < strInfoArr.Length; arr++)
{
expr = nav.Compile("/NewDataSet/tblGeneric[UTLName='" + strInfoArr[arr].ToString().ToLower() + "']");
XPathNodeIterator iterator = nav.Select(expr);
// Iterate on the node set
while (iterator.MoveNext())
{
XPathNavigator nav2 = iterator.Current.Clone();
if (strInfoArr[arr].ToString().ToLower() == nav2.Value.ToString().ToLower())
{
objsb.Append(strInfoArr[arr].ToString());
objsb.Append(" ");
}
}
}
return objsb.ToString();

}
public static string ReplaceSlang(string strSlangWord, string strInfo)
{
string[] strSlangArr = strSlangWord.Trim().Split(' ');
string strInputInfo = strInfo;
if (strInputInfo.Length > 0)
{
for (int arr = 0; arr < strSlangArr.Length; arr++)
{
strInputInfo = strInputInfo.Replace(strSlangArr[arr].ToString(), "*");
}
}
return strInputInfo;
}
public static string HighLightSlangWord(string strSlangWord, string strInfo)
{
string[] strSlangArr = strSlangWord.Trim().Split(' ');
string strInputInfo = strInfo;
if (strInputInfo.Length > 0)
{
for (int arr = 0; arr < strSlangArr.Length; arr++)
{
strInputInfo = Highlight(strSlangArr[arr], strInputInfo);

}
}
return strInputInfo;
}

//Highlight(txtSearchKeyword.Value, str_FullText)
public static string Highlight(string Search_Str, string InputTxt)
{
// Setup the regular expression and add the Or operator.

Regex reg = new Regex("[^A-Za-z0-9]");

Search_Str = reg.Replace(Search_Str, " ");

Regex RegExp = new Regex(Search_Str.Replace(" ", "|").Trim(), RegexOptions.IgnoreCase);

// Highlight keywords by calling the delegate each time a keyword is found.

// return RegExp.Replace(InputTxt, new MatchEvaluator(ReplaceKeyWords));

string sret = RegExp.Replace(InputTxt, new MatchEvaluator(ReplaceKeyWords));

return sret;

// Set the RegExp to null.

RegExp = null;

}
public static string ReplaceKeyWords(Match m)
{
return "" + m.Value + "";
return "span style='background-color: #ffff00'" + m.Value + "/span";
}

#endregion


Page Code : cs file
protected string strSlangWordCollection = "";

private Boolean ManageSlangWord()
{
bool Flag = false;
string strInputInfo = FCKeditorDecs.Value.Trim();
strInputInfo = Regex.Replace(strInputInfo, "<.*?>", string.Empty);
if (strInputInfo != "")
{
string strSlangWord = clsSlangWord.GetslangWord(strInputInfo);
if (strSlangWord.Length > 0)
{
//string strReplaceSlangWord = ReplaceSlang(strSlangWord, strName);
strSlangWordCollection = strSlangWordCollection + " " + strSlangWord;
string strHighlightSlangWord = clsSlangWord.HighLightSlangWord(strSlangWord, strInputInfo);
//txtName.Text = strHighlightSlangWord;
//lbl.Text = strHighlightSlangWord;
FCKeditorDecs.Value = strHighlightSlangWord;
Flag = true;
}
}
return Flag;
}

Btn Submit Code:

bool Flag = false;
msg_Slang.InnerHtml = "";
if (Convert.ToString(Session["UserID"]) != "")
{
Flag = ManageSlangWord();
if (Flag == false)
{
ManageComments();
}
else
{
msg_Slang.InnerHtml = "You can't use these slang words '" + strSlangWordCollection + " '";
}
}