Results 1 to 37 of 37

Thread: Using VB to find how many times 2 words appear in the same sentence together?

  1. #1

    Thread Starter
    Hyperactive Member stilekid007's Avatar
    Join Date
    Apr 2005
    Location
    DUDE! I'm your neighbor! HELLO!!!
    Posts
    388

    Using VB to find how many times 2 words appear in the same sentence together?

    Hello,


    I have this program where the user finds plays the game to find combinations - now what I need the computer to do is have the answers so that when the user clicks on a combination the computer will know whether or not it is a combination?

    Now what I mean by combination is:
    Example:

    I have a whole database of words/sentences.
    All I need the computer to do is tell me how many times each word appear with the other word in the same line of text (.txt file)

    So lets say I have a sentence like this:
    Jake and Alexa went to the movies.
    Sam and Alexa are siblings to Jake
    ---------------------------------------
    So "Alexa and Jake" appeared in the same sentence 2 times
    Except I need it to get all the other words also and tell me how many times they appear together in 2 word combinations.
    Can this even be done?

    Thank you for any suggestions info or code!
    Stilekid007

  2. #2
    Hyperactive Member
    Join Date
    May 2005
    Posts
    324

    Re: Using VB to find how many times 2 words appear in the same sentence together?

    Do you mean in the same line or in the same sentence?

  3. #3

    Thread Starter
    Hyperactive Member stilekid007's Avatar
    Join Date
    Apr 2005
    Location
    DUDE! I'm your neighbor! HELLO!!!
    Posts
    388

    Re: Using VB to find how many times 2 words appear in the same sentence together?

    Same Line.
    It will be in a .txt file and each line so it will be like this:
    Line1. Text here:
    Line2. Text here:
    Line3. Text here:

    So a period won't mark the end of a sentence but rather the end of the line.

    The .txt file is loaded into a text box (For your info)
    Thank you so much!
    Stilekid007

  4. #4
    Hyperactive Member
    Join Date
    May 2005
    Posts
    324

    Re: Using VB to find how many times 2 words appear in the same sentence together?

    Warning: I am not a programmer, and you need to check this. However- I'd try this. I am assuming the two words are in text boxes called NameOne and NameTwo on the same form

    VB Code:
    1. Dim TheList as variant, theCount as long
    2. thelist = split(me.text1,chr(13))
    3. for r = 1 to ubound(thelist,2)
    4. if instr(1,thelist(r),me.NameOne) > 0 and instr(1,thelist(r),me.NameTwo) > 0 then thecount=thecount + 1
    5. next
    6. msgbox "A total of " & thecount & " instances of " & me.nameone & " and " & nametwo & " were found

  5. #5
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Using VB to find how many times 2 words appear in the same sentence together?

    Are you comparing two lines or just one?

  6. #6
    Fanatic Member vbasicgirl's Avatar
    Join Date
    Jan 2004
    Location
    Manchester, UK
    Posts
    1,016

    Re: Using VB to find how many times 2 words appear in the same sentence together?

    plus, what is the user clicking when you say "when the user clicks on a combination".

    casey.

  7. #7

    Thread Starter
    Hyperactive Member stilekid007's Avatar
    Join Date
    Apr 2005
    Location
    DUDE! I'm your neighbor! HELLO!!!
    Posts
    388

    Re: Using VB to find how many times 2 words appear in the same sentence together?

    I am comparing a whole bunch of lines - like 50 lines of words.

    And I want it to tell me how many times in those 50 lines, 2 words appear in the same line.

    So lets say Jake and Alexa appear together in 8 different lines of the 50 lines.

    Then it would say:
    Jake, Alexa 8

    Or something like that.

    Right now the user would just click a command button and it will search the textbox.

    Thanks everyone!
    Stilekid007

  8. #8
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Using VB to find how many times 2 words appear in the same sentence together?

    Here we go I haven't tested it so save your work before you run it

    Code:
    Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" _
        (ByRef pDest As Any, _
         ByRef pSrc As Any, _
         ByVal ByteLen As Long)
    
    Private Function CountWords(ByVal lpFirstLine As Long, _
                                ByVal plngLinesUBound As Long, _
                                ParamArray strWords()) _
                               As Long
    ' IN: lpFirstLine:      Long pointer to the first element of an array 
    '                       containing the lines to search
    ' IN: plngLinesUBound:  Upper bound of the array containing the lines 
    ' IN: strWords():       String array of words to search for
    
        Dim Offset          As Long
        Dim strTemp         As String
        Dim strSentence     As String
        Dim astrWords()     As String
        Dim lngWordsPresent As Long
        Dim i               As Long
        Dim j               As Long
    
        ReDim alngWordCount(UBound(strWords)) As Long
    
        ' For each sentence
        For Offset = 0 To (plngLinesUBound * 4) Step 4
            CopyMemory ByVal strTemp, _
                       (lpFirstLine + Offset), _
                       4
            strSentence = strTemp
    
            astrWords = Split(Trim$(strTemp), " ")
    
            ' For each word in the sentence
            For i = 0 To UBound(astrWords)
    
                ' For each word to search for
                For j = 0 To UBound(alngWordCount)
                    If (astrWords(i) = strWords(j)) Then _
                        alngWordCount(j) = alngWordCount(j) + 1
                Next j
            Next i
    
            For i = 0 To UBound(alngWordCount)
                If (alngWordCount(i) > 0) Then _
                    lngWordsPresent = lngWordsPresent + 1
            Next i
            
            If (lngWordsPresent = UBound(alngWordCount)) Then _
                CountWords = CountWords + 1
        Next Offset
        
        strTemp = "Zeroed"
    End Function
    Call like this:
    Code:
    Dim astrSentences()     As String
    ' Populate astrSentences
    ' and then call the function like this
    MsgBox CountWords(VarPtr(astrSentences(0)), UBound(astrSentences), "Jake", "Alexa")
    Hope it works

  9. #9

    Thread Starter
    Hyperactive Member stilekid007's Avatar
    Join Date
    Apr 2005
    Location
    DUDE! I'm your neighbor! HELLO!!!
    Posts
    388

    Re: Using VB to find how many times 2 words appear in the same sentence together?

    Hello Penagate!

    Thank you for getting that code for me.

    I get a subscript out of range error with this line of code...
    Code:
    MsgBox CountWords(VarPtr(astrSentences(0)), UBound(astrSentences), "Jake", "Alexa")
    Also is this only going to search for Jake and Alexa?
    Thank you for all your help!
    Stilekid007

  10. #10
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Using VB to find how many times 2 words appear in the same sentence together?

    Thank goodness thats where its crashing not later otherwise it will probably GPF

    Have you dimension the array astrSentences? You have to populate it first.

    A sample array would be

    VB Code:
    1. Dim astrSentences(3) As String
    2. astrSentences(0) = "Jake and Alexa sat on a bench."
    3. astrSentences(1) = "Jake was very bored."
    4. astrSentences(2) = "Alexa was wearing Jake's thermal underwear."
    5. astrSentences(3) = "Useless sentence without the words J*** and A****."
    6.  
    7. MsgBox CountWords(VarPtr(astrSentences(0)), UBound(astrSentences), "Jake", "Alexa")

    To search for other words, just list them after the 2nd parameter, e.g.
    VB Code:
    1. MsgBox CountWords(VarPtr(astrSentences(0)), UBound(astrSentences), "Hippo", "Rhinocerous")
    2.  
    3. MsgBox CountWords(VarPtr(astrSentences(0)), UBound(astrSentences), "Badger", "Frog", "Bunny", "Toadstool")


  11. #11
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Using VB to find how many times 2 words appear in the same sentence together?

    hi penagate,

    is there much advanage to your method in speed or other to
    VB Code:
    1. For i = 0 To UBound(astrSentences)
    2. If InStr(astrSentences(i), " Jake ") And InStr(astrSentences(i), " Alexia ") Then cnt = cnt + 1
    3. Next
    4. MsgBox "no of occurances = " & cnt

    pete

  12. #12
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Using VB to find how many times 2 words appear in the same sentence together?

    D'oh I completely forgot about InStr
    I bet my method doesn't work anyway, I think the string copying is a bit dodgy.

    The thing about my code is I wrapped it in a function and to save passing the whole array (Stilekid said he had 50 or so sentences) I made it so only a pointer to the start of the array is passed and so thats why I had to use Copymemory. And hence the other complications. Also, it accepts a variable number of words to search for, so an extra loop is needed.

    Anyway here's the function again using Instr:
    Code:
    Private Function CountWords(ByVal lpFirstLine As Long, _
                                ByVal plngLinesUBound As Long, _
                                ParamArray strWords()) _
                               As Long
    ' IN: lpFirstLine:      Long pointer to the first element of an array 
    '                       containing the lines to search
    ' IN: plngLinesUBound:  Upper bound of the array containing the lines 
    ' IN: strWords():       String array of words to search for
    
        Dim Offset          As Long
        Dim strTemp         As String
        Dim strSentence     As String
        Dim astrWords()     As String
        Dim lngWordsPresent As Long
        Dim i               As Long
    
        ReDim alngWordCount(UBound(strWords)) As Long
    
        ' For each sentence
        For Offset = 0 To (plngLinesUBound * 4) Step 4
            CopyMemory ByVal strTemp, _
                       (lpFirstLine + Offset), _
                       4
            strSentence = strTemp
    
            astrWords = Split(Trim$(strTemp), " ")
    
            ' For each word to search for
            For i = 0 To UBound(strWords)
                If (InStr(1, strSentence, astrWords(i))) Then _
                    lngWordsPresent = lngWordsPresent + 1
            Next i
            
            If (lngWordsPresent = UBound(alngWordCount)) Then _
                CountWords = CountWords + 1
            
            lngWordsPresent = 0
        Next Offset
        
        strTemp = "Zeroed"
    End Function

  13. #13
    Hyperactive Member
    Join Date
    May 2005
    Posts
    324

    Re: Using VB to find how many times 2 words appear in the same sentence together?

    Quote Originally Posted by anguswalker
    Warning: I am not a programmer, and you need to check this. However- I'd try this. I am assuming the two words are in text boxes called NameOne and NameTwo on the same form

    VB Code:
    1. Dim TheList as variant, theCount as long
    2. thelist = split(me.text1,chr(13))
    3. for r = 1 to ubound(thelist,2)
    4. if instr(1,thelist(r),me.NameOne) > 0 and instr(1,thelist(r),me.NameTwo) > 0 then thecount=thecount + 1
    5. next
    6. msgbox "A total of " & thecount & " instances of " & me.nameone & " and " & me.nametwo & " were found
    This used instr(). Also shows how to use split to get the contents of the textbox into an array. Is it OK do you think?

  14. #14

    Thread Starter
    Hyperactive Member stilekid007's Avatar
    Join Date
    Apr 2005
    Location
    DUDE! I'm your neighbor! HELLO!!!
    Posts
    388

    Re: Using VB to find how many times 2 words appear in the same sentence together?

    Originally Posted by anguswalker
    Warning: I am not a programmer, and you need to check this. However- I'd try this. I am assuming the two words are in text boxes called NameOne and NameTwo on the same form.

    [Highlight=VB]Dim TheList as variant, theCount as long
    thelist = split(me.text1,chr(13))
    for r = 1 to ubound(thelist,2)
    if instr(1,thelist(r),me.NameOne) > 0 and instr(1,thelist(r),me.NameTwo) > 0 then thecount=thecount + 1
    next
    msgbox "A total of " & thecount & " instances of " & me.nameone & " and " & me.nametwo & " were found [Highlight=VB]
    I couldn't get this to work. I am not to good at programing in VB yet.

    Penagate, With your code my VB program always crashes. And I get an error saying "The memory could not be written."

    That is when I click the command button to output the number of words.

    I am sorry that I can't fix this myself, but like I said I am not very good yet with VB
    I appreciate all your help!
    Stilekid007

  15. #15
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Using VB to find how many times 2 words appear in the same sentence together?

    Quote Originally Posted by stilekid007
    I couldn't get this to work. I am not to good at programing in VB yet.

    Penagate, With your code my VB program always crashes. And I get an error saying "The memory could not be written."

    That is when I click the command button to output the number of words.

    I am sorry that I can't fix this myself, but like I said I am not very good yet with VB
    I appreciate all your help!
    Stilekid007
    Hahaha I was afraid of that Sucks not being able to test it... wait... *flash of inspiration* I can use VBA...

    OK I stopped the crash but now I'm getting zero for some resaon... I'll try and fix it for ya.

  16. #16

    Thread Starter
    Hyperactive Member stilekid007's Avatar
    Join Date
    Apr 2005
    Location
    DUDE! I'm your neighbor! HELLO!!!
    Posts
    388

    Re: Using VB to find how many times 2 words appear in the same sentence together?

    Thank you so much man! I really appreciate your help buddy!

  17. #17
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: Using VB to find how many times 2 words appear in the same sentence together?

    I don't understand, can't you use the code I posted for you yesterday in this thread?

  18. #18
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Using VB to find how many times 2 words appear in the same sentence together?

    Here ya go mate
    VB Code:
    1. Option Explicit
    2.  
    3. Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" _
    4.     (ByRef pDest As Any, _
    5.      ByRef pSrc As Any, _
    6.      ByVal ByteLen As Long)
    7. '
    8.  
    9. Private Function CountWords(ByVal lpFirstLine As Long, _
    10.                             ByVal plngLinesUBound As Long, _
    11.                             ParamArray strWords()) _
    12.                            As Long
    13. ' IN: lpFirstLine:      Long pointer to the first element of an array
    14. '                       containing the lines to search
    15. ' IN: plngLinesUBound:  Upper bound of the array containing the lines
    16. ' IN: strWords():       String array of words to search for
    17.  
    18.     Dim Offset          As Long
    19.     Dim strSentence     As String
    20.     Dim lngWordsPresent As Long
    21.     Dim i               As Long
    22.  
    23.     ReDim alngWordCount(UBound(strWords)) As Long
    24.  
    25.     ' For each sentence
    26.     For Offset = 0 To (plngLinesUBound * 4) Step 4
    27.         strSentence = Space(255)
    28.  
    29.         CopyMemory ByVal VarPtr(strSentence), _
    30.                    ByVal (lpFirstLine + Offset), _
    31.                    4
    32.  
    33.         strSentence = Trim$(strSentence)
    34.  
    35.         ' For each word to search for
    36.         For i = 0 To UBound(strWords)
    37.             If (InStr(1, strSentence, strWords(i))) Then _
    38.                 lngWordsPresent = lngWordsPresent + 1
    39.         Next i
    40.  
    41.         If (lngWordsPresent = (UBound(alngWordCount) + 1)) Then _
    42.             CountWords = CountWords + 1
    43.  
    44.         lngWordsPresent = 0
    45.     Next Offset
    46. End Function
    47.  
    48. Public Sub Tester()
    49.     Dim astrSentences(3) As String
    50.     astrSentences(0) = "Jake and Alexa sat on a bench."
    51.     astrSentences(1) = "Jake was very bored."
    52.     astrSentences(2) = "Alexa was wearing Jake's thermal underwear."
    53.     astrSentences(3) = "Useless sentence without the words J*** and A****."
    54.  
    55.     MsgBox CountWords(VarPtr(astrSentences(0)), UBound(astrSentences), "Jake", "Alexa")
    56. End Sub


  19. #19
    Fanatic Member vbasicgirl's Avatar
    Join Date
    Jan 2004
    Location
    Manchester, UK
    Posts
    1,016

    Re: Using VB to find how many times 2 words appear in the same sentence together?

    in your first post you put the sentance
    "Jake and Alexa went to the movies. Sam and Alexa are siblings to Jake"

    if you look, there is 4 sets of 2 word combinations. (Jake, and, Alexa, to). did all these need finding. Using InStr() could be a problem aswell because you could find words which are inside other words and you would also need to check for any punctuation at the end of words.

    casey.

  20. #20
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: Using VB to find how many times 2 words appear in the same sentence together?

    You could use the classes I posted in your other thread yesterday to check the number of occurance for a particular word in this manner.
    VB Code:
    1. Dim oWordCount As CWords
    2. Set oWordCount = New CWords
    3. oWordCount.CountWords "Jake and Alexa went to the movies. Sam and Alexa are siblings to Jake"
    4. MsgBox oWordCount("Jake").Count

  21. #21

    Thread Starter
    Hyperactive Member stilekid007's Avatar
    Join Date
    Apr 2005
    Location
    DUDE! I'm your neighbor! HELLO!!!
    Posts
    388

    Re: Using VB to find how many times 2 words appear in the same sentence together?

    Joacim Andersson
    VB Code:
    1. Dim oWordCount As CWords
    2. Set oWordCount = New CWords
    3. oWordCount.CountWords "Jake and Alexa went to the movies. Sam and Alexa are siblings to Jake"
    4. MsgBox oWordCount("Jake").Count
    Hello ya'll!

    First off Joacim, for this part of the program it is not just detecting how many times Jake appears in the line but rather, how many times the combination of Jake and Alexa appear in the same line.

    in your first post you put the sentance
    "Jake and Alexa went to the movies. Sam and Alexa are siblings to Jake"

    if you look, there is 4 sets of 2 word combinations. (Jake, and, Alexa, to). did all these need finding. Using InStr() could be a problem aswell because you could find words which are inside other words and you would also need to check for any punctuation at the end of words.

    casey.
    Yes there are more combinations then just Jake and Alexa, but I have to put this code in manually for each word combos I want to find
    Code:
      MsgBox CountWords(VarPtr(astrSentences(0)), UBound(astrSentences), "NewWord1", "NewWord2")
    I was hoping the code would just find all the combo's of words rather then just the ones I input, but oh well.

    It will just take me alot longer to do it. So if there is a way to get it to just detect all the combo's rather then just Jake and Alexa (or other inputed words) then please post!

    Penagate,

    I really appreciate all your time in coding that. It works nicley for finding Jake and Alexa or any other 2 word combos. SO thank you sooo much!

    If anyone has got more code with more features I will rate you haha!

    I will also rate you Penagate! Its all worth it!
    Thank you all!
    Stilekid007

  22. #22
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: Using VB to find how many times 2 words appear in the same sentence together?

    I don't understand what you're looking for. How do you define what a combination is? As I showed you yesterday the classes count each appearance of each and every word what more do you want?

  23. #23
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Using VB to find how many times 2 words appear in the same sentence together?

    He wants to count the occurrence of each line that has two words in common, I think.

  24. #24

    Thread Starter
    Hyperactive Member stilekid007's Avatar
    Join Date
    Apr 2005
    Location
    DUDE! I'm your neighbor! HELLO!!!
    Posts
    388

    Re: Using VB to find how many times 2 words appear in the same sentence together?

    Yes, thats correct diglienna,

    Here is 2 Lines:

    "Jake is going to town with Alexa." <-Jake and Alexa both appear on this line there for the count would be 1 occurance of Jake and Alexa on one line.

    Jake is also going with Sam. <- ALEXA does not appear in this sentence therefore I would not want that occurance of Jake to be counted.

    Stilekid007

  25. #25
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Using VB to find how many times 2 words appear in the same sentence together?

    Is Jake and Alex different from Alex and Jake?

  26. #26

    Thread Starter
    Hyperactive Member stilekid007's Avatar
    Join Date
    Apr 2005
    Location
    DUDE! I'm your neighbor! HELLO!!!
    Posts
    388

    Re: Using VB to find how many times 2 words appear in the same sentence together?

    No sir.

  27. #27
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: Using VB to find how many times 2 words appear in the same sentence together?

    Quote Originally Posted by stilekid007
    "Jake is going to town with Alexa." <-Jake and Alexa both appear on this line there for the count would be 1 occurance of Jake and Alexa on one line.

    Jake is also going with Sam. <- ALEXA does not appear in this sentence therefore I would not want that occurance of Jake to be counted.
    OK, but in the above 2 lines the words "Jake" and "is" occurs on both lines so would that be considered a pair? Or the words "is" and "with" or "Jake" and "with". You must have some rules for what should be considered a pair and which words that shouldn't The word "going" also appears on both lines so maybe "is" and "going" is a pair. Or "going" and "Jake"...

  28. #28

    Thread Starter
    Hyperactive Member stilekid007's Avatar
    Join Date
    Apr 2005
    Location
    DUDE! I'm your neighbor! HELLO!!!
    Posts
    388

    Re: Using VB to find how many times 2 words appear in the same sentence together?

    Hello Joacim,

    You do bring up a very good point.

    I guess what you would have to do is add some code to disclude certain words from pairing up. Like "And" "Is" etc.

    But with Penagate's code it only pairs up the ones that are specified by the user.
    So I guess the for an auomatic pairing code you could exlude certain mainframe words like "And" or maybe something else the user might specify.
    Stilekid007

  29. #29
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Using VB to find how many times 2 words appear in the same sentence together?

    Stile, I wrote another function that counts the number of words occuring in multiples of 2 in all sentences. Is that what you want or have I misunderstood?

  30. #30

    Thread Starter
    Hyperactive Member stilekid007's Avatar
    Join Date
    Apr 2005
    Location
    DUDE! I'm your neighbor! HELLO!!!
    Posts
    388

    Re: Using VB to find how many times 2 words appear in the same sentence together?

    Yes Penagate, That is what I want, will this count all the words or just Jake and ALexa (Or other specified search words), or will it count all pairs no matter what they are?

  31. #31
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Using VB to find how many times 2 words appear in the same sentence together?

    It counts all pairs in all sentences.

    Here it is (keep the Copymemory dec'l and add reference to Microsoft Scripting Library):

    VB Code:
    1. '# Requires a reference to the Microsoft Scripting Runtime
    2. Public Function CountCombos(ByVal lpFirstLine As Long, _
    3.                             ByVal plngLinesUBound As Long) _
    4.                            As Long
    5. ' IN: lpFirstLine:      Long pointer to the first element of an array
    6. '                       containing the lines to search
    7. ' IN: plngLinesUBound:  Upper bound of the array containing the lines
    8. ' RETURNS: The number of two word combinations present in all the sentences.
    9.  
    10.     Dim Offset          As Long
    11.     Dim strSentence     As String
    12.     Dim astrWords()     As String
    13.     Dim colWords        As Scripting.Dictionary
    14.     Dim lngWordsPresent As Long
    15.     Dim i               As Long
    16.  
    17.     Set colWords = New Scripting.Dictionary
    18.  
    19.     ' For each sentence
    20.     For Offset = 0 To (plngLinesUBound * 4) Step 4
    21.         strSentence = Space(255)
    22.  
    23.         CopyMemory ByVal VarPtr(strSentence), _
    24.                    ByVal (lpFirstLine + Offset), _
    25.                    4
    26.  
    27.         strSentence = Replace(Trim$(strSentence), "'s", vbNullString)
    28.        
    29.         ' Get all the words
    30.         astrWords = Split(strSentence, " ")
    31.        
    32.         ' Add them to the dictionary
    33.         For i = 0 To UBound(astrWords)
    34.             If (colWords.Exists(astrWords(i))) Then
    35.                 colWords(astrWords(i)) = colWords(astrWords(i)) + 1
    36.               Else
    37.                 colWords.Add astrWords(i), 1
    38.             End If
    39.         Next i
    40.     Next Offset
    41.    
    42.     ' Return number of combos
    43.     Dim varKeys         As Variant
    44.  
    45.     varKeys = colWords.Keys
    46.  
    47.     For i = 0 To UBound(varKeys)
    48.         Debug.Print varKeys(i) & ": " & colWords(varKeys(i))
    49.         If (colWords(varKeys(i)) > 1) Then _
    50.             CountCombos = CountCombos + colWords(varKeys(i)) \ 2
    51.     Next i
    52.  
    53.     Set colWords = Nothing
    54. End Function
    55.  
    56. Public Sub Tester()
    57.     Dim astrSentences(3) As String
    58.     astrSentences(0) = "Jake and Alexa sat on a bench."
    59.     astrSentences(1) = "Jake was very bored."
    60.     astrSentences(2) = "Alexa was wearing Jake's thermal underwear."
    61.     astrSentences(3) = "Useless sentence without the words J*** and A****."
    62.  
    63.     MsgBox CountCombos(VarPtr(astrSentences(0)), UBound(astrSentences))
    64. End Sub


  32. #32
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Using VB to find how many times 2 words appear in the same sentence together?

    Oh yeah, I forgot to mention, the sentences can only be a maximum of 255 characters long. If you want longer ones you will have to change the line where it says
    VB Code:
    1. strSentence = Space(255)
    and put in an appropriate number.

  33. #33

    Thread Starter
    Hyperactive Member stilekid007's Avatar
    Join Date
    Apr 2005
    Location
    DUDE! I'm your neighbor! HELLO!!!
    Posts
    388

    Re: Using VB to find how many times 2 words appear in the same sentence together?

    Hello Penagate!

    Ok, That is really sweet code, but I just realized something. The output is how many pairs of words are in the text. Not how many Combo's of like Jake and Alexa together. I thought this function was going to give me all the different combo's of words like bench and Jake or Alexa and bored. But I guess it gives me how many pairs of words like - how many Jakes appear in the text.

    I guess I could still use this. And then to figure out how many times (EXAMPLE) Jake and Alexa appear as a combo, I would use your other function. Is this correct?

    Thank you so much for that new function also!
    Stilekid077

  34. #34
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Using VB to find how many times 2 words appear in the same sentence together?

    Quote Originally Posted by stilekid007
    I thought this function was going to give me all the different combo's of words like bench and Jake or Alexa and bored.
    Well how do you know what is a combo? And yes my 2nd function counts the number of pairs of Jakes and benches etc.

  35. #35

    Thread Starter
    Hyperactive Member stilekid007's Avatar
    Join Date
    Apr 2005
    Location
    DUDE! I'm your neighbor! HELLO!!!
    Posts
    388

    Re: Using VB to find how many times 2 words appear in the same sentence together?

    Well I was saying Jake and Bench being a combo, instead of two jakes being a combo or two benches being a combo.

    Am I misunderstanding?

    Stilekid007

  36. #36
    PowerPoster kfcSmitty's Avatar
    Join Date
    May 2005
    Posts
    2,248

    Re: Using VB to find how many times 2 words appear in the same sentence together?

    does the user input each combo at startup? are they predefined?

  37. #37

    Thread Starter
    Hyperactive Member stilekid007's Avatar
    Join Date
    Apr 2005
    Location
    DUDE! I'm your neighbor! HELLO!!!
    Posts
    388

    Re: Using VB to find how many times 2 words appear in the same sentence together?

    Well right now it is predifined but I could set it up for the user to input combo's at startup.

    Stilekid007

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