2016-03-02 25 views
1

我最近st type於先頭/血獵犬,並試圖在本地數據的頁面上實現它。把它全部運行起來,但還剩下一個問題。鍵入以管理同義詞?

是否可以實現某種靜態同義詞列表?我想處理的例子,如:

  • 醫院<> Sygehus
  • 立方<>庫貝

搜索一個或另一個要麼在兩個搜索,或者搜索時,如果沒有結果分別對返回第一個字。該列表可能不會超過少數幾個字,所以我正在考慮手動更新列表。

但是在哪裏/如何做到這一點?我的設置類似於typeahead.js上多個數據集的示例(typeahead example

回答

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()使用這兩個源。

+0

現在我唯一的問題是,結果將有重複的結果不在規範化列表中。 hmmmm – user1865820