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!

A Problem for th Expert programmers - or not!

Status
Not open for further replies.

Cballe

Programmer
Apr 14, 2002
16
0
0
ZA
Hi there you all,

I need some help with something that sound like an easy task, but in fact could be very very difficult. The problem is as follows:

In my program, I have the following situation:

(My program makes use of Speech Recognition)

A TextBox, named TextBox1, gives the user Text to read.
As the user read the text thru a microphone, the text is displayed in a second TextBox, named TextBox2. (The text that are recognised by the dictation engine when reading the text in TextBox1, are displayed in the TextBox2)

Now, here is where the problem comes in: When the user has finished reading the text in TextBox1, he clicks on a button, named btnResult. The text of TextBox1, must then be compared with the text of TextBox2 and a percentage must be given of how accurate the text compare to each other (of TextBox1 with the text of TextBox2).

Please guys, this is officially a problem that kept me busy for 4 months now. I have tried everything – or almost everything. This is the code that are tried (Thanks to a guy from this site for his help! – Kojiro) – but it still does not work – Any suggestions are welcome, please…The problem with the code below is that I get an Overflow Error Message, although I made sure that there is text in both TextBoxes…

THE CODE:

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

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) 'why? you figure it out.. ;-)
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) 'why? you figure it out.. ;-)
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


The reason why I said that the problem is not that easy to solve is because the following things should be kept in mind when coding the problem:

1.) The spaces between words sold be taken in consideration
2.) The TextBoxes could not be compared line by line because the following situation could appear:

TextBox1: (Text that is given to user to read)

Line 1: This is a speech Recognition Test. Please speak in a natural tone of voice.
Line 2: Please do not speak to fast.

TextBox2: (Text that is recognised by Dictation engine)- When an extra word is recognised by dictation engine in Line1 and the words is continued in Line2:

Line 1: This is a speech Recognition Test. Please speak in the a natural tone
Line 2: of voice. Please do not speak to fast.

So as you all could see – the problem has some crazy things to keep in mind – but it is not impossible to solve – somewhere in the world on this great site there must be someone who could solve this stupid problem – please guys – help!

Thank you very much for your time and Effort
Best Regards from South Africa
C-Power


To Be Is To Do And To Do Is To Do Now Because Tomorrow May Not Be
 
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