我想制作一个应用程序来计算文本文件中的单词并计算单词 [英] i want to make an app that count words from a text file and count words

查看:50
本文介绍了我想制作一个应用程序来计算文本文件中的单词并计算单词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个应用程序来计算文本文件中的单词并计算单词和每个单词的数量,然后按字母顺序列出它,例如:

text file:hello world hi world

单词数:4

每个单词的数量:

你好1次

hi 1次

世界2次



i设法编写一个代码,设法打开一个文本文件并计算单词的数量,但我没有管理如何计算每个单词然后按字母顺序显示它们,代码到现在为止:







 Imports System.IO 


Public Class mainform
Public ac As Double
Public ch As Double


Private Sub mainform_Load(ByVal sender As System.Object,ByVal e As System.EventArgs)Handles MyBase.Load

End Sub


'open file

Private Sub OpenToolStripMenuItem_Click(ByVal sender As System.Object,By Val e As System.EventArgs)处理OpenToolStripMenuItem.Click
Dim filereader As StreamReader
Dim result As DialogResult
result = OpenFileDialog1.ShowDialog
如果result = DialogResult.OK那么
filereader = New StreamReader(OpenFileDialog1.FileName)
Txtcount.Text = filereader.ReadToEnd
filereader.Close()
End if

End Sub
通过计算空格
'字计数器

Private Sub TheNumberOfWordsToolStripMenuItem_Click(ByVal sender As System.Object,ByVal e As System.EventArgs)处理TheNumberOfWordsToolStripMenuItem.Click
txtnmb.Text =

如果Txtcount.Text =那么
MsgBox(请先打开文本文件!!!)
退出Sub
结束如果

Dim number()As String

ac = 0
ch = 0

number = Txtcount.Text.Split( )

'txtnmb.Text =
For i = 0 To(number.Length - 2)
ac = ac + 1

Next

txtnmb.Text = txtnmb.Text& Val(ac)& vbTab

结束子


结束类

解决方案

这是不太难,但由于这是作业,我不会给你任何代码。



创建一个字典,字符串键和整数值。 />
遍历文本中的每个单词,并使用Dictionary.ContainsKey方法检查单词是否已存在。如果不是,请将其添加为值1。如果是,只需将当前值增加1。在循环结束时,你有一个字典,其中包含每个单词,以及它出现的次数。



编辑:纠正怪异的单词..

i want to make an app that count words from a text file and count words and the number of each word then to list it in alphabetical order for example:
text file : hello world hi world
number of words : 4
number of each word :
hello 1 time
hi 1 time
world 2 time

i manage to write a code that manage to open a text file and to count the number of words but i m not managing to how to count each word and then display them in alphabetical order ,the code till now :



Imports System.IO


Public Class mainform
    Public ac As Double
    Public ch As Double
    

    Private Sub mainform_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub


'open file 

    Private Sub OpenToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OpenToolStripMenuItem.Click
        Dim filereader As StreamReader
        Dim result As DialogResult
        result = OpenFileDialog1.ShowDialog
        If result = DialogResult.OK Then
            filereader = New StreamReader(OpenFileDialog1.FileName)
            Txtcount.Text = filereader.ReadToEnd
            filereader.Close()
        End If

    End Sub

    'word counter by counting the spaces 

    Private Sub TheNumberOfWordsToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TheNumberOfWordsToolStripMenuItem.Click
        txtnmb.Text = ""

        If Txtcount.Text = "" Then
            MsgBox("Please open a text file first !!!")
            Exit Sub
        End If

        Dim number() As String

        ac = 0
        ch = 0

        number = Txtcount.Text.Split(" ")

        'txtnmb.Text = ""
        For i = 0 To (number.Length - 2)
            ac = ac+ 1
            
        Next
        
        txtnmb.Text = txtnmb.Text & Val(ac) & vbTab

    End Sub

    
End Class

解决方案

It's not too difficult, but as this is homework, I'll not give you any code.

Create a Dictionary, of a string key, and integer value.
Loop through each word in your text, and use the Dictionary.ContainsKey method to check if the word is already present. If it isn't, add it with a value of one. If it is, just increment the current value by one. At the end of the loop, you have a dictionary which contains each word, with the count of how many times it appears.

Edit: corrected weird as word...


这篇关于我想制作一个应用程序来计算文本文件中的单词并计算单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆