1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
| Public Function ScoreAlignement(Str1 As String, Str2 As String, _
ScoreMatch As Integer, ScoreMismatch As Integer, _
ScoreGap As Integer, Optional Casse As Boolean = False) _
As Long
Dim i As Long
Dim j As Long
Dim TempScore As Integer
Dim Max As Long
Dim Gauche As Long
Dim Haut As Long
Dim Diag As Long
'enlève les espaces superflus à droite et à gauche
Str1 = Trim(Str1)
Str2 = Trim(Str2)
'les deux chaines doivent contenir au moins un caractère
If IsNull(Str1) Or IsNull(Str2) Then
Exit Function
ElseIf Len(Str1) = 0 Or Len(Str2) = 0 Then
Exit Function
End If
ReDim TableauScore(0 To Len(Str1), 0 To Len(Str2))
'initialisation du tableau de score
TableauScore(0, 0) = 0
For i = 1 To Len(Str1)
TableauScore(i, 0) = TableauScore(i - 1, 0) + ScoreGap
Next i
For j = 1 To Len(Str2)
TableauScore(0, j) = TableauScore(0, j - 1) + ScoreGap
Next j
If Casse Then
Str1 = UCase(Str1)
Str2 = UCase(Str2)
End If
For i = 1 To Len(Str1)
For j = 1 To Len(Str2)
If Mid(Str1, i, 1) = Mid(Str2, j, 1) Then
TempScore = ScoreMatch
Else
TempScore = ScoreMismatch
End If
'score si on aligne le i-ème caractère de Str1 et le j-ème
'caractère de Str2
Diag = TableauScore(i - 1, j - 1) + TempScore
'score si on aligne le j-ème caractère de Str2 avec un gap
Gauche = TableauScore(i - 1, j) + ScoreGap
'score si on aligne le i-ème caractère de Str1 avec un gap
Haut = TableauScore(i, j - 1) + ScoreGap
'on choisit le score le plus important
Max = IIf(Diag >= Gaughe, Diag, Gauche)
Max = IIf(Haut >= Max, Haut, Max)
TableauScore(i, j) = Max
Next j
Next i
ScoreAlignement = TableauScore(Len(Str1), Len(Str2))
End Function |
Partager