@@ -84,14 +84,8 @@ $.Thailand = function (options) {
84
84
} ) ;
85
85
return expanded ;
86
86
} ,
87
- similar_text = function ( first , second ) {
88
- // Calculates the similarity between two strings
89
- // discuss at: http://phpjs.org/functions/similar_text
90
-
91
- if ( first === null || second === null || typeof first === 'undefined' || typeof second === 'undefined' ) {
92
- return 0 ;
93
- }
94
-
87
+ similar_text = function ( first , second , percentage ) {
88
+
95
89
first += '' ;
96
90
second += '' ;
97
91
@@ -102,8 +96,6 @@ $.Thailand = function (options) {
102
96
secondLength = second . length ,
103
97
p , q , l , sum ;
104
98
105
- max = 0 ;
106
-
107
99
for ( p = 0 ; p < firstLength ; p ++ ) {
108
100
for ( q = 0 ; q < secondLength ; q ++ ) {
109
101
for ( l = 0 ;
@@ -120,15 +112,28 @@ $.Thailand = function (options) {
120
112
121
113
if ( sum ) {
122
114
if ( pos1 && pos2 ) {
123
- sum += this . similar_text ( first . substr ( 0 , pos2 ) , second . substr ( 0 , pos2 ) ) ;
115
+ sum += similar_text ( first . substr ( 0 , pos2 ) , second . substr ( 0 , pos2 ) , false ) ;
124
116
}
125
117
126
118
if ( ( pos1 + max < firstLength ) && ( pos2 + max < secondLength ) ) {
127
- sum += this . similar_text ( first . substr ( pos1 + max , firstLength - pos1 - max ) , second . substr ( pos2 + max , secondLength - pos2 - max ) ) ;
119
+ sum += similar_text ( first . substr ( pos1 + max , firstLength - pos1 - max ) , second . substr ( pos2 + max , secondLength - pos2 - max ) , false ) ;
120
+ }
121
+ }
122
+
123
+ if ( percentage === false ) {
124
+ return sum ;
125
+ } else {
126
+ if ( first === second ) {
127
+ return 100 ;
128
+ } else {
129
+ if ( firstLength > secondLength ) {
130
+ return Math . floor ( sum / firstLength * 100 ) ;
131
+ } else {
132
+ return Math . floor ( sum / secondLength * 100 ) ;
133
+ }
128
134
}
129
135
}
130
136
131
- return sum ;
132
137
}
133
138
134
139
// get zip binary
@@ -266,8 +271,15 @@ $.Thailand = function (options) {
266
271
}
267
272
return isUnique ;
268
273
} ) . map ( function ( self ) { // give a likely score, will use to sort data later
269
- self . likely = [ similar_text ( str , self . d ) , similar_text ( str , self . a ) , similar_text ( str , self . p ) , similar_text ( str , self . z ) , ] . sort ( ) . pop ( ) ;
270
- } ) ) . select ( '*' ) . orderBy ( 'likely' ) . fetch ( ) ;
274
+ self . likely = [
275
+ similar_text ( str , self . d ) * 5 ,
276
+ similar_text ( str , self . a . replace ( / ^ เ ม ื อ ง / , '' ) ) * 3 ,
277
+ similar_text ( str , self . p ) ,
278
+ similar_text ( str , self . z )
279
+ ] . sort ( function ( a , b ) { return a - b } ) . pop ( ) ;
280
+
281
+ return self ;
282
+ } ) ) . select ( '*' ) . orderBy ( 'likely desc' ) . fetch ( ) ;
271
283
} catch ( e ) { }
272
284
273
285
callback ( possibles ) ;
0 commit comments