0

私はTwitterからアーティストを特定しようとしています。だから私はつぶやきを持っていて、私はnatural for nodeを使ってツイートをトークン化し、それをアーティストと並べるためにLevenshtein距離を使ってアーティストの配列と比較します。私の問題は、実際に各トークンをアーティストのリストと比較し、ツイートが参照しているものと一致するという論理には問題があります。JavaScriptの最小値とそれに関連するキー

次の例では、アーティストとしてClean Banditを取得する必要があります。

var saturday = ["Kanye West", "Pharrell Williams", "Paloma Faith", "Burt Bacharach", "Clean Bandit"]; 

var tweet = "My queen @graciechatto about to go on The Other Stage at Glastonbury #cleanbandit #glastonbury…" 

tokenizer = new natural.WordTokenizer(); //new tokeniser 

var tweetTokenised = tokenizer.tokenize(tweet); //tokenise the tweet and store it in tweetTokenised 

var i , j; 

//loop through tokenised tweet  
for(i=0;i<tweetTokenised.length;i++){ 
    console.log(tweetTokenised[i] + "--------------------------"); 
    var temp = []; 

    //compare token with list of artists performing on saturday  
    for(j=0;j<saturday.length;j++){ 

     //remove whitespace from the tweet tokens 
     console.log(tweetTokenised[i]+ "--->"+saturday[j]); //testing purposes 
     var score = natural.LevenshteinDistance(tweetTokenised[i].replace(/\s+/g, '').toLowerCase(),saturday[j].toLowerCase()); 

     //store score for each token vs artists in a temp dictionary 
     temp.push({ 
      key: saturday[j], 
      value: score 
     }); 
    } 
} 
+0

'score'プロパティで配列をソートし、最初の要素は、最低のスコアを有するであろう。 – Barmar

+0

おかげさまで@Barmarに感謝! – user130316

答えて

0
 //sort array from lowest to biggest 

     temp.sort(function(a, b) { 

      return parseFloat(a.value) - parseFloat(b.value); 

     }); 





     //console.log(temp); 

     //get the first object (the smallest in this instance as its been sorted) 

     lowest = temp.shift(); 

     console.log(lowest); 

     if(lowest.value < 2){ 

      distances.push(lowest); 

     } 

    } 

    console.log("printing distances"); 

    console.log(distances[0].key); //get artist name 

} 
関連する問題