2012-04-16 66 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++) { /* ... */ }