-
counting each repeated Items in Listview using vb6
Hi, All I have large number of words in Listview and I want to count each repeated word and pt the result in a new listview for example
the new list view must be like table bellow
word frequency
go 100
the 300
they 400
.
.
.
..
and so on , Please can any help
you kind action really appreciated
-
Re: counting each repeated Items in Listview using vb6
Sounds like the hard way to do it. Unless you have one word per item
-
Re: counting each repeated Items in Listview using vb6
I'm using vb regex to find the occurences of each word in large text file , the thing is i want to find all word and put them in a listview, then listing the occurences for each word inside the listview .... can i find the occurences of each word before i put the words inside the listview ???if can plese help ... I have the code to find all word in large text file using vb regular expression (regex) can u follow it to me please ????
-
Re: counting each repeated Items in Listview using vb6
Break it down into steps. Show the code for this part:
Code:
find all word and put them in a listview
But, as I said. Re-think the output. Listview isn't the right control.
Hint: Use TWO ListBoxes. One can be SORTED, to get the listbox into alphabetical order (to count occurrences)
-
Re: counting each repeated Items in Listview using vb6
Yes, I'd also say the listview is a bit overdone, but the usual ListBox as proposed by David might come to an end of available items, when your text is large and contains more than 32767 words, which could be the case, if you put all the doubles into the list in the first place.
However, you could do a test for doubles BEFORE entering a word into the listbox, getting away with probably less than 32000 DIFFERENT words.
You could first scan the list items and see if the word is already there.
Let's assume lstWords is a ListBox with the words.
Code:
Private Function EnterNewWord(NewWord As String) As Integer
' function returns -1 if word exists or returns the current word count
Dim i%
For i = 0 To lstWords.ListCount - 1
If lstWords.List(i) = NewWord Then
EnterNewWord = -1
Exit Function
End If
Next
lstWords.AddItem NewWord
EnterNewWord = lstWords.ListCount
End Function
If you want to use a ListView, however, the addressing of the items looks a little different, but it will be basically the same.
It could be a little slow, however, when a higher word count is reached.
A little faster could be this method:
Code:
Private AllWords As String
Private Function EnterNewWord(NewWord As String) As Integer
If InStr(AllWords, "|"+NewWord + "|") Then
EnterNewWord = -1
Else
AllWords = AllWords + NewWord + "|"
lstWords.AddItem NewWord
EnterNewWord = lstWords.Count
End If
End Function
It keeps a variable named AllWords, in which all new words are stored, too, although separated by a "|". This allows us to use the InStr() function to test if the word is already there as such.
If we wouldn't have the separator, the InStr() would fail. If a word like "automobile" is in the string, then the word "auto" is already there, too. The separator, however, allows to seek the word "|auto|", which cannot be mixed up.
You have to initialise the AllWords varibale like this, before doing a new document:
AllWords = "|"
so it starts with a separator for the first word.
-
Re: counting each repeated Items in Listview using vb6
-
Re: counting each repeated Items in Listview using vb6
Batches of 32K items AT A TIME... Or, use a database. No limits there
-
Re: counting each repeated Items in Listview using vb6
-
Re: counting each repeated Items in Listview using vb6
Using a DB would let you create a DICTIONARY of words, and then, use a query to modify (or just report) the FREQUENCY.
Even the other way, 32K words, with 100 top words. 10,000 top words. Sort and count them...
DB would be preferred
-
Re: counting each repeated Items in Listview using vb6
Yes you could read all the words from the file, insert each word into a database then run a query to get each word and the count of each word fairly easy using the count() and Group by SQL methods.
.
-
Re: counting each repeated Items in Listview using vb6
.............................
-
Re: counting each repeated Items in Listview using vb6
Fix your code tag so the code is readable the end tag needs to have a / rather than \
-
Re: counting each repeated Items in Listview using vb6
COPY List1 to List2, then COUNT the item(s) and eliminate the DUPLICATES. This will be your good list. Store the results back in List1, along with the COUNT appended
Code:
' append count to listbox
You saw code for that
-
Re: counting each repeated Items in Listview using vb6
.........................
-
Re: counting each repeated Items in Listview using vb6
Follow DataMiser's advice. You can download the program that I used in my article, here
-
Re: counting each repeated Items in Listview using vb6
...................................
-
Re: counting each repeated Items in Listview using vb6
.................................
-
Re: counting each repeated Items in Listview using vb6
You use SQL Insert statements to insert the data.
You use a SQL select with the Count() and Group by methods to get the results.
You can find lots of info and examples on these with a simple google search.
-
Re: counting each repeated Items in Listview using vb6
.........................................
-
Re: counting each repeated Items in Listview using vb6
All the words you should need are provided in my previous post, try a few combinations and see what you get.
-
Re: counting each repeated Items in Listview using vb6
Falahjomor,
I see a multiple questions from you regarding ways to count the frequency of words from a text file. I have an example below, it doesn't use the listview control that you ask about. It uses ListBox controls instead, I believe that it does give you word counts you're asking about though.
Code assumes a form with . .
A command button named "cmdCountWords"
A listbox named "lstAllWords"
A listbox named "lstWordCount"
Manually set Sorted property of both ListBox controls to True
Code:
Option Explicit
Private Sub cmdCountWords_Click()
GetSubMatches
End Sub
Private Sub GetSubMatches()
Dim sFileContents As String
Dim saFileContents() As String
Dim iFreeFile As Integer
Dim iListCount As Long
Dim iWordCount As Long
Dim sCurrentWord As String
Dim sLastWord As String
On Error GoTo GetSubMatchesErrHandler
iFreeFile = FreeFile
Open "C:\Path\To\File.txt" For Binary As #iFreeFile
sFileContents = Input(LOF(iFreeFile), iFreeFile)
sFileContents = Replace(sFileContents, vbCr, " ")
sFileContents = Replace(sFileContents, vbLf, " ")
iListCount = 0
saFileContents = Split(sFileContents, " ")
Do Until iListCount > UBound(saFileContents)
If saFileContents(iListCount) = vbNullString Then
Else
Me.lstAllWords.AddItem saFileContents(iListCount)
End If
iListCount = iListCount + 1
Loop
iWordCount = 0
iListCount = 0
sCurrentWord = Me.lstAllWords.List(iListCount)
sLastWord = sCurrentWord
Do Until iListCount = Me.lstAllWords.ListCount
sCurrentWord = Me.lstAllWords.List(iListCount)
If UCase$(sCurrentWord) = UCase$(sLastWord) Then
iWordCount = iWordCount + 1
Else
Me.lstWordCount.AddItem sLastWord & " " & Format$(iWordCount + 1)
iWordCount = 0
sLastWord = sCurrentWord
End If
iListCount = iListCount + 1
Loop
GetSubMatchesExitPoint:
On Error Resume Next
Close #iFreeFile
Erase saFileContents
Exit Sub
GetSubMatchesErrHandler:
Select Case Err.Number
Case Else
MsgBox "Error occured in GetSubMatches" & vbCrLf & vbCrLf & Err.Description, vbOKOnly + vbExclamation
End Select
Resume GetSubMatchesExitPoint
End Sub
-
Re: counting each repeated Items in Listview using vb6
Quote:
Originally Posted by
DinoVaught
Falahjomor,
I see a multiple questions from you regarding ways to count the frequency of words from a text file. I have an example below, it doesn't use the listview control that you ask about. It uses ListBox controls instead, I believe that it does give you word counts you're asking about though.
Code assumes a form with . .
A command button named "cmdCountWords"
A listbox named "lstAllWords"
A listbox named "lstWordCount"
Manually set Sorted property of both ListBox controls to True
Code:
Option Explicit
Private Sub cmdCountWords_Click()
GetSubMatches
End Sub
Private Sub GetSubMatches()
Dim sFileContents As String
Dim saFileContents() As String
Dim iFreeFile As Integer
Dim iListCount As Long
Dim iWordCount As Long
Dim sCurrentWord As String
Dim sLastWord As String
On Error GoTo GetSubMatchesErrHandler
iFreeFile = FreeFile
Open "C:\Path\To\File.txt" For Binary As #iFreeFile
sFileContents = Input(LOF(iFreeFile), iFreeFile)
sFileContents = Replace(sFileContents, vbCr, " ")
sFileContents = Replace(sFileContents, vbLf, " ")
iListCount = 0
saFileContents = Split(sFileContents, " ")
Do Until iListCount > UBound(saFileContents)
If saFileContents(iListCount) = vbNullString Then
Else
Me.lstAllWords.AddItem saFileContents(iListCount)
End If
iListCount = iListCount + 1
Loop
iWordCount = 0
iListCount = 0
sCurrentWord = Me.lstAllWords.List(iListCount)
sLastWord = sCurrentWord
Do Until iListCount = Me.lstAllWords.ListCount
sCurrentWord = Me.lstAllWords.List(iListCount)
If UCase$(sCurrentWord) = UCase$(sLastWord) Then
iWordCount = iWordCount + 1
Else
Me.lstWordCount.AddItem sLastWord & " " & Format$(iWordCount + 1)
iWordCount = 0
sLastWord = sCurrentWord
End If
iListCount = iListCount + 1
Loop
GetSubMatchesExitPoint:
On Error Resume Next
Close #iFreeFile
Erase saFileContents
Exit Sub
GetSubMatchesErrHandler:
Select Case Err.Number
Case Else
MsgBox "Error occured in GetSubMatches" & vbCrLf & vbCrLf & Err.Description, vbOKOnly + vbExclamation
End Select
Resume GetSubMatchesExitPoint
End Sub
thank you for the nice code, but i tried your code with textfile of 64000 words and show me an error "error occurred getsubmatches Overflow"
Best Regard
-
Re: counting each repeated Items in Listview using vb6
Why have you deleted content from earlier posts?
-
Re: counting each repeated Items in Listview using vb6
Quote:
Originally Posted by
falahjomor
thank you for the nice code, but i tried your code with textfile of 64000 words and show me an error "error occurred getsubmatches Overflow"
Best Regard
I was able to recreate the same error you mention.
Although you can load more than 32767 items into a listbox or treeview, as soon as you try to read them using List(###) and ### is more than 32767 the overflow error will fire.
These controls, listbox or treeview, have the limitations or maximum values of a signed integer which is 32767 in VB.
There are numerous ways around this. You'll just have to use something other than the listview or listbox. Considering the amount of data you're dealing with, as others have suggested, you may want to considering using a database
In the sample code I posted, the general idea is there. Each line of code that adds an item to a list box could replaced with in INSERT into a table in a database, massaged then queried back to your VB application and displayed by a control that can handle more than the 64000 words you mention.
-
Re: counting each repeated Items in Listview using vb6
Odds are that once they are placed into a database and a proper count query is executed the number of words returned will be far less than the number in the original document as words like The, and and other common words may occur lots of times but the query should return the word only once with the number of occurances. So 64k words could turn into 20k or even less depending on the content of the document in question.
-
Re: counting each repeated Items in Listview using vb6
-
Re: counting each repeated Items in Listview using vb6
Quote:
Originally Posted by
WoF
????
Spam, close to absolute certainty. Try a search for posts from this "user" for confirmation...
-
Re: counting each repeated Items in Listview using vb6
-
Re: counting each repeated Items in Listview using vb6
Ok. The weird post is gone and my question marks remain.
You know, they weren't meant to follow your post, DataMiser...
-
Re: counting each repeated Items in Listview using vb6
:) does look a little odd now but I did see the other post before it was removed.