Results 1 to 19 of 19

Thread: [RESOLVED] finding the number of tokens of a word in a string

  1. #1

    Thread Starter
    Frenzied Member litlewiki's Avatar
    Join Date
    Dec 2005
    Location
    Zeta Reticuli Distro:Ubuntu Fiesty
    Posts
    1,162

    Resolved [RESOLVED] finding the number of tokens of a word in a string

    hello everyone

    i'm writing a program that finds out how many times a word has been repeated in a string

    this is what i did
    VB Code:
    1. Dim chkstr(1 To 10) As String
    2.  
    3. temp = rtb.Text
    4.  
    5. Do While i <= Len(temp)
    6.  
    7. i = i + 1
    8. chkstr(i) = Mid$(temp, 1, InStr(1, temp, Chr(32)))
    9.  
    10. temp = Mid$(temp, InStr(1, temp, Chr(32)) + 1, Len(temp))
    11.  
    12. If InStr(1, temp, Chr(32)) <= 0 Then
    13.  
    14. chkstr(i) = temp
    15.  
    16. Exit Do
    17.  
    18. End If
    19.  
    20. Loop

    this adds all the words to the chkstr() array

    now
    the following should check if any word was repeated again before in the string
    VB Code:
    1. Dim wrdtoken(1 to 10) As Integer
    2.  
    3. For i = 2 To numWords
    4.  
    5.     For j = 1 To i - 1
    6.  
    7.         If StrComp(chkstr(i), chkstr(j), vbTextCompare) = 0 Then
    8.            
    9.             wrdtoken(i) = wrdtoken(i) + 1
    10.          Else
    11.  
    12.             unique = False
    13.            
    14.         End If
    15.     Next j
    16.  
    17. Next i

    the numWords function::

    VB Code:
    1. Public Function NumWords() As Integer
    2. Dim i As Integer
    3. Dim temp As String
    4. i = 0
    5. temp = rtb.Text
    6.  
    7. Do While i <= Len(temp)
    8. i = i + 1
    9. temp = Mid$(temp, InStr(1, temp, Chr(32)) + 1, Len(temp))
    10. If InStr(1, temp, Chr(32)) <= 0 Then Exit Do
    11. Loop
    12.  
    13. numwords = i
    14. End Function

    but it doesnt work the way i thought it would.....any suggestions please

  2. #2
    Fanatic Member Dnereb's Avatar
    Join Date
    Aug 2005
    Location
    Netherlands
    Posts
    863

    Re: finding the number of tokens of a word in a string

    There is another approch to count the amount of repetitions of a word/character sequence.

    on the fly:
    Code:
    Function CountA_Word(ByVal StrText As String, StrSequence As String) As Long
    
    Dim L As Long
    
    L = Len(Text)                                               'total length of text
    CountA_Word = L - Len(Replace(StrText, StrSequence, ""))    'Length taken by the sequence
    
    CountA_Word = CountA_Word / Len(StrSequence) 'divede throug the length of the sequence to get the number of occurences.
    
    End Function
    why can't programmers keep and 31 Oct and 25 dec apart. Why Rating is Useful
    for every question you ask provide an answer on another thread.

  3. #3
    Oi, fat-rag! bushmobile's Avatar
    Join Date
    Mar 2004
    Location
    on the poop deck
    Posts
    5,592

    Re: finding the number of tokens of a word in a string

    VB Code:
    1. Private Function StringCount(ByVal sText As String, ByVal sFind As String) As Long
    2.     StringCount = Len(Replace(sText, sFind, sFind & vbNullChar)) - Len(sText)
    3. End Function

  4. #4

    Thread Starter
    Frenzied Member litlewiki's Avatar
    Join Date
    Dec 2005
    Location
    Zeta Reticuli Distro:Ubuntu Fiesty
    Posts
    1,162

    Re: finding the number of tokens of a word in a string

    thanks bushmob it works like a charm but how to match for whole word only for eg the above code will give 3 if stext is say "why white which" and sfind is "wh"

  5. #5
    Oi, fat-rag! bushmobile's Avatar
    Join Date
    Mar 2004
    Location
    on the poop deck
    Posts
    5,592

    Re: finding the number of tokens of a word in a string

    here's one way:
    VB Code:
    1. Private Function WordCount(ByVal sText As String, ByVal sFind As String) As Long
    2.     sText = " " & sText & " "
    3.     Do While sText Like "*[!A-Z,a-z]" & sFind & "[!A-Z,a-z]*"
    4.         sText = Replace(sText, sFind, vbNullString, , 1)
    5.         WordCount = WordCount + 1
    6.     Loop
    7. End Function

  6. #6

    Thread Starter
    Frenzied Member litlewiki's Avatar
    Join Date
    Dec 2005
    Location
    Zeta Reticuli Distro:Ubuntu Fiesty
    Posts
    1,162

    Re: finding the number of tokens of a word in a string

    okay bushmob this is what my assignment is abt

    given a string
    1.find repititive words in the strings
    2.replace them with the the "starting pos:ending pos" of the first occurence of that word in the string

    for e.g
    the string "abc ddd efg abc abc ddd" will be "abc ddd efg 1:3 1:3 5:7"

    iam trying to incorporate your ideas but things are getting really complicated ...

  7. #7
    Oi, fat-rag! bushmobile's Avatar
    Join Date
    Mar 2004
    Location
    on the poop deck
    Posts
    5,592

    Re: finding the number of tokens of a word in a string

    hmmmm, this is one of those things that have countless ways of being approached. It all depends on the situation really - on what is going to be contained in the string.

    If it's only going to be "word word word word word" without any punctuation or anything then something similar to what you were attempting to do earlier might be one approach.

    Create an array of the words using split:
    VB Code:
    1. sParts() = Split(sText)
    Loop through each item in the array and compare it against all the following items:
    VB Code:
    1. For N = 0 To UBound(sParts)
    2.         For I = N + 1 To UBound(sParts)
    then compare the items and change if necessary:
    VB Code:
    1. If UCase$(sParts(I)) = UCase$(sParts(N)) Then sParts(I) = lPos & ":" & lPos + Len(sParts(N)) - 1
    you'll need to keep track of the of the word, then at the end join all the bits of the array back together.

    This is just one suggestion - writing a function that'll will work with punctuation as well is a bit more work.

  8. #8

    Thread Starter
    Frenzied Member litlewiki's Avatar
    Join Date
    Dec 2005
    Location
    Zeta Reticuli Distro:Ubuntu Fiesty
    Posts
    1,162

    Re: finding the number of tokens of a word in a string

    no no there are no punctuation or special symbols just plain string of words... thanks for those code snippets anyway .i will be working on these and keep you updated as and when required...thanks a million bushmob

  9. #9
    Oi, fat-rag! bushmobile's Avatar
    Join Date
    Mar 2004
    Location
    on the poop deck
    Posts
    5,592

    Re: finding the number of tokens of a word in a string

    An alternative - which is a bit faster, would be to loop through the string picking out the words from between the spaces
    VB Code:
    1. Do
    2.         lStart = InStr(lEnd, sText, " ")
    3.         If lStart Then
    4.             lEnd = InStr(lStart + 1, sText, " ")
    5.             If lEnd Then
    6.                 ' Get the word with Mid$ including spaces either side
    and then using the Replace function, starting it after the instance of the first word (which crops the string so you have to tack the start on yourself):
    VB Code:
    1. sText = Left$(sText, lEnd - 1) & Replace(sText, sWord, " " & lStart & ":" & lEnd - 2 & " ", lEnd)

  10. #10
    Fanatic Member Dnereb's Avatar
    Join Date
    Aug 2005
    Location
    Netherlands
    Posts
    863

    Re: finding the number of tokens of a word in a string

    You seem to overlook my example post 2. it's in basic the same as bushmobile only written less compact to let you understand the code.
    The big diffrence is Bushmobile doesn't divide through the length of the string thet was searched to get the correct occurences.

    Why gives 3 instead of one if it was 1 time in the text, but if it was diveded though the number of characters in Why (3) it would have been correct and the same as my post only written compact.
    why can't programmers keep and 31 Oct and 25 dec apart. Why Rating is Useful
    for every question you ask provide an answer on another thread.

  11. #11
    Oi, fat-rag! bushmobile's Avatar
    Join Date
    Mar 2004
    Location
    on the poop deck
    Posts
    5,592

    Re: finding the number of tokens of a word in a string

    Dnereb: the code in posts #2 and #3 will give exactly the same answer - but post #3 has one less operation.

    You'll also notice (posts 6 and onward) that getting the number of occurances of a string within another string isn't what litlewiki is actually trying to do making those posts redundant anyway.

  12. #12
    Fanatic Member Dnereb's Avatar
    Join Date
    Aug 2005
    Location
    Netherlands
    Posts
    863

    Re: finding the number of tokens of a word in a string

    I've noticed the change as well but I fail to see any use for it.

    My two cents about this counting:
    It is a poor way to compact text. In particular longer text with sequence that occur later in the text more often wil increas in size suppos the 'word'
    Bad is first encountered at position 200 an wil be used often from then on.
    bad...... 200:203....200:203.........200:203 etc
    The number of three letter (not adding interpunction and numbers) sequences posible is 17576 (dutch alfabet) although not all are prenounceble.
    the pronblem increases if you add four letter sequences.
    why can't programmers keep and 31 Oct and 25 dec apart. Why Rating is Useful
    for every question you ask provide an answer on another thread.

  13. #13
    Oi, fat-rag! bushmobile's Avatar
    Join Date
    Mar 2004
    Location
    on the poop deck
    Posts
    5,592

    Re: finding the number of tokens of a word in a string

    I'm not sure there is a purpose to the modifying the string - i think it's just an exercise in string manipulation.

  14. #14

    Thread Starter
    Frenzied Member litlewiki's Avatar
    Join Date
    Dec 2005
    Location
    Zeta Reticuli Distro:Ubuntu Fiesty
    Posts
    1,162

    Re: finding the number of tokens of a word in a string

    okay this is my new coding which iam attaching it here.
    it works as per what i expected but now my professor says that i need to check for phrase and not words alone.
    that is if the text is "the quick brown fox jumps over the red brown fox"
    then it should be coded as "the quick brown fox jumps over 1:3 red 11:19"
    and not the "quick brown fox jumps over 1:3 red 11:15 17:19" .
    Now im totally confused as to how to do that...

    i will paste the coding here

    VB Code:
    1. Option Explicit
    2. Dim sText() As String
    3. Dim N, i As Integer
    4.  
    5.  
    6. Private Sub Command1_Click()
    7. Dim tempstr, tempstr2, tempstr3 As String
    8. Dim temp As Integer
    9. sText() = Split(rtb.Text)
    10.  
    11. For N = 0 To UBound(sText)
    12.     temp = 0
    13.     For i = 0 To UBound(sText)
    14.    
    15.         If ((i <> N) And UCase$(sText(N)) = UCase$(sText(i))) Then
    16.        
    17.             temp = temp + 1
    18.            
    19.         End If
    20.    
    21.     Next i
    22.    
    23.         If temp = 0 Then
    24.         'unique
    25.        
    26.         Else
    27.        
    28.  
    29. rtb.Text = Mid$(rtb.Text, 1, InStr(1, rtb.Text, sText(N)) + Len(sText(N))) & Trim(Replace(Mid$(rtb.Text, Len(Mid$(rtb.Text, 1, InStr(1,_
    30. rtb.Text, sText(N)) + Len(sText(N))))), sText(N), CStr(InStr(1, rtb.Text,_
    31. sText(N))) & ":" & CStr(InStr(1, rtb.Text, sText(N)) + Len(sText(N)) - 1)))
    32.         End If
    33.  
    34. Next N
    35.  
    36.  
    37. End Sub
    Last edited by litlewiki; Apr 13th, 2007 at 02:02 PM.

  15. #15

    Thread Starter
    Frenzied Member litlewiki's Avatar
    Join Date
    Dec 2005
    Location
    Zeta Reticuli Distro:Ubuntu Fiesty
    Posts
    1,162

    Re: finding the number of tokens of a word in a string

    its just a string manipulation exercise ...the other name to this compression is dictionary based text compression and iam not supposed to use any inbuilt functions except the most used ones...

  16. #16
    Oi, fat-rag! bushmobile's Avatar
    Join Date
    Mar 2004
    Location
    on the poop deck
    Posts
    5,592

    Re: finding the number of tokens of a word in a string

    just a comment on your current code: that line is so ridiculously long that it's a wonder you have any idea what it's doing - you should store the results of functions that you are going to need more than once.

    If you're interested, here is the function using Split that i came up with:
    VB Code:
    1. Private Function ProcessString(ByVal sText As String) As String
    2.     Dim sParts() As String, lPos As Long, N As Long, I As Long
    3.     sParts = Split(sText, " ")
    4.     lPos = 1
    5.     For N = 0 To UBound(sParts)
    6.         If InStr(sParts(N), ":") = 0 And Len(sParts(N)) > 0 Then
    7.             For I = N + 1 To UBound(sParts)
    8.                 If UCase$(sParts(I)) = UCase$(sParts(N)) Then sParts(I) = lPos & ":" & lPos + Len(sParts(N)) - 1
    9.             Next I
    10.         End If
    11.         lPos = lPos + Len(sParts(N)) + 1
    12.     Next N
    13.     ProcessString = Join(sParts)
    14. End Function

  17. #17

    Thread Starter
    Frenzied Member litlewiki's Avatar
    Join Date
    Dec 2005
    Location
    Zeta Reticuli Distro:Ubuntu Fiesty
    Posts
    1,162

    Re: finding the number of tokens of a word in a string

    yea i know its ridiculously long i actually stored them in variables but .i just merged them to keep the code short (height wise ) .anyway ur code is smarter than mine ...but now how should i tackle the new situation....(detecting the pattern of phrases and not words)

  18. #18
    Oi, fat-rag! bushmobile's Avatar
    Join Date
    Mar 2004
    Location
    on the poop deck
    Posts
    5,592

    Re: finding the number of tokens of a word in a string

    well, to develop post #16 as an example - once you've found a match between sParts(I) and sParts(N) you'll need to loop comparing the consecutive words to each other, e.g.
    VB Code:
    1. If UCase$(sParts(I)) = UCase$(sParts(N)) Then
    2.         For J = 1 To UBound(sParts) - I
    3.             If UCase$(sParts(I + J)) = UCase$(sParts(N + J)) Then

  19. #19

    Thread Starter
    Frenzied Member litlewiki's Avatar
    Join Date
    Dec 2005
    Location
    Zeta Reticuli Distro:Ubuntu Fiesty
    Posts
    1,162

    Re: finding the number of tokens of a word in a string

    alright i will work on that idea and post back if iam stuck thanks again bushmob

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width