Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

URGENT: HELP WITH TEXTBOX COMPARE

Status
Not open for further replies.

Cballe

Programmer
Apr 14, 2002
16
0
0
ZA
Hi,

I am struggling with a compare function of two TextBoxes. The situation is as follows: TextBox1 contains text, say 50 words. TextBox2 contains text (suppose to be the same amount of words (and also same text)). When the user click on a button, cmdTextStart, a percentage must be given of how accurate the text of TextBox1, compare with the text of TextBox2.

THE PROBLEM: TextBox1 contains text that will always stay the same and is 50 words. TextBox 2, contains the text that could be typed in. Say for instance the person has 1 minute to type the text of TextBox1 in TextBox2. After the user has finised typing the text, he click on the cmdTextStart button and an result must be given of how accurate the text of TextBox1, compare with the text of TextBox2. The problem with the code below is that although there are text typed in TextBox2, it gives the error message : Textbox empty or no periods encountered!". Could someone please help me with this problem?

The code look as follows:

Option Explicit

Private Sub cmdTextStart_Click()
Dim colA As New Collection
Dim colB As New Collection
Dim strA As String
Dim strB As String
Dim Words As Long
Dim Percent As Long
Dim intSpace As Long
Dim intSpaceB As Long
Dim strSentenceA As String
Dim strSentenceB As String
Dim i As Long

strA = Text1.Text
strB = Text2.Text

'assumes that both textboxes have same no. of sentences.
'remove space after period
strA = Replace(strA, ". ", ".")
strB = Replace(strB, ". ", ".")

strA = UCase(strA) 'uppercase strings
strB = UCase(strB)

Do

intSpace = InStr(1, strA, ".")
If intSpace <> 0 Then
strSentenceA = Left(strA, intSpace - 1)
colA.Add strSentenceA
strA = Right(strA, Len(strA) - intSpace)
End If

intSpaceB = InStr(1, strB, &quot;.&quot;)
If intSpaceB <> 0 Then
strSentenceB = Left(strB, intSpaceB - 1)
colB.Add strSentenceB
strB = Right(strB, Len(strB) - intSpaceB)
End If

Loop While intSpace <> 0

For i = 1 To colA.Count
'MsgBox colA.Item(i) & vbCrLf & colB.Item(i)
Call Compare(colA.Item(i), colB.Item(i), Percent, Words)
Next

If Words = 0 Then
MsgBox &quot;Error: Textbox empty or no periods encountered!&quot;
End
End If

Percent = (Percent / Words) * 100 ' and the percentage
MsgBox &quot;The boxes are &quot; & Percent & &quot;% equal to each other&quot;, _
vbInformation, &quot;Result...&quot;

End Sub

Sub Compare(strA As String, strB As String, ByRef Percent As Long, ByRef Words As Long)
Dim colA As New Collection
Dim colB As New Collection
Dim intSpace As Integer
Dim intSpaceB As Integer
Dim i As Integer
Dim k As Integer
Dim x As String

Do
intSpace = InStr(1, strA, &quot; &quot;)
If intSpace <> 0 Then
colA.Add Left(strA, intSpace - 1)
x = Left(strA, intSpace - 1)
strA = Right(strA, Len(strA) - intSpace)
Else
x = Right(strA, Len(strA) - intSpace)
colA.Add x
End If
Loop While intSpace <> 0

Do
intSpaceB = InStr(1, strB, &quot; &quot;)
If intSpaceB <> 0 Then
colB.Add Left(strB, intSpaceB - 1)
strB = Right(strB, Len(strB) - intSpaceB)
Else
x = Right(strB, Len(strB) - intSpaceB)
colB.Add x
End If
Loop While intSpaceB <> 0

'find which sentence is longer.
If colA.Count < colB.Count Then
Words = Words + colB.Count 'word count of sentence having the more words
For i = 1 To colA.Count
For k = 1 To colB.Count
If colA.Item(i) = colB.Item(k) Then
Percent = Percent + 1
colB.Remove (k)
Exit For
End If
Next
Next
Else
Words = Words + colA.Count
For i = 1 To colB.Count
For k = 1 To colA.Count
If colB.Item(i) = colA.Item(k) Then
Percent = Percent + 1
colA.Remove (k)
Exit For
End If
Next
Next
End If


'----- section doesn't work 'faulty err.. bugs' -----
''find which sentence is longer.
'If colA.Count < colB.Count Then
' For i = 1 To colA.Count
' lngSuccess = InStr(1, strTempB, colA.Item(i), vbTextCompare)
' If lngSuccess > 0 Then
' Percent = Percent + 1
' strTempB = Replace(strTempB, colA.Item(i), &quot;&quot;, , 1)
' 'MsgBox colA.Item(i) & vbCrLf & strTempB
' End If
' Next
' Words = Words + colB.Count 'word count of sentence having the more words
'Else
' For i = 1 To colB.Count
' lngSuccess = InStr(1, strTempA, colB.Item(i), vbTextCompare)
' If lngSuccess > 0 Then
' Percent = Percent + 1
' strTempA = Replace(strTempA, colB.Item(i), &quot;&quot;, , 1)
' End If
' Next
' Words = Words + colA.Count 'word count of sentence having the more words
'End If

End Sub




 
In your previous posting of this question, I had a few questions for clarification -- maybe due to high volume activity, it was difficult find the thread, so I'll ask again


There are a number of questions that come to mind. The most basic question is

&quot;What is the formula for determining percentage match?&quot;

I ask because you stated that
1.) The spaces between words sold be taken in consideration
Just how are you to treat a spaces difference? In other words - is this a &quot;pause&quot; that is to be taken into account for comparison purposes?

I also see some potential problems in the handling of spaces. Because in the space handling loop, you're actually putting a NullString in the collection, whenever the 1st character in a string is a space.

intSpaceB = InStr(1, strB, &quot; &quot;)
If intSpaceB <> 0 Then ' Assume intSpaceB = 1
colB.Add Left(strB, intSpaceB - 1) ' Left(strB, 0)

It appears that neither option (the code section, and the commented out section) take into account the order or placement of the words within the sentence -- if I'm reading the code correctly

Suppose your two sentences are

This is a speech Recognition Test. Please speak in a natural
and
This is speech Recognition Test. Please speak in a natural

In the second sentence, the third word (&quot;a&quot;) is missing, but both the Remove Collection code, and the Replace with Null code will actually find and replace the second to last word (&quot;a&quot;) that appears in both sentences, and would consider that a match.

As far as the overflow condition - I would check for Words being 0.

So I'm really interesting in knowing what is the formula for determining Percentage Match. Good Luck
--------------
As a circle of light increases so does the circumference of darkness around it. - Albert Einstein
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top