Pergunta

I'm writing a program that is supposed to be similar to the Flesch Readability Index. It's supposed to read in a text file then count the number of words in the file (doesn't have to be "real" words, just anything separated by white space), the number of syllables in the file, and the number of sentences. It is then supposed to apply these calculations to a formula to get the reading level of the text.

My problem is that I have no idea how to count the number of words, syllables, or sentences. Here is the code I have so far, but I have no idea how to even start on the portion of the code to calculate the number of words, syllables, and sentences.

Option Strict On

Imports System.IO

Public Class Form1

    Private Sub ExitToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles ExitToolStripMenuItem.Click
        Me.Close()
    End Sub

    Private Sub OpenToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles OpenToolStripMenuItem.Click
        Dim open As New OpenFileDialog

        open.Filter = "text files |project7.txt|All file |*.*"
        open.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory)

       If open.ShowDialog() = Windows.Forms.DialogResult.OK Then
           Dim selectedFileName As String = System.IO.Path.GetFileName(open.FileName)
           If selectedFileName.ToLower = "project7.txt" Then
                Dim line As String
                Using reader As New StreamReader(open.OpenFile)
                    While Not reader.EndOfStream
                        line = reader.ReadLine
                        Console.WriteLine(line)
                    End While
                End Using
            Else
                MessageBox.Show("You cannot use that file!")
            End If
        End If
    End Sub
End Class

Any suggestions are welcomed and appreciated.

Foi útil?

Solução

Counting words and sentences can be achieved using String.Split:

    ' Reading text from a file
    Dim text = File.ReadAllText("file.txt")
    ' Counting words
    Dim words = text.Split(" "c)
    Dim wordCount = words.Length
    ' Counting sentences
    Dim sentences = text.Split("."c, "!"c, "?"c)
    Dim sentenceCount = sentences.Length

A syllable count can be approximated by counting vowel sounds. First map dipthongs (gliding vowels) to single vowel characters and then simply count all occurrences of vowels:

Function CountSyllables(word As String) As Integer
    word = word.ToLower()
    Dim dipthongs = {"oo", "ou", "ie", "oi", "ea", "ee", _
                     "eu", "ai", "ua", "ue", "au", "io"}
    For Each dipthong In dipthongs
        word = word.Replace(dipthong, dipthong(0))
    Next
    Dim vowels = "aeiou"
    Dim vowelCount = 0
    For Each c In word
        If vowels.IndexOf(c) >= 0 Then vowelCount += 1
    Next
    Return vowelCount
End Function

Outras dicas

Words are separated by spaces so to calculate the number of words you just could split the text content and count the splitted elements:

Dim TextContent as String = Io.File.ReadAllText("File.txt", System.Text.Encoding.Default)
Dim WordsCount as Integer = TextContent.Split().Count

I know this is horribly inefficient, but you could just treat the entire file as one string and then do some parsing logic on it...

so keep everything up until the line "Dim line As String" and replace with something like:

Dim doc As String = ""
Dim line As String
Using reader As New StreamReader(open.OpenFile)
    While Not reader.EndOfStream
        line = reader.ReadLine
        doc += line
        Console.WriteLine(line)
    End While
    Dim sentences As Integer = doc.parse('.').Count
    Dim words As Integer = doc.parse(' ').Count
End Using

I have absolutely no idea how you are expected to know the number of syllables a word has other than by having to reference a dictionary to compare each word against. Can't help you there.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top