在Visual Basic中计数文本文件中的单词

问题描述 投票:1回答:3

我正在编写一个与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

任何建议都受到欢迎和赞赏。

vb.net file counting
3个回答
2
投票

使用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

音节数可以用counting vowel sounds近似。首先将dipthongs(滑动元音)映射到单个元音字符,然后简单地计算所有出现的元音:

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

2
投票

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

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

1
投票

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

所以要保持一切,直到“ 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

我绝对不知道如何期望您知道一个单词具有的音节数,除了必须参考字典来比较每个单词。不能在那帮你。

© www.soinside.com 2019 - 2024. All rights reserved.