我想根据字符和字符串(,
.
;
and
or
though
{{来拆分字符串1}}等。)
原始字符串:but
结果:
"This movie is great. I like the story, acting is nice and direction is perfect but music is not good."
This movie is great
I like the story
acting is nice
direction is perfect
我试过了。
music is not good
这需要很多循环 如果此字符串中没有逗号,则不会检查其他字符。如何解决这些问题。请帮忙。
答案 0 :(得分:1)
String.Split
允许string[]
参数。
试试这个:
string test = "This movie is great. I like the story, acting is nice and direction is perfect but music is not good.";
var splitVals = test.Split(new string[] { ",", ".", ";", " and ", " or ", " though ", " but ", " etc. "}, StringSplitOptions.RemoveEmptyEntries);
答案 1 :(得分:1)
解析自然语言很难,因为计算机不理解上下文。如果可以的话,我们可以像对待他们一样与他们交谈。
有时句子中的ands和句点不是分隔符,有时句子不是以大写字母开头的。
史密斯先生说,iPhone很棒。“一,二,三,四。”唱着音乐家。
为了做好这份工作,我建议你
(a)非常严格地控制允许的输入,或
(b)使用自然语言解析库,例如本机SharpNLP,或者您可以从C#调用NLTK。 NLTK可能是最好的,但有时甚至会失败。由于机器学习所需的训练数据,它的大小也是5 GB。
答案 2 :(得分:0)
要完成这项工作,您需要使用词法分析器解析句子,然后处理生成的对象。示例关键字词汇项是“和”,“,”等。关键字项之间的解析项中的其余文本可以连接并发送到输出。
答案 3 :(得分:0)
尝试使用这个简单的正则表达式我写道它可能对你有所帮助:
var splitRegex=@"\.|\,|\;|(?:\sand\s)|(?:\sor\s)|(?:\sthough\s)|(?:\sbut\s)";
var splittC = Regex.Split(test, splitRegex);
...
答案 4 :(得分:0)
string test = "This movie is great. I like the story, acting is nice and direction is perfect but music is not good.";
var splitVals = test.Split(new string[]
{ ",", ".", ";", " and ", " or ",
" though ", " but ", " etc. "
},StringSplitOptions.RemoveEmptyEntries);