Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/336.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#_Algorithm_Linguistics - Fatal编程技术网

C# 从较长的字符串创建人类可读的短字符串

C# 从较长的字符串创建人类可读的短字符串,c#,algorithm,linguistics,C#,Algorithm,Linguistics,我有一个合同字符串的要求,如 你会考虑成为机器人吗?您将获得每年一次的免费换油。” …要短得多,但仍具有人的身份(需要从选择列表中找到它-我当前的解决方案允许用户输入任意标题以供选择) 我只想提取字符串中形成问题的部分(如果可能的话),然后以某种方式将其简化为 你会考虑成为一个机器人吗 有什么语法算法可以帮我解决这个问题吗?我想可能有什么东西可以让be只挑选动词和名词 因为这只是作为一个键,它不必是完美的;我并不是想淡化英语固有的复杂性。我不认为有任何算法可以识别字符串中的每个单词是名词、形容词

我有一个合同字符串的要求,如

你会考虑成为机器人吗?您将获得每年一次的免费换油。”

…要短得多,但仍具有人的身份(需要从选择列表中找到它-我当前的解决方案允许用户输入任意标题以供选择)

我只想提取字符串中形成问题的部分(如果可能的话),然后以某种方式将其简化为

你会考虑成为一个机器人吗

有什么语法算法可以帮我解决这个问题吗?我想可能有什么东西可以让be只挑选动词和名词


因为这只是作为一个键,它不必是完美的;我并不是想淡化英语固有的复杂性。

我不认为有任何算法可以识别字符串中的每个单词是名词、形容词还是其他什么。唯一的解决办法是使用自定义词典:只创建一个单词列表不能被识别为动词或名词(我、你、他们、他们、他的、她的、of、a、the等)

然后,你只需保留列表中没有的问号前的所有单词

这只是一个变通办法,我想你说过,这并不完美


希望这有帮助!

可能太简单了,但我可能想从一系列“填充词”开始:

然后在问号之前提取所有的东西(使用正则表达式,字符串散列,不管你想什么),让你“你会考虑成为一个机器人”吗? 然后遍历字符串,提取每个被认为是填充词的单词

var sentence = "Would you consider becoming a robot";
var newSentence = String.Join("",sentence.Split(" ").Where(w => !fillers.Contains(w)).ToArray());
// newSentence is "Wouldconsiderbecomingrobot".

Pascal将每个单词的大小写都会产生你想要的字符串-我将把它作为练习留给读者。

欢迎来到奇妙的世界。如果你想识别名词和动词,你需要一个。

创建一个流行的社交媒体网站。当用户想要加入或发表评论时,提示他们解决验证码。验证码将将长字符串的缩短版本与其完整版本进行匹配。缩短算法将基于神经网络或遗传算法,该算法根据capcha结果进行训练


你也可以在网站上销售广告。

我最终创建了以下扩展方法,效果出奇地好。感谢Joe Blow提出的出色而有效的建议:

    public static string Contract(this string e, int maxLength)
    {
        if(e == null) return e;

        int questionMarkIndex = e.IndexOf('?');
        if (questionMarkIndex == -1)
            questionMarkIndex = e.Length - 1;

        int lastPeriodIndex = e.LastIndexOf('.', questionMarkIndex, 0);

        string question = e.Substring(lastPeriodIndex != -1 ? lastPeriodIndex : 0, questionMarkIndex + 1).Trim();

        var punctuation =
            new [] {",", ".", "!", ";", ":", "/", "...", "...,", "-,", "(", ")", "{", "}", "[", "]","'","\""};

        question = punctuation.Aggregate(question, (current, t) => current.Replace(t, ""));

        IDictionary<string, bool> words = question.Split(' ').ToDictionary(x => x, x => false);

        string mash = string.Empty;
        while (words.Any(x => !x.Value) && mash.Length < maxLength)
        {
            int maxWordLength = words.Where(x => !x.Value).Max(x => x.Key.Length);
            var pair = words.Where(x => !x.Value).Last(x => x.Key.Length == maxWordLength);
            words.Remove(pair);
            words.Add(new KeyValuePair<string, bool>(pair.Key, true));
            mash = string.Join("", words.Where(x => x.Value)
                                       .Select(x => x.Key.Capitalize())
                                       .ToArray()
                );
        }

        return mash;
    }
publicstaticstringcontract(这个字符串是e,int-maxLength)
{
如果(e==null)返回e;
int questionMarkIndex=e.IndexOf(“?”);
如果(questionMarkIndex==-1)
questionMarkIndex=e.长度-1;
int lastPeriodIndex=e.LastIndexOf('.',questionMarkIndex,0);
字符串question=e.Substring(lastPeriodIndex!=-1?lastPeriodIndex:0,questionMarkIndex+1).Trim();
变量标点=
新的[]{“,”,“,”,“!”,“;”,“:”,“/”,“…,”,“-,”,“(“,”,“,”,“{”,“}”,“[”,“,”,“\”};
问题=标点符号。聚合(问题,(当前,t)=>当前。替换(t,”);
IDictionary words=question.Split(“”).ToDictionary(x=>x,x=>false);
stringmash=string.Empty;
while(words.Any(x=>!x.Value)&&mash.Length!x.Value).Max(x=>x.Key.Length);
var pair=words.Where(x=>!x.Value).Last(x=>x.Key.Length==maxWordLength);
字。删除(对);
添加(新的KeyValuePair(pair.Key,true));
mash=string.Join(“,words.Where(x=>x.Value)
.Select(x=>x.Key.Capitalize())
.ToArray()
);
}
返泥;
}
这将以下内容压缩为15个字符:

  • 这没有任何预要求-写一篇文章…:预要求写一篇文章
  • 您选择了一辆车:您选择了一辆车

这是一个很好的问题,它暴露了使用启发式与重人工智能的许多问题。好问题。@Joe Blow-谢谢你的夸奖-我以前从没想过这样的事。但是我已经做了很多年的编程了。为了添加到“填充词”列表中(更适合称之为连词),这里有一个网站,列出了一些常见的词:-
    public static string Contract(this string e, int maxLength)
    {
        if(e == null) return e;

        int questionMarkIndex = e.IndexOf('?');
        if (questionMarkIndex == -1)
            questionMarkIndex = e.Length - 1;

        int lastPeriodIndex = e.LastIndexOf('.', questionMarkIndex, 0);

        string question = e.Substring(lastPeriodIndex != -1 ? lastPeriodIndex : 0, questionMarkIndex + 1).Trim();

        var punctuation =
            new [] {",", ".", "!", ";", ":", "/", "...", "...,", "-,", "(", ")", "{", "}", "[", "]","'","\""};

        question = punctuation.Aggregate(question, (current, t) => current.Replace(t, ""));

        IDictionary<string, bool> words = question.Split(' ').ToDictionary(x => x, x => false);

        string mash = string.Empty;
        while (words.Any(x => !x.Value) && mash.Length < maxLength)
        {
            int maxWordLength = words.Where(x => !x.Value).Max(x => x.Key.Length);
            var pair = words.Where(x => !x.Value).Last(x => x.Key.Length == maxWordLength);
            words.Remove(pair);
            words.Add(new KeyValuePair<string, bool>(pair.Key, true));
            mash = string.Join("", words.Where(x => x.Value)
                                       .Select(x => x.Key.Capitalize())
                                       .ToArray()
                );
        }

        return mash;
    }