PageRenderTime 53ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/toolkit/PhoneToolkitSample/Data/LoremIpsum.cs

https://bitbucket.org/jeremejevs/word-steps
C# | 181 lines | 121 code | 24 blank | 36 comment | 21 complexity | 1d09261c1d989b893bdea2bb41c9b644 MD5 | raw file
  1. // (c) Copyright Microsoft Corporation.
  2. // This source is subject to the Microsoft Public License (Ms-PL).
  3. // Please see http://go.microsoft.com/fwlink/?LinkID=131993 for details.
  4. // All other rights reserved.
  5. using System;
  6. using System.Collections;
  7. using System.Collections.Generic;
  8. using System.IO;
  9. using System.Text;
  10. using System.Windows;
  11. using System.Windows.Resources;
  12. namespace PhoneToolkitSample.Data
  13. {
  14. /// <summary>
  15. /// A class to return paragraphs of random sentences or a word list from lorem ipsum data.
  16. /// </summary>
  17. public class LoremIpsum : IEnumerable<string>
  18. {
  19. public enum Capitalization
  20. {
  21. None,
  22. FirstWord,
  23. AllWords
  24. }
  25. private static Random _rnd = new Random(42);
  26. private static StringBuilder _builder = new StringBuilder();
  27. private static List<string> _sentences;
  28. private static List<string> _words;
  29. /// <summary>
  30. /// Returns random lorem ipsum sentences combined into a single string.
  31. /// </summary>
  32. /// <param name="sentenceCount">The nunmber of sentences.</param>
  33. /// <returns>The paragraph, composed of random sentences.</returns>
  34. public static string GetParagraph(int sentenceCount)
  35. {
  36. EnsureSentences();
  37. _builder.Length = 0;
  38. while (sentenceCount-- > 0)
  39. {
  40. _builder.Append(_rnd.Next(_sentences));
  41. if (sentenceCount > 0)
  42. {
  43. _builder.Append(' ');
  44. }
  45. }
  46. return _builder.ToString();
  47. }
  48. /// <summary>
  49. /// Return an alphabetized, lower-case list of lorem ipsum words.
  50. /// </summary>
  51. public static ICollection<string> Words
  52. {
  53. get
  54. {
  55. EnsureWords();
  56. return (ICollection<string>)_words;
  57. }
  58. }
  59. /// <summary>
  60. /// Get a string composed of random lorem ipsum words. Will not end with punctuation.
  61. /// </summary>
  62. /// <param name="wordCount">Number of words.</param>
  63. /// <param name="capitalize">How to capitalize the words.</param>
  64. /// <returns></returns>
  65. public static string GetWords(int wordCount, Capitalization capitalization)
  66. {
  67. EnsureWords();
  68. _builder.Length = 0;
  69. while (wordCount-- > 0)
  70. {
  71. int position = _builder.Length;
  72. _builder.Append(_rnd.Next(_words));
  73. if (capitalization == Capitalization.AllWords || (position == 0 && capitalization == Capitalization.FirstWord))
  74. {
  75. _builder[position] = char.ToUpper(_builder[position]);
  76. }
  77. if (wordCount > 0)
  78. {
  79. _builder.Append(' ');
  80. }
  81. }
  82. return _builder.ToString();
  83. }
  84. /// <summary>
  85. /// Enumerates the Words property.
  86. /// </summary>
  87. /// <returns>The enumerator.</returns>
  88. public IEnumerator<string> GetEnumerator()
  89. {
  90. return LoremIpsum.Words.GetEnumerator();
  91. }
  92. /// <summary>
  93. /// Enumerates the Words property.
  94. /// </summary>
  95. /// <returns>The enumerator.</returns>
  96. IEnumerator IEnumerable.GetEnumerator()
  97. {
  98. return LoremIpsum.Words.GetEnumerator();
  99. }
  100. /// <summary>
  101. /// Reads the lorem ipsum sentences. Supplies some data in case reading fails, which
  102. /// it will do at design time.
  103. /// </summary>
  104. private static void EnsureSentences()
  105. {
  106. if (_sentences == null)
  107. {
  108. _sentences = new List<string>();
  109. StreamResourceInfo info = Application.GetResourceStream(new Uri("Data/LoremIpsum.txt", UriKind.Relative));
  110. if (info != null)
  111. {
  112. Stream stream = info.Stream;
  113. if (stream != null)
  114. {
  115. using (StreamReader reader = new StreamReader(stream))
  116. {
  117. while (!reader.EndOfStream)
  118. {
  119. _sentences.Add(reader.ReadLine());
  120. }
  121. };
  122. }
  123. }
  124. if (_sentences.Count == 0)
  125. {
  126. _sentences.Add("Lorem ipsum dolor sit amet, consectetuer adipiscing elit.");
  127. _sentences.Add("Maecenas porttitor congue massa.");
  128. _sentences.Add("Fusce posuere, magna sed pulvinar ultricies, purus lectus malesuada libero, sit amet commodo magna eros quis urna.");
  129. _sentences.Add("Nunc viverra imperdiet enim.");
  130. _sentences.Add("Fusce est.");
  131. }
  132. }
  133. }
  134. /// <summary>
  135. /// Creates an alphabetized list of the words from the lorem ipsum text.
  136. /// </summary>
  137. private static void EnsureWords()
  138. {
  139. char[] separators = { ' ', ',', '.' };
  140. EnsureSentences();
  141. if (_words == null)
  142. {
  143. Dictionary<string, object> temp = new Dictionary<string, object>();
  144. foreach (string sentence in _sentences)
  145. {
  146. string[] words = sentence.Split(separators, StringSplitOptions.RemoveEmptyEntries);
  147. foreach (string word in words)
  148. {
  149. temp[word.ToLower()] = null;
  150. }
  151. }
  152. _words = new List<string>(temp.Keys);
  153. _words.Sort();
  154. }
  155. }
  156. }
  157. }