1

このためのライブラリがどこかにある必要があることは知っていますが、私がやろうとしているのは、文字列の配列と検索文字列を渡して、検索文字列との関連性に応じて配列を並べ替えることです。

私はこれを理解するためにグーグルで何をすべきかを理解するのに苦労してきました、誰かが私を正しい方向に向けますか?

できればphp、または別のサーバー側言語で

4

2 に答える 2

1

私はあなたが「啓示的」とはどういう意味か本当にわかりません。

ただし、検索文字列に基づいて最適な文字列を見つけたい場合は、レーベンシュタインアルゴリズムを使用できます。2つの弦の間の「距離」を計算します。

詳細はこちら: http: //php.net/manual/fr/function.levenshtein.php

于 2012-12-31T01:04:46.377 に答える
0

これはちょっとトリッキーで、英語で説明することはできないので、動作するコードを表示する必要があります。ご不明な点がございましたら、お問い合わせください。

<!DOCTYPE html>
   <head>
      <title>Search Array</title>
      <script>
      //so let's set an array of values we will be searching

    function searchArray(dis) {

        var bigContent = new Array('Jesus loves you','Angle','God in Heaven','Bala','Overcomer','Be born again','Adesuwa','Heaven is real','James','Apple','John the baptist');//this is the array that serves as your database

        var searchi = dis.value, result = Array();
        searchi = searchi.toLowerCase();

        //order the array alphabetically
        bigContent = bigContent.sort();

        var content;

        if(searchi !='') {
            //Define the arrays for initial pre-storage
            var keys = new Array(), contentArray = new Array();

            //Loop through the content array to search for all occurence of string
            for(var i=0;i<bigContent.length;i++) {
                content = bigContent[i].toLowerCase();
                if(content.indexOf(searchi) > -1) {//found the search in this value
                    //get the position of the search string in content
                    var pos = content.indexOf(searchi);

                    //make position the key for your content array
                    if(contentArray[pos]) {//check if this position has already been assigned to a content
                        //if yes, append this content.
                        contentArray[pos] += '[]'+bigContent[i];
                    } else {//else, set the content
                        contentArray[pos] = bigContent[i];
                        //only save the position the first time you find it, to avoid duplication
                        keys[keys.length] = pos;
                    }
                }
            }
            //sort the key so it can order the values in ascending order(relevance)
            keys = keys.sort();

            //loop thru the key
            for(var i=0;i<keys.length;i++) {
                var key = keys[i];//remember the value of "var key" is the key for contentArray value
                if(contentArray[key]) {//just to be sure it's ther
                    var thisContent = contentArray[key];
                    //check if the content has more than 1 value
                    if(thisContent.indexOf('[]') < 0) {//if it doesn't
                        result[result.length] = contentArray[key];
                    } else {//if it does
                        //convert content into array
                        var thisContentAr = thisContent.split('[]');
                        //Loop thru the array
                        for(var j=0;j<thisContentAr.length;j++) {
                            result[result.length] = thisContentAr[j];
                        }
                    }
                }
            }
        }
        document.getElementById('ov').innerHTML = '';
        for(var i = 0; i<result.length;i++) {
            document.getElementById('ov').innerHTML += '<div>'+result[i]+'</div>';
        }
    }

</script>
</head>
<body>
    <div><input type="text" onkeyup="searchArray(this);" autofocus="autofocus" /></div>
    <div id="ov"></div>
 </body>
</html>
于 2015-09-04T08:47:09.007 に答える