-
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! :thumb:
Stilekid007 :wave:
-
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?
-
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
-
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:
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 " & nametwo & " were found
-
Re: Using VB to find how many times 2 words appear in the same sentence together?
Are you comparing two lines or just one?
-
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.
-
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
-
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 :D
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 :eek:
-
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
-
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 :lol:
Have you dimension the array astrSentences? You have to populate it first.
A sample array would be
VB Code:
Dim astrSentences(3) As String
astrSentences(0) = "Jake and Alexa sat on a bench."
astrSentences(1) = "Jake was very bored."
astrSentences(2) = "Alexa was wearing Jake's thermal underwear."
astrSentences(3) = "Useless sentence without the words J*** and A****."
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:
MsgBox CountWords(VarPtr(astrSentences(0)), UBound(astrSentences), "Hippo", "Rhinocerous")
MsgBox CountWords(VarPtr(astrSentences(0)), UBound(astrSentences), "Badger", "Frog", "Bunny", "Toadstool")
:)
-
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:
For i = 0 To UBound(astrSentences)
If InStr(astrSentences(i), " Jake ") And InStr(astrSentences(i), " Alexia ") Then cnt = cnt + 1
Next
MsgBox "no of occurances = " & cnt
pete
-
Re: Using VB to find how many times 2 words appear in the same sentence together?
D'oh I completely forgot about InStr :blush:
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
-
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:
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
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?
-
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.
[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
-
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 :lol: 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.
-
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!
-
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?
-
Re: Using VB to find how many times 2 words appear in the same sentence together?
Here ya go mate
VB Code:
Option Explicit
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 strSentence 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
strSentence = Space(255)
CopyMemory ByVal VarPtr(strSentence), _
ByVal (lpFirstLine + Offset), _
4
strSentence = Trim$(strSentence)
' For each word to search for
For i = 0 To UBound(strWords)
If (InStr(1, strSentence, strWords(i))) Then _
lngWordsPresent = lngWordsPresent + 1
Next i
If (lngWordsPresent = (UBound(alngWordCount) + 1)) Then _
CountWords = CountWords + 1
lngWordsPresent = 0
Next Offset
End Function
Public Sub Tester()
Dim astrSentences(3) As String
astrSentences(0) = "Jake and Alexa sat on a bench."
astrSentences(1) = "Jake was very bored."
astrSentences(2) = "Alexa was wearing Jake's thermal underwear."
astrSentences(3) = "Useless sentence without the words J*** and A****."
MsgBox CountWords(VarPtr(astrSentences(0)), UBound(astrSentences), "Jake", "Alexa")
End Sub
:)
-
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.
-
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:
Dim oWordCount As CWords
Set oWordCount = New CWords
oWordCount.CountWords "Jake and Alexa went to the movies. Sam and Alexa are siblings to Jake"
MsgBox oWordCount("Jake").Count
-
Re: Using VB to find how many times 2 words appear in the same sentence together?
Quote:
Joacim Andersson
VB Code:
Dim oWordCount As CWords
Set oWordCount = New CWords
oWordCount.CountWords "Jake and Alexa went to the movies. Sam and Alexa are siblings to Jake"
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.
Quote:
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! :thumb:
Thank you all!
Stilekid007 :wave:
-
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?
-
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.
-
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
-
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?
-
Re: Using VB to find how many times 2 words appear in the same sentence together?
-
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"...
-
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
-
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?
-
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?
-
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:
'# Requires a reference to the Microsoft Scripting Runtime
Public Function CountCombos(ByVal lpFirstLine As Long, _
ByVal plngLinesUBound As Long) _
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
' RETURNS: The number of two word combinations present in all the sentences.
Dim Offset As Long
Dim strSentence As String
Dim astrWords() As String
Dim colWords As Scripting.Dictionary
Dim lngWordsPresent As Long
Dim i As Long
Set colWords = New Scripting.Dictionary
' For each sentence
For Offset = 0 To (plngLinesUBound * 4) Step 4
strSentence = Space(255)
CopyMemory ByVal VarPtr(strSentence), _
ByVal (lpFirstLine + Offset), _
4
strSentence = Replace(Trim$(strSentence), "'s", vbNullString)
' Get all the words
astrWords = Split(strSentence, " ")
' Add them to the dictionary
For i = 0 To UBound(astrWords)
If (colWords.Exists(astrWords(i))) Then
colWords(astrWords(i)) = colWords(astrWords(i)) + 1
Else
colWords.Add astrWords(i), 1
End If
Next i
Next Offset
' Return number of combos
Dim varKeys As Variant
varKeys = colWords.Keys
For i = 0 To UBound(varKeys)
Debug.Print varKeys(i) & ": " & colWords(varKeys(i))
If (colWords(varKeys(i)) > 1) Then _
CountCombos = CountCombos + colWords(varKeys(i)) \ 2
Next i
Set colWords = Nothing
End Function
Public Sub Tester()
Dim astrSentences(3) As String
astrSentences(0) = "Jake and Alexa sat on a bench."
astrSentences(1) = "Jake was very bored."
astrSentences(2) = "Alexa was wearing Jake's thermal underwear."
astrSentences(3) = "Useless sentence without the words J*** and A****."
MsgBox CountCombos(VarPtr(astrSentences(0)), UBound(astrSentences))
End Sub
:)
-
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
and put in an appropriate number.
-
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! :thumb: :thumb: :thumb:
Stilekid077
-
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.
-
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
-
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?
-
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