How to count character, words, sentences and paragraphs
-
I have a really big problem here. This is what I need to do: Create a Class Library (I did that)in this class it will open a *.* file and once that file is open it needs to read that file and then count the number of characters, words, sentences and Paragraphs. I created a ReadOnly Property for each: Ex: Public ReadOnly Property as Characters as Integer For each of these properties they need to have hidden variables for the ReadOnly property to return. The hidden variables is where I need the calucaltion to be at to count the characters, words,...ect. Can someone just give me one code for either one of the above counts and I should beable to go from there? Thanks in advance!
-
I have a really big problem here. This is what I need to do: Create a Class Library (I did that)in this class it will open a *.* file and once that file is open it needs to read that file and then count the number of characters, words, sentences and Paragraphs. I created a ReadOnly Property for each: Ex: Public ReadOnly Property as Characters as Integer For each of these properties they need to have hidden variables for the ReadOnly property to return. The hidden variables is where I need the calucaltion to be at to count the characters, words,...ect. Can someone just give me one code for either one of the above counts and I should beable to go from there? Thanks in advance!
Sorry -- I am a bit confused; do you need functions that determine the counts, or do you need to know how to code the accessors for the properties? What a piece of work is man, how noble in reason, how infinite in faculties, in form and moving how express and admirable . . . and yet to me, what is this quintessence of dust? -- Hamlet, Act II, Scene ii.
-
Sorry -- I am a bit confused; do you need functions that determine the counts, or do you need to know how to code the accessors for the properties? What a piece of work is man, how noble in reason, how infinite in faculties, in form and moving how express and admirable . . . and yet to me, what is this quintessence of dust? -- Hamlet, Act II, Scene ii.
-
OK, this is a pretty basic example -- the way you parse words and sentences can be way more complex -- but it is a starting point...
Imports System.IO
Imports System.TextPublic Class FileCounts
Private \_text As String Private \_name As String Private \_sentences As Integer Private \_words As Integer Private \_characters As Integer Public ReadOnly Property Sentences() Get Return \_sentences End Get End Property Public ReadOnly Property Words() Get Return \_words End Get End Property Public ReadOnly Property Characters() Get Return \_characters End Get End Property Public Sub New(ByVal filename As String) If File.Exists(filename) Then \_name = filename \_readFile() \_characters = \_text.Length \_sentences = \_text.Split(".").Length \_words = \_text.Split(" ").Length Else Throw New ApplicationException("File " + filename + " does not exist") End If End Sub Private Sub \_readFile() Dim sb As StringBuilder = New StringBuilder Try Dim fs As FileStream = File.Open(\_name, FileMode.Open, FileAccess.Read, FileShare.None) Dim b(1024) As Byte Dim temp As UTF8Encoding = New UTF8Encoding(True) While fs.Read(b, 0, b.Length) > 0 sb.Append(temp.GetString(b)) End While Catch ex As Exception Console.WriteLine(ex.Message) End Try \_text = sb.ToString() End Sub
End Class
What a piece of work is man, how noble in reason, how infinite in faculties, in form and moving how express and admirable . . . and yet to me, what is this quintessence of dust? -- Hamlet, Act II, Scene ii.