使用 elasticsearch 的真实单词拼写检查器

real-word spell-checker with elasticsearch

我已经熟悉 Elasticsearch 的拼写检查器,我可以使用 suggest API 构建一个简单的拼写检查器。问题是,有一种拼错的词,叫做 "real-word" 拼错。真实单词拼写错误发生在书写单词拼写错误时,创建了索引数据中存在的另一个单词,因此词汇拼写检查器无法纠正它,因为单词在词汇上是正确的。

例如,考虑查询 "How to bell my laptop?"。"bell" 的用户意味着 "sell",但 "bell" 出现在索引词汇表中。所以拼写检查器让它成为现实。

查找和纠正真实单词拼写错误的想法是利用索引数据 n-gram 的频率。如果当前 n-gram 的频率非常低,另一方面在索引数据中有一个非常相似的高频 n-gram,那么我们很可能有一个真实的单词拼写错误。

我想知道是否有一种方法可以使用 elasticsearch API 来实现这种拼写检查?

在我搜索了一段时间后,我发现使用 phrase_suggester 可以实现这样的事情。

    POST v2_201911/_search
{
  "suggest": {
    "text": "how to bell my laptop",
    "simple_phrase": {
      "phrase": {
        "field": "content",
        "gram_size": 2,
        "real_word_error_likelihood": 0.95,
        "direct_generator": [
          {
            "field": "content",
            "suggest_mode": "always",
            "prefix_length": 0,
            "min_word_length": 1
          }
        ],
        "highlight": {
          "pre_tag": "<em>",
          "post_tag": "</em>"
        }
      }
    }
  }
}

根据documentation

real_word_error_likelihood :

The likelihood of a term being a misspelled even if the term exists in the dictionary. The default is 0.95, meaning 5% of the real words are misspelled.