Tokenizer question

  • Thread starter Thread starter Burak
  • Start date Start date
B

Burak

Hello,

I will start working on a project where we have to parse a string and
store the words and the number of times each word appears.

I know you can use the Split function to split a string into words
and then I'd have to savethe words and keep a track of how many times
each one appears.

Does .NET already have a way to do this?

Thank you,

Burak
 
* (e-mail address removed) (Burak) scripsit:
I will start working on a project where we have to parse a string and
store the words and the number of times each word appears.

I know you can use the Split function to split a string into words
and then I'd have to savethe words and keep a track of how many times
each one appears.

Does .NET already have a way to do this?

Split and Count ;-).
 
Hi Burak,

Yes it is in the simple .Net namespace VS and also in the extended .Net VB
namespace.

Split and Split
and
Count

Cor
 
Hello Cor,

I ran into the same problem that this person did

From: Tina ([email protected])
Subject: ArrayList problem
Newsgroups: microsoft.public.dotnet.languages.vb
Date: 2003-07-14 15:24:02 PST


http://groups.google.com/groups?hl=en&lr=&ie=UTF-8&oe=UTF-8&threadm=e#
p4eYlSDHA.2188%40TK2MSFTNGP10.phx.gbl&rnum=2&prev=/groups%3Fq%3DLateboun
d%2Bassignment%2Bto%2Ba%2Bfield%26hl%3Den%26lr%3D%26ie%3DUTF-8%26oe%3DUT
F-8%26selm%3De%2523p4eYlSDHA.2188%2540TK2MSFTNGP10.phx.gbl%26rnum%3D2

and changed my code based on the suggestions she received.

I am having problem actually incrementing a member of the structure.

Here is my code
----------------------------
Option Strict On

Public Class test
Inherits System.Windows.Forms.Form
Public Structure countInfo
Public word As String
Public num As Integer
End Structure

Private Sub btnClick_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnClick.Click

Dim y As New ArrayList
Dim infolist As New ArrayList
Dim strIn, str As String
Dim strOut() As String
Dim i, index As Integer
Dim z1 As countInfo

' string to parse
strIn = txtInfo.Text
' parse results
strOut = strIn.Split(CChar(" "))

' save the string the first time
' next time, incement its number
For i = 0 To strOut.GetLength(0) - 1
str = CStr(strOut.GetValue(i))
index = y.IndexOf(str)
If Not index >= 0 Then
y.Add(str)
Dim newInfo As countInfo
newInfo.word = CStr(str)
newInfo.num = 1
infolist.Add(newInfo)
Else

For Each z1 In infolist
If str = z1.word Then
' z1.num gets incremented
' but the actual structure in the
' array does not
z1.num += 1
Exit For
End If
Next

End If
Next

' this works fine
For Each z In infolist
txtOut.AppendText(z.word)
txtOut.AppendText(" ")
txtOut.AppendText(CStr(z.num))
txtOut.AppendText(Constants.vbNewLine)
Next

End Sub
End Class

--------------

I would appreciate your help.

Thanks ,

Burak
 
Hi Burak,

I did not look but made something I hope this helps?

Cor

Private Sub Y()
Dim infolist As New ArrayList
Dim info As New countInfo
Dim delimStr As String = " ,.:;#"
Dim delimiter As Char() = delimStr.ToCharArray()
Dim Cor As String = "Hello Cor, I ran into that same problem: that
this person did"
Dim Burrakstr As String = Cor.Replace(vbCrLf, "#")
Dim Burrak() As String = Burrakstr.Split(delimiter)
Burrak.Sort(Burrak)
Dim count As Integer
For Each a As String In Burrak
If a <> "" Then
If a <> info.word Then
If info.word <> "" Then
infolist.Add(info)
End If
info = New countInfo
info.word = a
info.count = 0
End If
info.count += 1
End If
Next
End Sub
Public Class countInfo
Public word As String
Public count As Integer
Public Sub New()
End Sub
End Class
 
Back
Top