2012-04-16 13 views
0

私は2つの文字列を比較しなければなりません。つまり、str1のパーセンテージにstr2が含まれています。JavaScriptの距離/差を計算するには?

str1="Hello wolrd" 
str2="hello" 

を意味します。distance(str1, str2)は、そのように50%を返します。

+3

実装するルールは何ですか?つまり大文字と小文字は区別されますあなたは、合計単語のうちどれくらいの単語が一致しているかを数えて、パーセントで表示していますか? – psynnott

+4

"文字列距離"は、進行中の数学的調査の複雑で活動的な領域です。アプリケーションに応じて、さまざまな長所や短所を持つLevenshteinの一般的な距離など、さまざまな測定方法があります。アプリケーションの詳細を教えてください。それが、この性質の質問に有用な何かをもって答える唯一の方法です。 –

+2

http://ja.wikibooks.org/wiki/Algorithm_Implementation/Strings/Levenshtein_distance#JavaScript –

答えて

-1

下記のコードで試してください。

<script> 
var str1 = 'helloworld'; 
var str2 = 'hello'; 
var percent = 0; 
var newstr = str1.search(str2); 
if(newstr != -1){ 

    percent = (str2.length * 100)/str1.length; 
    alert(percent+'%'); 

}else{ 
    alert(percent+'%'); 
} 

</script> 

私があなたの質問を理解していれば、これはあなたに役立つと確信しています。

ありがとうございました。

0

私は数学者ではないですし、あなたにこのような何かのための魔法の公式を表示することはできません、しかし、この例では、あなたは再帰を使い始めるだろう - 慎重コードのコメントを見てください。

/* same_words array should hold the following: 
    ["john", "bbc", "hot", "red,", "xyz,", "apples,", "and", "likes"] 

    NOTES: 
    - The more words you add, the less the percentage will be... that's how percent works. 
    - milk and MILK are the same word, but: MILK and MILK, aren't the same because of the comma (same goes for periods). 
*/ 

var jSmith = 'JOHN smith LIKES bananas, HOT chocolate, AND APPLES, nonsense, RED, 321, XYZ, BBC or npr', 
jSmithArray = jSmith.trim().toLowerCase().split(' '); //Makes an array of words for jSmith 

var jDoe = 'JOHN doe LIKES oranges, milk, AND APPLES, XYZ, 123, RED, green, HOT and... cnn sucks, BBC rocks', 
jDoeArray = jDoe.trim().toLowerCase().split(' '); //Makes an array of words for jDoe 

var bothJohns = jSmithArray.concat(jDoeArray); //console.log(bothJohns); 

var c = 0; //counter 
var same_words = []; //collection container 


//collects all the words that occur in both sentences 
function collectSimilarWords(word) { 
    same_words.push(word); 
} 


//The w parameter holds the word "john" for the 1st time. 
//The fn parameter holds always the collectSimilarWords() function. 
function recur(w, fn) { 
    for (var p in jSmithArray) { //Every property of the jSmithArray Array holds a word string. 
    if (w) { 
     if (w == jSmithArray[p]) { //If the word we passed in as a parameter is the same word as one of the jSmithArray elements... 
      fn(jSmithArray[p]); //...then pass this word to the collectSimilarWords() function. 
     } 
     c += 1; //Increase c so we can move to... 
     recur(jDoeArray[c], collectSimilarWords); //...the next word recursively. 
    } 
    } 
} 

//Call recur() and pass in the first word of the jDoeArray as the 1st param, and a function as the 2nd param. 
recur(jDoeArray[c], collectSimilarWords); 


function calcWhatever(samewords) { //Feel free to calculate however you want. 
    var percent = ((samewords.length * 100)/bothJohns.length).toFixed(2); 
    var msg = "Out of Smith\'s plus Doe's words (total: " + bothJohns.length + "), about " + percent + "% (or " + same_words.length + ") of them are found in both of their sentences!"; 
    return msg; 
} 

//Hopefuly setTimeout() will log the same_words array when the recursive function has finished. 
window.setTimeout(function() { 
    console.log(same_words); 
    console.log( calcWhatever(same_words) ); 
}, 1000); 

の代わりにfor inループを使用すると、簡単にforループを以下のように使用できます。

for (var p = 0; p<jSmithArray.length; p++) { /* ... */ } 
関連する問題