gpt4 book ai didi

jquery - Typeahead.js 仅匹配单词的开头

转载 作者:行者123 更新时间:2023-11-30 23:55:11 26 4
gpt4 key购买 nike

我的预输入代码是:

var events = new Bloodhound({
datumTokenizer: Bloodhound.tokenizers.obj.whitespace('name'),
queryTokenizer: Bloodhound.tokenizers.whitespace,
limit: 100,
prefetch: {
url: Ajax.pluginurl + 'json/events.json',
ttl: 1
}
});

events.initialize();

initialize_events_typeahead ();

function initialize_events_typeahead () {
$('.event_name').typeahead(null, {
name: 'event',
displayKey: 'name',
source: events.ttAdapter(),
templates: {
empty: [
'<div class="empty-message">',
Ajax.no_results_found,
'</div>'
].join('\n'),
suggestion: function(data){
return '<p><strong>' + data.name + '</strong> - ' + data.description + '</p>';
}
},
engine: Hogan
});


$('.event_name').on("typeahead:selected typeahead:autocompleted", function(e,datum) {
$(this).parent().parent().parent().parent().find('.event_description').val(datum.description);
});
}

由于某种原因,此代码仅匹配名称的开头。然而,在演示中http://twitter.github.io/typeahead.js/examples/ ,预取部分,据我所知,具有相同的代码,即使在开始在世界中间输入某些内容时它也会匹配。

我该如何解决这个问题?谢谢

编辑:JSON 是:

[{"name":"P\u0159\u00edjezd host\u016f","description":"Cras ullamcorper ornare semper. Phasellus faucibus augue congue dapibus mollis. "},{"name":"Ob\u0159ad","description":"Curabitur fermentum diam quis viverra sodales. Phasellus sed sollicitudin magna, a dictum metus."},{"name":"Ve\u010dern\u00ed z\u00e1bava","description":"Curabitur fermentum diam quis viverra sodales."},{"name":"After-party","description":"Proin ipsum odio, vehicula vel diam a, dapibus suscipit velit. "},{"name":"Sn\u00eddan\u011b","description":"Proin ornare tempus ipsum at blandit. Nam hendrerit dolor et interdum vulputate."}]

最佳答案

不确定为什么他们的样本显示中间词匹配,但我 guess您应该开发自己的匹配算法(datumTokenizer)。

这是我的解决方案(临时的,以防他们在 Bloodhound 引擎中默认实现某些功能):

var engine = new Bloodhound({

datumTokenizer: function(d){
var tokens = [];
//the available string is 'name' in your datum
var stringSize = d.name.length;
//multiple combinations for every available size
//(eg. dog = d, o, g, do, og, dog)
for (var size = 1; size <= stringSize; size++){
for (var i = 0; i+size<= stringSize; i++){
tokens.push(d.name.substr(i, size));
}
}

return tokens;
},...

关于jquery - Typeahead.js 仅匹配单词的开头,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23448193/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com