あなたはLinq.Aggregate
n個を使用することができます - それはのをどのように動作するかの説明のためのコードとコード出力のコメントを参照してください。
"Bla。blubb"も尊重します。 "。?!"の後の空白をチェックする必要があります。
using System;
using System.Linq;
internal class Program
{
static string Capitalize(string oldSentence)
{
return
// this will look at oldSentence char for char, we start with a
// new string "" (the accumulator, short acc)
// and inspect each char c of oldSentence
// comment all the Console.Writelines in this function, thats
// just so you see whats done by Aggregate, not needed for it to
// work
oldSentence
.Aggregate("", (acc, c) =>
{
System.Console.WriteLine("Accumulated: " + acc);
System.Console.WriteLine("Cecking: " + c);
// if the accumulator is empty or the last character of
// trimmed acc is a ".?!" we append the
// upper case of c to it
if (acc.Length == 0 || ".?!".Any(p => p == acc.Trim().Last())) // (*)
acc += char.ToUpper(c);
else
acc += c; // else we add it unmodified
System.Console.WriteLine($"After: {acc}\n");
return acc; // this returns the acc for the next iteration/next c
});
}
static void Main(string[] args)
{
Console.SetBufferSize(120, 1000);
var oldSentence = "This is a testSentence. some occurences "
+ "need capitalization! for examlpe here. or here? maybe "
+ "yes, maybe not.";
var newSentence = Capitalize(oldSentence);
Console.WriteLine(new string('*', 80));
Console.WriteLine(newSentence);
Console.ReadLine();
}
}
(*)
".?!".Any(p => p == ...))
手段は".?!"
が
acc.Trim().Last()
...
は意味等しい任意の文字を含む文字列を行いますacc
の終わりに/前の空白を削除し、私の最後を与えます文字
.Last()
および.Any()
もLinqです。 LINQの-ESC延長のほとんどは、ここで見つけることができます:https://msdn.microsoft.com/en-us/library/9eekhta0(v=vs.110).aspx
出力(切り取ら - そのかなり長め; O)
Accumulated:
Cecking: T
After: T
Accumulated: T
Cecking: h
After: Th
Accumulated: Th
Cecking: i
After: Thi
Accumulated: Thi
Cecking: s
After: This
Accumulated: This
Cecking:
After: This
Accumulated: This
Cecking: i
After: This i
Accumulated: This i
Cecking: s
After: This is
<snipp - .. you get the idea how Aggregate works ...>
Accumulated: This is a testSentence.
Cecking: s
After: This is a testSentence. S
<snipp>
Accumulated: This is a testSentence. Some occurences need capitalization!
Cecking: f
After: This is a testSentence. Some occurences need capitalization! F
<snipp>
********************************************************************************
This is a testSentence. Some occurences need capitalization! For examlpe here. Or here? Maybe yes, maybe not.
「私はそれが正しいように見えることはできません。」 - いくつかの詳細が便利です。あなたのコードの問題は何ですか? – CodeFuller
アルゴリズムには少なくとも1つの概念上の問題があります。句読記号の次の文字が2文字離れていない場合はどうなりますか?句読点の後に2つのスペースがあっても、改行文字であってもかまいません。 –
これは宿題のように聞こえますが、文識別のパラメータがわかりますか?それは難しいからです。私がそれをしなければならなかったとき、私はMicrosoft Cognitive Servicesにその文章を発送し、それを私のために分割させました。 – Crowcoder