预先管理同义词?

时间:2016-03-02 07:35:58

标签: typeahead.js bloodhound

我最近对先行/猎犬进行了捣乱,并尝试在包含本地数据的页面上实现它。完成并运行,但还有一个问题。

是否可以实现某种静态同义词列表?我想处理如下例子:

  • 医院<> Sygehus
  • 立方体<>库贝

搜索一个或另一个应该搜索两者,或者搜索第一个单词上是否没有返回结果。该列表可能不会包含多个单词,因此我正在考虑手动更新列表。

但是在哪里/怎么做呢?我的设置类似于typeahead.js(typeahead example

上的多个数据集的示例

1 个答案:

答案 0 :(得分:0)

我解决了我的问题,但可能不是一个漂亮的问题。 Question about accents

我在这个例子中找到了灵感,并制作了两只带有正常化的猎犬。一个"Cube" : "Kube",一个"Kube" : "Cube"。然后我会得到Kube和Cube的结果,而不仅仅是Cube或Kube(仅使用一个规范化)。

      var charMap = {
        'a': /[àáâã]/gi,
        'c': /[ç]/gi,
        'e': /[èéêë]/gi,
        'i': /[ïí]/gi,
        'o': /[ôó]/gi,
        'oe': /[œ]/gi,
        'u': /[üú]/gi
    };

var normalize = function (str) {
    $.each(charMap, function (normalized, regex) {
        str = str.replace(regex, normalized);
    });
    return str;
};

var queryTokenizer = function (q) {
    var normalized = normalize(q);
    return Bloodhound.tokenizers.whitespace(normalized);
};

var foo_keywords = [
{name: 'foo1', keywords_query: normalize('foo1à, foo1ç, foo1ê'), keywords: 'foo1à, foo1ç, foo1ê', picture: '/img/foo1.png'},
{name: 'foo2', keywords_query: normalize('foo2à, foo2ç, foo2ê'), keywords: 'foo2à, foo2ç, foo2ê', picture: '/img/foo2.png'},
{name: 'foo3', keywords_query: normalize('foo3à, foo3ç, foo3ê'), keywords: 'foo3à, foo3ç, foo3ê', picture: '/img/foo3.png'}
];

var foo = new Bloodhound({
  datumTokenizer: Bloodhound.tokenizers.obj.whitespace(normalize('keywords_query')),
  queryTokenizer: queryTokenizer,
  local: foo_keywords
});

foo.initialize();

$('.typeahead').typeahead(
    {
        hint: true,
        highlight: true,
        minLength: 1,
    },
    {
        name: 'foo',
        displayKey: 'name',
        source: foo.ttAdapter(),
        templates: {
            suggestion: function(el){return '<div style=\"display:table;\"><div style=\"display:table-cell;text-align:left;margin:0;width:100px;\"><img src="'+el.picture+'" style=\'width:100px;height:auto;\'/></div><div style=\"display:table-cell;text-align:left;margin:0;padding:10px;vertical-align:top;">'+el.name+'<br><font style=\"font-size:0.6em;color:#7b7b7b;\">'+el.keywords+'</font></div></div>'}
        }
    }
);

然后只使用ttAdapter()的两个来源。