|
-
Apr 1st, 2009, 09:39 AM
#1
Thread Starter
PowerPoster
[Resolved] Does anyone know why the VB Replace function doesn't work?
I'm parsing web pages. Basically I replace all the carriage-return/line feeds with a space character. I replace all the word separators with a space character. Then I replace the double-spaces with a space character. And so on until I have a list of words and phrases.
All that works fine and happens pretty quickly. I'm timing everything and I'm pleased with the speed.
Eventually I have a list of comma-separated words and phrases with no spaces before or after the commas.
I then loop through a list of common words (the, and, is, at, etc.) and build a token as follows:
"," & commonword(n) & ","
Then I replace that word in the string. I loop through the commonwords removing each one.
My final string is turned into an array.
After that I count how many times each word that remains is used.
Basically what all this does is give me a list of not common words sorted by number of times they appear in the page.
Screenshot
What I noticed was that, for example, the word "The" is replaced several times, it's not replaced in all cases. I checked the string before replacement and after replacement and it's in the string like this,
etc,The,etc,etc,The,etc.
So there's no reason for it not to be replaced.
I've also converted everything to lower case for the comparison so that's not an issue.
To resolve it I've used a Do-Loop but it shouldn't be necessary unless I don't understand something about how replace is supposed to work.
For count, I'm using the default of -1 which according to MSDN, replaces all instances.
Here's the code:
Code:
Private Function RemoveWords(ByRef Words() As String) As Long
Dim C() As String
Dim sText As String
Dim n As Long
Dim s As String
Dim nStart As Long
Dim nEnd As Long
Dim nCount As Long
Dim nMostCount As Long
nStart = GetTickCount
C = Project_V2.CommonWords
If ArrayInitialized(C) And ArrayInitialized(Words) Then
' Rejoin string using a comma delimiter.
sText = Join(Words, ",")
' Check entire token by including commas on both sides so partial words aren't removed.
For n = LBound(C) To UBound(C)
' Create token.
s = "," & Trim$(C(n)) & ","
nCount = 0
' For whatever reason not all instances of token are removed in one replace operation.
Do While InStr(1, sText, s, vbTextCompare)
nCount = nCount + 1
sText = Replace(sText, s, ",", 1, -1, vbTextCompare)
Loop
If nCount > nMostCount Then nMostCount = nCount
Next n
Debug.Print nMostCount
' Remove leading and trailing commas.
If Left$(sText, 1) = "," Then sText = Right$(sText, Len(sText) - 1)
If Right$(sText, 1) = "," Then sText = Left$(sText, Len(sText) - 1)
Words = Split(sText, ",", -1, vbTextCompare)
End If
nEnd = GetTickCount
MsgBox (nEnd - nStart) / 1000 & " seconds to remove common words."
End Function
nMostCount is returning a value of 2 for several exceptionally verbose web pages.
Can anyone tell me why I have to call Replace multiple times?
Last edited by cafeenman; Sep 19th, 2010 at 10:15 AM.
Reason: Resolved
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|