PageRenderTime 55ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 0ms

/toolkit/PhoneToolkitSample/Data/LoremIpsum.vb

https://bitbucket.org/jeremejevs/word-steps
Visual Basic | 164 lines | 101 code | 27 blank | 36 comment | 0 complexity | 49244c1f794e1097cb36764dec0be2ac 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. Imports System.Text
  6. Imports System.Windows.Resources
  7. Imports System.IO
  8. Namespace Data
  9. ''' <summary>
  10. ''' A class to return paragraphs of random sentences or a word list from lorem ipsum data.
  11. ''' </summary>
  12. Public Class LoremIpsum
  13. Implements System.Collections.Generic.IEnumerable(Of String)
  14. Public Enum Capitalization
  15. None
  16. FirstWord
  17. AllWords
  18. End Enum
  19. Private Shared _rnd As New Random(42)
  20. Private Shared _builder As New StringBuilder
  21. Private Shared _sentences As List(Of String)
  22. Private Shared _words As List(Of String)
  23. ''' <summary>
  24. ''' Returns random lorem ipsum sentences combined into a single string.
  25. ''' </summary>
  26. ''' <param name="sentenceCount">The nunmber of sentences.</param>
  27. ''' <returns>The paragraph, composed of random sentences.</returns>
  28. Public Shared Function GetParagraph(ByVal sentenceCount As Integer) As String
  29. EnsureSentences()
  30. _builder.Length = 0
  31. Dim tempVar = sentenceCount > 0
  32. sentenceCount -= 1
  33. Do While tempVar
  34. _builder.Append(_rnd.Next(_sentences))
  35. If sentenceCount > 0 Then
  36. _builder.Append(" "c)
  37. End If
  38. tempVar = sentenceCount > 0
  39. sentenceCount -= 1
  40. Loop
  41. Return _builder.ToString()
  42. End Function
  43. ''' <summary>
  44. ''' Return an alphabetized, lower-case list of lorem ipsum words.
  45. ''' </summary>
  46. Public Shared ReadOnly Property Words As ICollection(Of String)
  47. Get
  48. EnsureWords()
  49. Return CType(_words, ICollection(Of String))
  50. End Get
  51. End Property
  52. ''' <summary>
  53. ''' Get a string composed of random lorem ipsum words. Will not end with punctuation.
  54. ''' </summary>
  55. ''' <param name="wordCount">Number of words.</param>
  56. ''' <param name="capitalization">How to capitalize the words.</param>
  57. ''' <returns></returns>
  58. Public Shared Function GetWords(ByVal wordCount As Integer, ByVal capitalization As Capitalization) As String
  59. EnsureWords()
  60. _builder.Length = 0
  61. Dim tempVar = wordCount > 0
  62. wordCount -= 1
  63. Do While tempVar
  64. Dim position = _builder.Length
  65. _builder.Append(_rnd.Next(_words))
  66. If capitalization = capitalization.AllWords OrElse (position = 0 AndAlso capitalization = capitalization.FirstWord) Then
  67. _builder(position) = Char.ToUpper(_builder(position))
  68. End If
  69. If wordCount > 0 Then
  70. _builder.Append(" "c)
  71. End If
  72. tempVar = wordCount > 0
  73. wordCount -= 1
  74. Loop
  75. Return _builder.ToString()
  76. End Function
  77. ''' <summary>
  78. ''' Enumerates the Words property.
  79. ''' </summary>
  80. ''' <returns>The enumerator.</returns>
  81. Public Function GetEnumerator() As System.Collections.Generic.IEnumerator(Of String) Implements System.Collections.Generic.IEnumerable(Of String).GetEnumerator
  82. Return LoremIpsum.Words.GetEnumerator()
  83. End Function
  84. ''' <summary>
  85. ''' Enumerates the Words property.
  86. ''' </summary>
  87. ''' <returns>The enumerator.</returns>
  88. Private Function IEnumerable_GetEnumerator() As System.Collections.IEnumerator Implements System.Collections.IEnumerable.GetEnumerator
  89. Return LoremIpsum.Words.GetEnumerator()
  90. End Function
  91. ''' <summary>
  92. ''' Reads the lorem ipsum sentences. Supplies some data in case reading fails, which
  93. ''' it will do at design time.
  94. ''' </summary>
  95. Private Shared Sub EnsureSentences()
  96. If _sentences Is Nothing Then
  97. _sentences = New List(Of String)
  98. Dim info = Application.GetResourceStream(New Uri("Data/LoremIpsum.txt", UriKind.Relative))
  99. If info IsNot Nothing Then
  100. Dim stream = info.Stream
  101. If stream IsNot Nothing Then
  102. Using reader As New StreamReader(stream)
  103. Do While Not reader.EndOfStream
  104. _sentences.Add(reader.ReadLine())
  105. Loop
  106. End Using
  107. End If
  108. End If
  109. If _sentences.Count = 0 Then
  110. _sentences.Add("Lorem ipsum dolor sit amet, consectetuer adipiscing elit.")
  111. _sentences.Add("Maecenas porttitor congue massa.")
  112. _sentences.Add("Fusce posuere, magna sed pulvinar ultricies, purus lectus malesuada libero, sit amet commodo magna eros quis urna.")
  113. _sentences.Add("Nunc viverra imperdiet enim.")
  114. _sentences.Add("Fusce est.")
  115. End If
  116. End If
  117. End Sub
  118. ''' <summary>
  119. ''' Creates an alphabetized list of the words from the lorem ipsum text.
  120. ''' </summary>
  121. Private Shared Sub EnsureWords()
  122. Dim separators() = {" "c, ","c, "."c}
  123. EnsureSentences()
  124. If _words Is Nothing Then
  125. Dim temp As New Dictionary(Of String, Object)
  126. For Each sentence In _sentences
  127. Dim words() = sentence.Split(separators, StringSplitOptions.RemoveEmptyEntries)
  128. For Each word In words
  129. temp(word.ToLower()) = Nothing
  130. Next word
  131. Next sentence
  132. _words = New List(Of String)(temp.Keys)
  133. _words.Sort()
  134. End If
  135. End Sub
  136. End Class
  137. End Namespace