r112813 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r112812‎ | r112813 | r112814 >
Date:15:21, 1 March 2012
Author:nikerabbit
Status:ok
Tags:i18nreview 
Comment:
Add upper limit for string length to compare, the time taken grows very fast.
Mark the function static as it is used as such.
Modified paths:
  • /trunk/extensions/Translate/utils/TTMServer.php (modified) (history)

Diff [purge]

Index: trunk/extensions/Translate/utils/TTMServer.php
@@ -201,7 +201,13 @@
202202 $b = $row->tms_text;
203203 $lenB = mb_strlen( $b );
204204 $len = min( $lenA, $lenB );
205 - $dist = self::levenshtein_php( $a, $b, $lenA, $lenB );
 205+ if ( $len > 1000 ) {
 206+ // two strings of length 1500 ~ 10s
 207+ // two strings of length 2250 ~ 30s
 208+ $dist = $len;
 209+ } else {
 210+ $dist = self::levenshtein( $a, $b, $lenA, $lenB );
 211+ }
206212 $quality = 1 - ( $dist / $len );
207213
208214 if ( $quality >= $this->config['cutoff'] ) {
@@ -261,7 +267,7 @@
262268 /**
263269 * The native levenshtein is limited to 255 bytes.
264270 */
265 - function levenshtein_php( $str1, $str2, $length1, $length2 ) {
 271+ public static function levenshtein( $str1, $str2, $length1, $length2 ) {
266272 if ( $length1 == 0 ) return $length2;
267273 if ( $length2 == 0 ) return $length1;
268274 if ( $str1 === $str2 ) return 0;

Status & tagging log