Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/280.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 动态生成指向页面的链接,并使用';链接到文本片段';插件_C# - Fatal编程技术网

C# 动态生成指向页面的链接,并使用';链接到文本片段';插件

C# 动态生成指向页面的链接,并使用';链接到文本片段';插件,c#,C#,我正在尝试生成一个函数,该函数可以基于HTML页面的文本链接片段生成文本链接URL 以编程方式生成指向要突出显示的段落的链接。 我相信可能有一些更好的算法已经可用,这样我就不必再重新发明轮子了 public string GetTextHighLighterUrl(string firstValidFileUrl, string answer) { var sb = new StringBuilder(); var textHighLighterUrl = string.Empty; var h

我正在尝试生成一个函数,该函数可以基于HTML页面的文本链接片段生成文本链接URL

以编程方式生成指向要突出显示的段落的链接。

我相信可能有一些更好的算法已经可用,这样我就不必再重新发明轮子了

public string GetTextHighLighterUrl(string firstValidFileUrl, string answer)
{
var sb = new StringBuilder();
var textHighLighterUrl = string.Empty;
var highlightText = answer.Replace(firstValidFileUrl, "");
highlightText = highlightText.Replace("()", "");
highlightText = $"{Regex.Replace(highlightText, @"[[^]]*]", "")}"; // remove text within square brackets.
highlightText = $"{Regex.Replace(highlightText, @"\t|\r", "$$")}";
highlightText = RemoveExtraSentences(highlightText, "$$").Trim();
highlightText = highlightText.Replace("$$", "");
highlightText = FilterWhiteSpaces(highlightText);
highlightText = highlightText.Replace("$", "");
highlightText = highlightText.Replace("(/)", "");
highlightText = highlightText.Replace(""", "%22");
highlightText = highlightText.Replace(",", "%2C");

        if (!ContainsOnlyAlphaNumericCharacters(highlightText))
        {
            //remove sentences with special char and get a safe sentence
            highlightText = GetCleanSentence(highlightText);
        }


        if (highlightText.Length > 50)
        {
            var textArrayn = highlightText.Split("\n"); //if ans contains \n
            if (highlightText.Contains("\n") && textArrayn?.Length == 2)
            {
                textArrayn = highlightText.Split("\n");
            }
            else
            {
                textArrayn = new List<string>().ToArray();
            }
            var textArray = highlightText.Split();
            var n = textArray.Length;
            sb.Clear();
            for (int i = 0; i < 5; i++)
            {
                if (IsValidIndex(i, textArray))
                {
                    sb = sb.Append(textArray[i] + "%20");
                }
            }
            var start = sb.ToString();
            if (start.Contains(","))
            {
                var tempStart = start.Split(",");
                start = tempStart[0].ToString();
            }


            if (textArrayn.Length > 0)
            {
                start = textArrayn[0].ToString();  //if ans contains \n then start from there
            }
            sb.Clear();
            for (int i = n - 6; i <= n; i++)
            {
                if (IsValidIndex(i, textArray))
                {
                    if (i != n - 1)
                    {
                        sb = sb.Append(textArray[i] + "%20");
                    }
                    else
                    {
                        sb = sb.Append(textArray[i]);
                    }

                }
            }
            var end = sb.ToString().Trim();
            end = end.Replace(",", "%2C");
            sb.Clear();
            sb = sb.Append(firstValidFileUrl).Append("#object:~:text=").Append(start).Append(",").Append(end);
            textHighLighterUrl = sb.ToString();
            sb.Clear();
        }
        else
        {
            sb.Clear();
            highlightText = highlightText.Replace(" ", "%20");
            if (!string.IsNullOrEmpty(highlightText))
            {
                sb = sb.Append(firstValidFileUrl).Append("#object:~:text=").Append(highlightText);
            }
            else
            {
                sb = sb.Append(firstValidFileUrl);
            }

            textHighLighterUrl = sb.ToString();
            sb.Clear();
        }
        textHighLighterUrl = $"{Regex.Replace(textHighLighterUrl, @"\.{2,}", "")}";//edge cases.Remove Multiple dots
        textHighLighterUrl = textHighLighterUrl.Replace(",%20,", ",");

        return textHighLighterUrl;
    }
例如,创建一个C#函数,该函数接受URL和段落(要突出显示),并返回文本HighlighterUrl

[Fact]
        public void GettextHighlighterURL()
        {
            string initialFileUrl = "https://docs.microsoft.com/en-us/azure/cognitive-services/cognitive-services-and-machine-learning";
            string answer = "Machine learning is a concept where you bring together data and an algorithm to solve a specific need. Once the data and algorithm are trained, the output is a model that you can use again with different data. The trained model provides insights based on the new data."+
                 "[Machine Learning](https://docs.microsoft.com/en-us/azure/cognitive-services/cognitive-services-and-machine-learning)";
            string actual = HighlighterHelper.GetTextHighLighterUrl(initialFileUrl, answer);
            string expected= "https://docs.microsoft.com/en-us/azure/cognitive-services/cognitive-services-and-machine-learning#object:~:text=Machine%20learning%20is%20a%20concept%20,insights%20based%20on%20the%20new%20data."
            Assert.Equal(expected, actual);
        }
下面的帖子是我到目前为止尝试过的,它只适用于10%的案例。我怎样才能从这里提高?我相信可能有一些更好的算法已经可用,这样我就不必再重新发明轮子了

public string GetTextHighLighterUrl(string firstValidFileUrl, string answer)
{
var sb = new StringBuilder();
var textHighLighterUrl = string.Empty;
var highlightText = answer.Replace(firstValidFileUrl, "");
highlightText = highlightText.Replace("()", "");
highlightText = $"{Regex.Replace(highlightText, @"[[^]]*]", "")}"; // remove text within square brackets.
highlightText = $"{Regex.Replace(highlightText, @"\t|\r", "$$")}";
highlightText = RemoveExtraSentences(highlightText, "$$").Trim();
highlightText = highlightText.Replace("$$", "");
highlightText = FilterWhiteSpaces(highlightText);
highlightText = highlightText.Replace("$", "");
highlightText = highlightText.Replace("(/)", "");
highlightText = highlightText.Replace(""", "%22");
highlightText = highlightText.Replace(",", "%2C");

        if (!ContainsOnlyAlphaNumericCharacters(highlightText))
        {
            //remove sentences with special char and get a safe sentence
            highlightText = GetCleanSentence(highlightText);
        }


        if (highlightText.Length > 50)
        {
            var textArrayn = highlightText.Split("\n"); //if ans contains \n
            if (highlightText.Contains("\n") && textArrayn?.Length == 2)
            {
                textArrayn = highlightText.Split("\n");
            }
            else
            {
                textArrayn = new List<string>().ToArray();
            }
            var textArray = highlightText.Split();
            var n = textArray.Length;
            sb.Clear();
            for (int i = 0; i < 5; i++)
            {
                if (IsValidIndex(i, textArray))
                {
                    sb = sb.Append(textArray[i] + "%20");
                }
            }
            var start = sb.ToString();
            if (start.Contains(","))
            {
                var tempStart = start.Split(",");
                start = tempStart[0].ToString();
            }


            if (textArrayn.Length > 0)
            {
                start = textArrayn[0].ToString();  //if ans contains \n then start from there
            }
            sb.Clear();
            for (int i = n - 6; i <= n; i++)
            {
                if (IsValidIndex(i, textArray))
                {
                    if (i != n - 1)
                    {
                        sb = sb.Append(textArray[i] + "%20");
                    }
                    else
                    {
                        sb = sb.Append(textArray[i]);
                    }

                }
            }
            var end = sb.ToString().Trim();
            end = end.Replace(",", "%2C");
            sb.Clear();
            sb = sb.Append(firstValidFileUrl).Append("#object:~:text=").Append(start).Append(",").Append(end);
            textHighLighterUrl = sb.ToString();
            sb.Clear();
        }
        else
        {
            sb.Clear();
            highlightText = highlightText.Replace(" ", "%20");
            if (!string.IsNullOrEmpty(highlightText))
            {
                sb = sb.Append(firstValidFileUrl).Append("#object:~:text=").Append(highlightText);
            }
            else
            {
                sb = sb.Append(firstValidFileUrl);
            }

            textHighLighterUrl = sb.ToString();
            sb.Clear();
        }
        textHighLighterUrl = $"{Regex.Replace(textHighLighterUrl, @"\.{2,}", "")}";//edge cases.Remove Multiple dots
        textHighLighterUrl = textHighLighterUrl.Replace(",%20,", ",");

        return textHighLighterUrl;
    }
public string GetTextHighLighterUrl(string firstValidFileUrl,string answer)
{
var sb=新的StringBuilder();
var textHighLighterUrl=string.Empty;
var highlightText=answer.Replace(firstvalidfluerl,“”);
highlightText=highlightText.Replace(“()”,”);
highlightText=$“{Regex.Replace(highlightText,@“[^]]*]”,“”)}”;//删除方括号内的文本。
highlightText=$“{Regex.Replace(highlightText,@“\t|\r”,“$$”)}”;
highlightText=RemoveExtramentes(highlightText,“$$”).Trim();
highlightText=highlightText.Replace(“$$”,”);
highlightText=过滤器白色空间(highlightText);
highlightText=highlightText.Replace(“$”,”);
highlightText=highlightText.Replace((/),“”);
highlightText=highlightText.Replace(“”,“%22”);
highlightText=highlightText.Replace(“,”,“%2C”);
如果(!只包含字母数字字符(highlightText))
{
//删除带有特殊字符的句子,得到一个安全的句子
highlightText=GetCleanStence(highlightText);
}
如果(highlightText.Length>50)
{
var textArrayn=highlightText.Split(“\n”);//如果ans包含\n
if(highlightText.Contains(“\n”)&&textArrayn?.Length==2)
{
textArrayn=highlightText.Split(“\n”);
}
其他的
{
textArrayn=新列表().ToArray();
}
var textary=highlightText.Split();
var n=textArray.Length;
(某人清楚地);
对于(int i=0;i<5;i++)
{
if(IsValidIndex(i,textArray))
{
sb=sb.Append(textArray[i]+%20”);
}
}
var start=sb.ToString();
if(start.Contains(“,”))
{
var tempStart=start.Split(“,”);
start=tempStart[0]。ToString();
}
如果(textArrayn.Length>0)
{
start=textArrayn[0].ToString();//如果ans包含\n则从那里开始
}
(某人清楚地);

对于(int i=n-6;i我不知道是否完全理解您试图实现的目标,但请尝试一下以下代码:

给定您的输入数据,它将返回此链接:

使用System.Web;
//#:~:text=[前缀-,]textStart[,textsend][,-后缀]
私有字符串Go(字符串url、字符串文本)
{
int段_大小=20;
//拆下最后一个支架后的所有部件
text=Regex.Replace(text,@“\[(?:.(?!\[)+$”,“”);
串链;
如果(文本长度<100)
{
link=$“{url}}:~:text={Uri.EscapeUriString(text)}”;
}
其他的
{
var startBreak=text.IndexOf(“”,段大小);
var endBreak=text.Substring(0,text.Length-SEGMENT\u SIZE).LastIndexOf(“”);
link=$“{url}#:~:text={Uri.EscapeUriString(text.Substring(0,startBreak))},{Uri.EscapeUriString(text.Substring(endBreak))}”;
}
返回链接;
}

您需要使用最少的数据创建一些测试,以显示您想要实现的目标,因为我发现很难理解输入是什么,或者您希望输出是什么be@MichalCiechan添加了一个测试用例