[英]Split the string C#
我想根據字符和字符串(如 ( ,
.
;
and
or
though
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
我試過這個。
string test = "This movie is great. I like the story, acting is nice and direction is perfect but music is not good.";
var splittC = Regex.Split(test, ",");
foreach(var a in splittC){
var splittD = Regex.Split(test, ".");
foreach(var b in splittD){
var splittA = Regex.Split(test, "and");
}
}// and so on....
它需要這么多循環。
如果此字符串中沒有逗號,則不會檢查其他字符。 如何解決這些問題。 請幫忙。
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);
解析自然語言很困難,因為計算機不理解上下文。 如果他們可以的話,我們可以像他們是人一樣與他們交談。
有時句子中的and和句號不是分隔符,有時句子不以大寫字母開頭。
史密斯先生說,iPhone 很棒。
“一加二,三加四。” 唱歌的音樂家。
為了做好這項工作,我建議你要么
(a) 非常嚴格地控制允許的輸入,或
(b) 使用自然語言解析庫,例如原生的 SharpNLP,或者您可以從 C# 調用 NLTK。 NLTK 可能是最好的,但有時也會失敗。 由於其機器學習所需的訓練數據,它的大小也為 5 GB。
要完成這項工作,您需要使用詞法分析器解析句子,然后處理生成的對象。 示例關鍵字詞法項是“and”、“,”等。然后可以連接關鍵字項之間的已解析項中的其余文本並發送到輸出。
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);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.