컴

levenshtein - 두 문자열 사이의 Levenshtein 거리를 계산합니다.

· 2017-04-10 (월) 21:25:37 · 2432

levenshtein - 두 문자열 사이의 Levenshtein 거리를 계산합니다.

 

설명 ¶

 

int levenshtein ( string $str1 , string $str2 )

int levenshtein ( string $str1 , string $str2 , int $cost_ins , int $cost_rep , int $cost_del )

Levenshtein distance는 변환하기 위해 바꾸거나 삽입하거나 삭제해야하는 최소 문자 수로 정의 str1됩니다 str2. 알고리즘의 복잡성은 O (m * n)입니다 . 여기서 n 과 m 은 O (max (n, m) ** 3) 인 similar_text () 의 길이 str1와 str2(오히려 좋지만 여전히 비쌉니다. ).

 

가장 간단한 형식에서 함수는 매개 변수로 두 개의 문자열 만 가져오고 str1변환에 필요한 삽입, 교체 및 삭제 작업의 수만 계산합니다 str2.

 

두 번째 변형은 삽입, 교체 및 삭제 작업의 비용을 정의하는 세 개의 추가 매개 변수를 취합니다. 이는 변형 1보다 일반적이며 적응력이 있지만 효율적이지는 않습니다.

 

매개 변수 ¶

 

str1

Levenshtein 거리에 대해 평가되는 문자열 중 하나입니다.

 

str2

Levenshtein 거리에 대해 평가되는 문자열 중 하나입니다.

 

cost_ins

삽입 비용을 정의합니다.

 

cost_rep

대체 비용을 정의합니다.

 

cost_del

삭제 비용을 정의합니다.

 

반환 값 ¶

 

이 함수는 두 인수 문자열 사이의 Levenshtein-Distance 또는 인수 문자열 중 하나가 255 자의 제한보다 긴 경우 -1을 반환합니다.

 

예 ¶

 

Example # 1 levenshtein () 예제

 

<?php

// input misspelled word

$input = 'carrrot';

 

// array of words to check against

$words  = array('apple','pineapple','banana','orange',

                'radish','carrot','pea','bean','potato');

 

// no shortest distance found, yet

$shortest = -1;

 

// loop through words to find the closest

foreach ($words as $word) {

 

    // calculate the distance between the input word,

    // and the current word

    $lev = levenshtein($input, $word);

 

    // check for an exact match

    if ($lev == 0) {

 

        // closest word is this one (exact match)

        $closest = $word;

        $shortest = 0;

 

        // break out of the loop; we've found an exact match

        break;

    }

 

    // if this distance is less than the next found shortest

    // distance, OR if a next shortest word has not yet been found

    if ($lev <= $shortest || $shortest < 0) {

        // set the closest match, and shortest distance

        $closest  = $word;

        $shortest = $lev;

    }

}

 

echo "Input word: $input\n";

if ($shortest == 0) {

    echo "Exact match found: $closest\n";

} else {

    echo "Did you mean: $closest?\n";

}

 

?>

위의 예제는 다음과 같이 출력됩니다 :

 

입력 단어 : carrrot

당신은 의미 했나요? 당근?

|
댓글을 작성하시려면 로그인이 필요합니다.

개발자팁

5,403건

개발과 관련된 유용한 정보를 공유하세요. 질문은 QA에서 해주시기 바랍니다.

+
분류 제목 글쓴이 날짜 조회
PHP 17-02-24 조회 3,648
PHP 17-02-24 조회 3,123
jQuery 17-02-23 조회 2,612
jQuery 17-02-23 조회 2,894
jQuery 17-02-23 조회 2,631
jQuery 17-02-22 조회 2,445
PHP 17-02-21 조회 2,688
PHP 17-02-21 조회 2,627
PHP 17-02-21 조회 4,424
PHP 17-02-20 조회 3,636
PHP 17-02-20 조회 3,028
PHP 17-02-20 조회 3,026
PHP 17-02-19 조회 2,850
PHP 17-02-19 조회 2,685
PHP 17-02-19 조회 2,825
PHP 17-02-17 조회 2,562
PHP 17-02-17 조회 2,989
PHP 17-02-17 조회 2,629
jQuery 17-02-16 조회 3,377
jQuery 17-02-16 조회 3,424
jQuery 17-02-16 조회 2,434
jQuery 17-02-15 조회 3,066
jQuery 17-02-15 조회 3,357
jQuery 17-02-15 조회 3,694
PHP 17-02-14 조회 2,889
PHP 17-02-14 조회 4,438
PHP 17-02-14 조회 2,680
PHP 17-02-13 조회 3,197
PHP 17-02-13 조회 2,604
PHP 17-02-13 조회 2,940