住所、数字、郵便番号/郵便番号、国、電話番号、電子メールアドレスなどの単語/段落の数と特定の構文で指定されたランダムな単語、文、段落を生成できるac#ジェネレーターを探しています。
10 に答える
static string LoremIpsum(int minWords, int maxWords,
int minSentences, int maxSentences,
int numParagraphs) {
var words = new[]{"lorem", "ipsum", "dolor", "sit", "amet", "consectetuer",
"adipiscing", "elit", "sed", "diam", "nonummy", "nibh", "euismod",
"tincidunt", "ut", "laoreet", "dolore", "magna", "aliquam", "erat"};
var rand = new Random();
int numSentences = rand.Next(maxSentences - minSentences)
+ minSentences + 1;
int numWords = rand.Next(maxWords - minWords) + minWords + 1;
StringBuilder result = new StringBuilder();
for(int p = 0; p < numParagraphs; p++) {
result.Append("<p>");
for(int s = 0; s < numSentences; s++) {
for(int w = 0; w < numWords; w++) {
if (w > 0) { result.Append(" "); }
result.Append(words[rand.Next(words.Length)]);
}
result.Append(". ");
}
result.Append("</p>");
}
return result.ToString();
}
名前、住所、電話番号、lorem ipsum テキストなどの偽のデータを簡単に生成するために使用できる Ruby Faker gem の C# ポートを作成しました。
NuGet パッケージ ( Install-Package Faker.Net
) として Github にソースがあり、その機能の一部をサンプル コードとともに紹介する投稿も書きました。
このような:
const string LoremIpsum = @"Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.";
それを繰り返すには:
String.Join(Environment.NewLine,
Array.ConvertAll(new int[count], i => LoremIpsum));
または、.Net 4.0 では:
String.Join(Environment.NewLine, Enumerable.Repeat(LoremIpsum, count));
Nuget には、これとまったく同じことを行うパッケージが実際にあります。
http://www.nuget.org/packages/NLipsum/
たとえば、これを行うだけでテキストの段落を生成できます。
var someComments = new NLipsum.Core.Paragraph();
Lorem Ipsum Online ジェネレーターを使用しないのはなぜですか?
HTML ページから lorem ispum 文字列を抽出するこのコードを書きました。
string LoremIpsum()
{
string HTML = null;
WebRequest request = WebRequest.Create("http://lipsum.com/feed/html");
request.Credentials = CredentialCache.DefaultCredentials;
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream dataStream = response.GetResponseStream();
StreamReader reader = new StreamReader(dataStream);
HTML = reader.ReadToEnd(); //se citeste codul HTMl
//searching for Lorem Ipsum
HTML = HTML.Remove(0, HTML.IndexOf("<div id=\"lipsum\">"));
HTML = HTML.Remove(HTML.IndexOf("</div>"));
HTML = HTML
.Replace("<div id=\"lipsum\">", "")
.Replace("</div>", "")
.Replace("<p>", "")
.Replace("</p>", "");
reader.Close();
dataStream.Close();
response.Close();
return HTML;
}
各文の最初の単語を大文字にする、上記の Greg + Tomino の優れた方法を少し修正。また、末尾の改行を削除し、1 が多すぎる「+ 1」をいくつか削除しました。ユーザー インターフェイスのワード ラップ機能をテストするのに非常に便利です。トミノとグレッグに感謝します。
private static string LoremIpsum(int minWords, int maxWords, int minSentences, int maxSentences, int numLines)
{
var words = new[]{"lorem", "ipsum", "dolor", "sit", "amet", "consectetuer", "adipiscing", "elit", "sed", "diam", "nonummy", "nibh", "euismod", "tincidunt", "ut", "laoreet", "dolore", "magna", "aliquam", "erat"};
var rand = new Random();
int numSentences = rand.Next(maxSentences - minSentences)
+ minSentences;
int numWords = rand.Next(maxWords - minWords) + minWords;
var sb = new StringBuilder();
for (int p = 0; p < numLines; p++)
{
for (int s = 0; s < numSentences; s++)
{
for( int w = 0; w < numWords; w++ )
{
if( w > 0 ) { sb.Append( " " ); }
string word = words[ rand.Next( words.Length ) ];
if( w == 0 ) { word = word.Substring( 0, 1 ).Trim().ToUpper() + word.Substring( 1 ); }
sb.Append( word );
}
sb.Append(". ");
}
if ( p < numLines-1 ) sb.AppendLine();
}
return sb.ToString();
}
StringBuilder を使用し、HTML タグを使用しないバージョン (段落記号の代わりに改行を使用):
private static string LoremIpsum(int minWords, int maxWords, int minSentences, int maxSentences, int numLines)
{
var words = new[]{"lorem", "ipsum", "dolor", "sit", "amet", "consectetuer", "adipiscing", "elit", "sed", "diam", "nonummy", "nibh", "euismod", "tincidunt", "ut", "laoreet", "dolore", "magna", "aliquam", "erat"};
var rand = new Random();
int numSentences = rand.Next(maxSentences - minSentences)
+ minSentences + 1;
int numWords = rand.Next(maxWords - minWords) + minWords + 1;
var sb = new StringBuilder();
for (int p = 0; p < numLines; p++)
{
for (int s = 0; s < numSentences; s++)
{
for (int w = 0; w < numWords; w++)
{
if (w > 0) { sb.Append(" "); }
sb.Append(words[rand.Next(words.Length)]);
}
sb.Append(". ");
}
sb.AppendLine();
}
return sb.ToString();
}
このLoremIpsumジェネレーターが見つかりました:http://www.gutgames.com/post/Lorem-Ipsum-Generator-in-C.aspx
NuGet にはNetFx Ipsum Generatorと呼ばれるものがあります。
でインストールできます
Install-Package netfx-IpsumGenerator
それはかなり最小限ですが、私は現在、より良いもの、または貢献する方法を探しています。