using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
namespace textProcessorMap
{
public class Program
{
public static string tagAdder(string text, string startTag, string endTag, int startIndex, int lengthWord)
{
text = text.Insert(startIndex, startTag);
int tagStartLength = startTag.Length;
int endIndex = startIndex + lengthWord + tagStartLength;
int tagEndLength = endTag.Length;
text = text.Insert(endIndex, endTag);
return text;
//int endReserved = startIndex + tagStartLength + lengthWord + tagEndLength;
}
static void Main(string[] args)
{
//List<int> reservedStart = new List<int>(new int[] { 0 });
//List<int> reservedEnd = new List<int>(new int[] { 0 });
List<int> reservedStart = new List<int>();
List<int> reservedEnd = new List<int>();
string text = "Zennoposter it's a great piece of software but without proper documentation little tasks take alot of time and instead of coding some amazing bots I struggle understanding the meaning of the english words. Like every other great software it will fade if it's not properly maintained by its community as history repeats itself.";
List<string> keywords = new List<string>(new string[] { "bbb", "zennoposter", "bots", "software" });
string link = "http://zennolab.com/en/";
string link2 = "http://zennolab.com/ru/";
string linkStart = "<a href\"=" + link + "\">";
string linkEnd = "</a>";
string linkStart2 = "<a href\"=" + link2 + "\">";
int tagCounter = 0;
bool ok = true;
foreach (var k in keywords)
{
MatchCollection foundItems = Regex.Matches(text, $@"\b{k}\b", RegexOptions.IgnoreCase);
var countItems = foundItems.Count;
if (countItems > 0)
{
if (tagCounter == 0) //main link adding
{
foreach (Match cucu in foundItems)
{
ok = true;
int startR = cucu.Index;
//int endR = foundItems[rndKey].Index + linkEnd.Length;
for (int r = 0; r < reservedStart.Count; r++)
{
if (startR >= reservedStart[r] && startR <= reservedEnd[r])
{
ok = false;
break;
}
}
if (ok == true)
{
text = tagAdder(text, linkStart, linkEnd, cucu.Index, cucu.Length);
reservedStart.Add(cucu.Index);
int endR = cucu.Index + linkStart.Length + cucu.Length + linkEnd.Length;
reservedEnd.Add(endR);
tagCounter++;
ok = true;
break;
}
//tagAdder(text, linkStart, linkEnd, foundItems[rndKey].Index, foundItems[rndKey].Length);
//reservedStart.Add(foundItems[rndKey].Index);
//reservedEnd.Add(endR);
}
}
else if (tagCounter == 1)
{
foreach (Match cucu in foundItems)
{
ok = true;
int startR = cucu.Index;
//int endR = foundItems[rndKey].Index + linkEnd.Length;
for (int r = 0; r < reservedStart.Count; r++)
{
if (startR >= reservedStart[r] && startR <= reservedEnd[r])
{
ok = false;
break;
}
}
if (ok == true)
{
text = tagAdder(text, linkStart2, linkEnd, cucu.Index, cucu.Length);
reservedStart.Add(cucu.Index);
int endR = cucu.Index + linkStart.Length + cucu.Length + linkEnd.Length;
reservedEnd.Add(endR);
tagCounter++;
break;
}
}
}
}
}
Console.WriteLine(text);
}
}
}