我正在编写一个类似于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
欢迎并赞赏任何建议。
答案 0 :(得分:2)
单词由空格分隔,以便计算您可以拆分文本内容并计算拆分元素的单词数:
Dim TextContent as String = Io.File.ReadAllText("File.txt", System.Text.Encoding.Default)
Dim WordsCount as Integer = TextContent.Split().Count
答案 1 :(得分: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 :(得分:1)
我知道这非常低效,但您可以将整个文件视为一个字符串,然后对其执行一些解析逻辑...
所以保持一切直到第34行: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
我完全不知道除了必须引用字典来比较每个单词之外,你应该如何知道一个单词所具有的音节数量。无法帮助你。