This is the html string :
string htmlString = "<body lang=\"EN-US\" link=\"blue\" vlink=\"#954F72\"><div class=\"WordSection1\"><p class=\"MsoNormal\">Hi, </p><p class=\"MsoNormal\"><o:p> </o:p></p><p class=\"MsoNormal\"><o:p> </o:p></p><p class=\"MsoNormal\">My name is Gaurav Illness.</p><p class=\"MsoNormal\"><span style=\"color:purple !important\">Today <b>MY relation</b>ship breakdown <span style=\"color:red\">happened?<o:p></o:p></span> </span></p><p class=\"MsoNormal\"><span style=\"color:red\"><o:p> </o:p></span></p><p class=\"MsoNormal\"><span style=\"color:red\">I am Gr</span><span style=\"font-size:15.0pt;color:red;background:yellow;mso-highlight:yellow\">iESh and I</span><span style=\"font-size:15.0pt;color:red\"><o:p></o:p></span></p><p class=\"MsoNormal\"><span style=\"font-size:15.0pt;color:#B4C7E7;mso-style-textfill-fill-color:#B4C7E7;mso-style-textfill-fill-alpha:100.0%\">Am drugger.<o:p></o:p></span></p><p class=\"MsoNormal\"><o:p> </o:p></p><p class=\"MsoNormal\" style=\"line-height:16.5pt\"><span style=\"font-size:10.0pt;font-family:"Arial",sans-serif;color:#1F497D\">Thanks<span style=\"text-transform:uppercase\">,<o:p></o:p>"
I am Extracting Plain text from this using this function :
private static string extractTextFromHtml(string htmlString)
{
// Remove new lines since they are not visible in HTML
html = html.Replace("\n", " ");
// Remove tab spaces
html = html.Replace("\t", " ");
// Remove multiple white spaces from HTML
html = Regex.Replace(html, "\\s+", " ");
// Remove HEAD tag
html = Regex.Replace(html, "<head.*?</head>", ""
, RegexOptions.IgnoreCase | RegexOptions.Singleline);
// Remove any JavaScript
html = Regex.Replace(html, "<script.*?</script>", ""
, RegexOptions.IgnoreCase | RegexOptions.Singleline);
// Replace special characters like &, <, >, " etc.
StringBuilder sbHTML = new StringBuilder(html);
// Note: There are many more special characters, these are just
// most common. You can add new characters in this arrays if needed
string[] OldWords = {" ", "&", """, "<", ">", "®", "©", "•", "™"};
string[] NewWords = { " ", "&", "\"", "<", ">", "®", "©", "•", "™" };
for (int i = 0; i < OldWords.Length; i++)
{
sbHTML.Replace(OldWords[i], NewWords[i]);
}
// Check if there are line breaks (<br>) or paragraph (<p>)
sbHTML.Replace("<br>", "\n<br>");
sbHTML.Replace("<br ", "\n<br ");
sbHTML.Replace("<p ", "\n<p ");
// Finally, remove all HTML tags and return plain text
return System.Text.RegularExpressions.Regex.Replace(
sbHTML.ToString(), "<[^>]*>", "");
}
This function returns :
"Hi,
My name is Gaurav Illness. Today MY relationship breakdown happened?
I am GriESh and I Am drugger.
Thanks,"
Now I send this Text to an API that detects weather there is an emotion or not in these sentences. The API gives a response of all the sentences which are emotional. For example, API says "Today MY relationship breakdown happened?" is emotional. Now I want to mark this sentence as purple color in the html for which I have to add a span around the sentence. To do this I have to find the start and end index of this sentence in the html code.
How can I find the start and end index of this sentence in the html code?
I have a code which gives me the indexes but I think it is not the best way to do. Can anyone suggest a better way? This is my code example :
public static void findTextInHtml(string htmlCode)
{
string textToBeFind = "I am GriESh and IAm drugger.";
int i = 0;
int j = 0;
int startIndex = 0;
int endIndex = 0;
bool isHtml = false;
bool isbeingMatched = false;
while (i < htmlCode.Length)
{
if (htmlCode[i] == '<')
{
isHtml = true;
i++;
continue;
}
if (htmlCode[i] == '>')
{
isHtml = false;
i++;
continue;
}
if (isHtml)
{
i++;
continue;
}
if (textToBeFind[j] == htmlCode[i])
{
if (!isbeingMatched)
{
startIndex = i;
}
isbeingMatched = true;
j++;
if (j == textToBeFind.Length)
{
endIndex = i;
break;
}
}
else
{
isbeingMatched = false;
j = 0;
}
i++;
}
AddStartSpan(startIndex, htmlCode);
AddEndSpan(endIndex, htmlCode);
}
Install the nuget package HtmlAgilityPack
Then its easy to parse like this:
string htmlString = "<p class=\"MsoNormal\"><span style=\"color:red\"><o:p> </o:p></span></p><p class=\"MsoNormal\"><span style=\"color:red\">I am Gr</span><span style=\"font-size:15.0pt;color:red;background:yellow;mso-highlight:yellow\">iESh and I</span><span style=\"font-size:15.0pt;color:red\"><o:p></o:p></span></p><p class=\"MsoNormal\"><span style=\"font-size:15.0pt;color:#B4C7E7;mso-style-textfill-fill-color:#B4C7E7;mso-style-textfill-fill-alpha:100.0%\">Am drugger.<o:p></o:p></span></p>";
var doc = new HtmlDocument();
doc.LoadHtml(htmlString);
var inner = doc.DocumentNode.InnerText.TrimStartString(" ");
// inner = I am GriESh and IAm drugger.
To remove the nbsp;
at the start of the InnerText
public static string TrimStartString(this string input, string prefixToRemove,
StringComparison comparisonType = StringComparison.OrdinalIgnoreCase)
{
if (input != null && prefixToRemove != null
&& input.StartsWith(prefixToRemove, comparisonType))
{
return input.Substring(prefixToRemove.Length);
}
else return input;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.