在lunr中为希腊语单词注册一个新的词干器函数并不像预期的那样有效。这里是我的代码。我没有收到任何错误,函数stemWord()在单独使用时运行良好,但是它无法阻止lunr中的单词。下面是代码的示例:
function stemWord(w) {
// code that returns the stemmed word
};
// create the new function
greekStemmer = function (token) {
return stemWord(token);
};
// register it with lunr.Pipeline, this allows you to still serialise the index
lunr.Pipeline.registerFunction(greekStemmer, 'greekStemmer')
var index = lunr(function () {
this.field('title', {boost: 10})
this.field('body')
this.ref('id')
this.pipeline.remove(lunr.trimmer) // it doesn't work well with non-latin characters
this.pipeline.add(greekStemmer)
})
index.add({
id: 1,
title: 'ΚΑΠΟΙΟΣ',
body: 'Foo foo foo!'
})
index.add({
id: 2,
title: 'ΚΑΠΟΙΕΣ',
body: 'Bar bar bar!'
})
index.add({
id: 3,
title: 'ΤΙΠΟΤΑ',
body: 'Bar bar bar!'
})发布于 2016-09-07 19:38:21
在lunr中,词干分析器被实现为管道函数。在文档索引时对文档中的每个单词执行管道函数,在搜索时对搜索查询中的每个单词执行管道函数。
要使函数在管道中工作,它必须实现一个非常简单的接口。它需要接受单个字符串作为输入,并且必须以字符串作为输出进行响应。
因此,一个非常简单(且无用的)管道函数如下所示:
var simplePipelineFunction = function (word) {
return word
}要真正利用这个管道功能,我们需要做两件事:
看起来是这样的:
// registering our pipeline function with the name 'simplePipelineFunction'
lunr.Pipeline.registerFunction(simplePipelineFunction, 'simplePipelineFunction')
var idx = lunr(function () {
// adding the pipeline function to our indexes pipeline
// when defining the pipeline
this.pipeline.add(simplePipelineFunction)
})现在,您可以使用上面的内容,并交换我们的管道函数的实现。所以,不只是返回不变的单词,它可以使用希腊语词干器来阻止这个词,也许是这样的:
var myGreekStemmer = function (word) {
// I don't know how to use the greek stemmer, but I think
// its safe to assume it won't be that different than this
return greekStem(word)
}不过,要使lunr适应英语以外的语言,不仅仅需要添加词干器。lunr的默认语言是英语,因此,默认情况下,它包括专门用于英语的管道函数。英语和希腊语有很大的不同,您可能会遇到一些问题,试图用英语缺省值索引希腊语单词,因此我们需要做以下工作:
剪裁器和停止字过滤器是作为管道函数实现的,因此实现特定于语言的词干过滤器也是类似的。
因此,要为希腊语设置lunr,您可以这样做:
var idx = lunr(function () {
this.pipeline.after(lunr.stemmer, greekStemmer)
this.pipeline.remove(lunr.stemmer)
this.pipeline.after(lunr.trimmer, greekTrimmer)
this.pipeline.remove(lunr.trimmer)
this.pipeline.after(lunr.stopWordFilter, greekStopWordFilter)
this.pipeline.remove(lunr.stopWordFilter)
// define the index as normal
this.ref('id')
this.field('title')
this.field('body')
})为了获得更多的灵感,您可以看看优秀的lunr-语言项目,它有许多为lunr创建语言扩展的示例。你甚至可以为希腊语提交一个!
编辑看起来好像我不知道lunr.Pipeline API,就像我想的那样,没有replace函数,相反,我们只是在要删除的函数之后插入替换,然后删除它。
编辑添加这个,以帮助其他人在未来.事实证明,问题的根源在于在隆尔范围内的令牌的外壳。lunr希望将所有令牌作为小写来处理,这是在令牌器中完成的,没有任何可配置性。对于大多数语言处理功能来说,这并不是一个问题,事实上,大多数假定词都是小写的。在这种情况下,希腊语词干词干只有大写词的词干,因为词根在希腊语中的复杂性(我不是说希腊语的人,所以不能评论词干词干有多复杂)。一种解决方案是在调用希腊词干器之前将其转换为大写,然后在将令牌传递到管道的其余部分之前将其转换回小写。
https://stackoverflow.com/questions/39344299
复制相似问题