2012-04-16 110 views
0

我必須比較兩個字符串,即多少百分比str1包含str2。計算字符串距離/ JavaScript差異?

str1="Hello wolrd" 
str2="hello" 

意味着distance(str1, str2)應該返回50%..就是這樣。

+3

你想實現什麼規則? IE瀏覽器。是否區分大小寫?你是否正在計算總字數中有多少單詞匹配並以百分比形式顯示? – psynnott 2012-04-16 06:31:50

+4

「字符串距離」是進行中數學調查的一個複雜且活躍的領域。測量它的方法有多種,例如Levenshtein距離等,具有不同的優點和缺點,具體取決於應用。你能否提供有關申請的詳細信息?這是用有用的東西來回答這種性質問題的唯一方法。 – 2012-04-16 06:42:13

+2

http://en.wikibooks.org/wiki/Algorithm_Implementation/Strings/Levenshtein_distance#JavaScript – 2012-04-16 06:45:15

回答

-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++) { /* ... */ }