Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
JavaScript中的最小值及其关联键_Javascript_Arrays_Node.js_Twitter_Named Entity Recognition - Fatal编程技术网

JavaScript中的最小值及其关联键

JavaScript中的最小值及其关联键,javascript,arrays,node.js,twitter,named-entity-recognition,Javascript,Arrays,Node.js,Twitter,Named Entity Recognition,我正在尝试从twitter上识别艺术家。所以我有一条tweet,我使用标记化tweet,并将其与使用Levenshtein距离将标记与艺术家匹配的艺术家阵列进行比较。我的问题是,我很难将每个标记与艺术家列表进行实际比较,并与推特所指的标记进行匹配 下面的例子应该得到干净的班迪特作为艺术家 var saturday = ["Kanye West", "Pharrell Williams", "Paloma Faith", "Burt Bacharach", "Clean Bandit"]; va

我正在尝试从twitter上识别艺术家。所以我有一条tweet,我使用标记化tweet,并将其与使用Levenshtein距离将标记与艺术家匹配的艺术家阵列进行比较。我的问题是,我很难将每个标记与艺术家列表进行实际比较,并与推特所指的标记进行匹配

下面的例子应该得到干净的班迪特作为艺术家

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
        });
    }
}
var saturday=[“Kanye West”、“Pharrell Williams”、“Paloma Faith”、“Burt Bacharach”、“Clean Bandit”]; var tweet=“我的女王@graciechatto即将在格拉斯顿伯里#cleanbuitt#格拉斯顿伯里进入另一个舞台…” tokenizer=new natural.WordTokenizer()//新标记器 var TweetTokenized=tokenizer.tokenize(tweet)//标记化tweet并将其存储在TweetTokenized中 varⅠ,j; //循环通过标记化tweet 对于(i=0;i
//从最低到最大排序数组
温度排序(功能(a、b){
返回parseFloat(a.value)-parseFloat(b.value);
});
//控制台日志(temp);
//获取第一个对象(此实例中最小的对象已排序)
最低=温度偏移();
控制台日志(最低);
如果(最低值<2){
距离。推(最低);
}
}
控制台日志(“打印距离”);
console.log(距离[0].key);//获取艺术家名称
}

根据
score
属性对数组进行排序,然后第一个元素的得分最低。谢谢@Barmar,这正是我所需要的!
        //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 

}