在visualbasic中计算文本文件中的单词



我正在编写一个程序,应该类似于Flesch可读性索引。它应该在文本文件中读取,然后计算文件中的单词数量(不必是"真正的"单词,只是用空白分隔的任何东西),文件中的音节数量和句子数量。然后,它应该将这些计算应用于一个公式,以获得文本的阅读水平。

我的问题是我不知道如何计算单词、音节或句子的数量。这是我到目前为止的代码,但我不知道如何开始计算单词、音节和句子的数量。

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

任何建议都是欢迎和感激的。

可以使用String实现单词和句子的计数。分裂:

    ' 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

音节数可以通过计算元音来近似计算。首先将双元音(滑动元音)映射到单元音字符,然后简单地计算元音出现的次数:

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

单词之间用空格分隔,因此要计算单词的数量,您只需拆分文本内容并计算拆分的元素数量:

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

我知道这非常低效,但是您可以将整个文件视为一个字符串,然后对其进行一些解析逻辑…

所以保持所有内容直到"Dim line As String"行,并替换为如下内容:

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

我完全不知道你要怎么知道一个单词的音节数,而不是通过参考字典来比较每个单词。我帮不了你。

相关内容

  • 没有找到相关文章

最新更新