Javascript lunr.js的希腊语言支持

Javascript lunr.js的希腊语言支持,javascript,full-text-search,static-site,non-latin,lunrjs,Javascript,Full Text Search,Static Site,Non Latin,Lunrjs,在lunr中为希腊语单词注册新的词干分析器函数并不像预期的那样有效。是我在代码笔上的代码。我没有收到任何错误,函数stemWord()单独使用时可以正常工作,但它无法阻止lunr中的单词。 下面是代码示例: function stemWord(w) { // code that returns the stemmed word }; // create the new function greekStemmer = function (token) { return stemWord(

在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!'
  })

在lunr中,词干分析器作为管道函数实现。索引文档时对文档中的每个单词执行管道函数,搜索时对搜索查询中的每个单词执行管道函数

为了使函数在管道中工作,它必须实现一个非常简单的接口。它需要接受单个字符串作为输入,并且必须以字符串作为输出进行响应

因此,一个非常简单(且无用)的管道函数如下所示:

var simplePipelineFunction = function (word) {
  return word
}
要实际使用此管道功能,我们需要做两件事:

  • 将其注册为管道函数,这允许lunr正确地序列化和反序列化管道
  • 将其添加到索引管道中
  • 看起来是这样的:

    // 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)
    }
    
    现在,您可以接受上面的内容,并交换管道函数的实现。因此,它可以使用您找到的希腊词干分析器来对单词进行词干处理,而不是只返回未更改的单词,可能如下所示:

    // 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的默认语言是英语,因此,默认情况下,它包括专门用于英语的管道函数。英语和希腊语差异很大,您可能会遇到使用英语默认值索引希腊语单词的问题,因此我们需要执行以下操作:

    var simplePipelineFunction = function (word) {
      return word
    }
    
  • 用特定于语言的词干分析器替换默认词干分析器
  • 删除默认的修剪器,它不能很好地处理非拉丁字符
  • 替换/删除默认的停止字过滤器,它不太可能在英语以外的语言上使用
  • trimmer和stop word filter是作为管道函数实现的,因此实现特定于语言的函数与stemmer类似

    因此,要为希腊文设置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创建语言扩展的例子。你甚至可以提交一个希腊语

    EDIT看起来像我不知道
    lunr.Pipeline
    API一样,没有
    replace
    函数,我们只是在要删除的函数后插入替换,然后删除它


    编辑添加此内容以在将来帮助他人。。。事实证明,问题出在lunr中的令牌的外壳上。lunr希望将所有令牌都视为小写,这是在中完成的,没有任何可配置性。对于大多数语言处理函数来说,这不是问题,事实上,大多数函数都假设单词的大小写较低。在本例中,由于希腊语词干分析的复杂性,希腊语词干分析只对大写单词进行词干分析(我不是希腊语使用者,因此无法评论词干分析的复杂性)。一个解决方案是在调用希腊词干分析器之前转换为大写,然后在将标记传递到管道的其余部分之前转换回小写。

    我建议您将此作为问题发布到Github存储库中。我编辑了我的问题,您能看一下代码吗?词干分析器本身可以工作,但当我尝试在lunr.js文件中这样替换它时,它不工作。到底是什么不工作?你犯了什么错误?如果没有这些信息,这有点难以帮助。我已经用我正在使用的代码更新了我的问题。没有错误,但这些词似乎没有词干。你能看一下代码并告诉我它是否正确吗?还有什么方法可以调试这个东西吗?像console.log一样,lunr搜索的最后一个单词?它现在可以工作了,我没有考虑lunr将标记转换为小写,而词干分析器只处理大写。