Make WordPress Core

Ticket #35667: 35667.2.patch

File 35667.2.patch, 2.5 KB (added by subharanjan, 11 years ago)

Implemented custom levenshtein() from ​https://github.com/wikimedia/mediawiki-extensions-Translate/blob/master/ttmserver/TTMServer.php#L79-L125

  • src/wp-includes/wp-diff.php

     
    430430         * @return int
    431431         */
    432432        public function compute_string_distance( $string1, $string2 ) {
    433                 // Vectors containing character frequency for all chars in each string
    434                 $chars1 = count_chars($string1);
    435                 $chars2 = count_chars($string2);
     433                $len1 = mb_strlen( $string1 );
     434                $len2 = mb_strlen( $string2 );
    436435
    437                 // L1-norm of difference vector.
    438                 $difference = array_sum( array_map( array($this, 'difference'), $chars1, $chars2 ) );
     436                $len = min( $len1, $len2 );
    439437
    440                 // $string1 has zero length? Odd. Give huge penalty by not dividing.
    441                 if ( !$string1 )
    442                         return $difference;
     438                if ( $len > 600 ) {
     439                        // arbitrary limit on character length for speed purpose.
     440                        $string_distance = $len;
     441                } else {
     442                        $string_distance = $this->levenshtein( $string1, $string2, $len1, $len2 );
     443                }
    443444
    444                 // Return distance per character (of string1).
    445                 return $difference / strlen($string1);
     445                return $string_distance;
    446446        }
    447447
    448448        /**
     449         * PHP implementation of Levenshtein edit distance algorithm.
     450         * Uses the native PHP implementation when possible for speed.
     451         * The native levenshtein is limited to 255 bytes.
     452         *
     453         * @since 4.5.0
     454         *
     455         * @param string $str1
     456         * @param string $str2
     457         * @param int $length1
     458         * @param int $length2
     459         * @return int
     460         */
     461        public function levenshtein( $str1, $str2, $length1, $length2 ) {
     462                if ( $length1 == 0 ) {
     463                        return $length2;
     464                }
     465                if ( $length2 == 0 ) {
     466                        return $length1;
     467                }
     468                if ( $str1 === $str2 ) {
     469                        return 0;
     470                }
     471
     472                $bytelength1 = strlen( $str1 );
     473                $bytelength2 = strlen( $str2 );
     474                if ( $bytelength1 === $length1 && $bytelength1 <= 255
     475                        && $bytelength2 === $length2 && $bytelength2 <= 255
     476                ) {
     477                        return levenshtein( $str1, $str2 );
     478                }
     479
     480                $prevRow = range( 0, $length2 );
     481                for ( $i = 0; $i < $length1; $i++ ) {
     482                        $currentRow = array();
     483                        $currentRow[0] = $i + 1;
     484                        $c1 = mb_substr( $str1, $i, 1 );
     485                        for ( $j = 0; $j < $length2; $j++ ) {
     486                                $c2 = mb_substr( $str2, $j, 1 );
     487                                $insertions = $prevRow[$j + 1] + 1;
     488                                $deletions = $currentRow[$j] + 1;
     489                                $substitutions = $prevRow[$j] + ( ( $c1 != $c2 ) ? 1 : 0 );
     490                                $currentRow[] = min( $insertions, $deletions, $substitutions );
     491                        }
     492                        $prevRow = $currentRow;
     493                }
     494
     495                return $prevRow[$length2];
     496        }       
     497
     498        /**
    449499         * @ignore
    450500         * @since 2.6.0
    451501         *