【问题标题】:lucene implement custom scoring to relevancylucene 实现对相关性的自定义评分
【发布时间】:2014-06-23 20:50:41
【问题描述】:

我正在尝试将潜在狄利克雷分配 (LDA) 注入到对搜索文档的相关性进行评分中,结果卡住了。我才刚刚开始使用 Lucene。我正在使用“Lucene in Action”中的代码开始。

计划是尝试混合使用默认 tf-idf 模型的权重以及查询的主题向量与每个文档之间的余弦相似度。例如0.5 * tfidf + 0.5 * cos(Q,D)

我尝试在索引期间存储每个文档的主题向量,在每个分数索引之间使用分隔符:

doc.add(new Field("lda score", "0.200|0.111|0.4999",
                  Field.Store.NO,
                  Field.Index.NOT_ANALYZED_NO_NORMS));

然后在搜索过程中:

//tfidf 
Query q = new QueryParser(Version.LUCENE_30,
                          "content",
                          new StandardAnalyzer(
                            Version.LUCENE_30))
             .parse("some text here");
FieldScoreQuery qf = new FieldScoreQuery("lda score",
                                         FieldScoreQuery.Type.BYTE);
CustomScoreQuery customQ = new CustomScoreQuery(q, qf) {
  public CustomScoreProvider getCustomScoreProvider(IndexReader r) {
    return new CustomScoreProvider(r) {
      public float customScore(int doc,
                               float tfidfScore,
                               float ldaScore) {
        return 0.5*tfidfScore + 0.5*ldaScore);
} };

显然,我需要帮助的是FieldScoreQuery 部分。如何读取查询字符串、运行 lda 推理(与 lucene 分开分析)和余弦相似度以生成供 CustomScoreQuery 使用的分数?

这是执行此操作的正确方法,还是我需要进入Similarity 课程?一些帮助我入门的代码示例将不胜感激。

【问题讨论】:

    标签: java lucene


    【解决方案1】:

    据我所知,您不能将字符串用作 FieldScoreQuery。如果您需要 3 个值,请使用 3 个字段并使用 3 个 FLOAT 类型的不同 FieldScoreQuery。

    我使用数字字段

    luc_doc.add(new NumericField( FIELD_NAME,Field.Store.NO,true).setFloatValue(x));

    然后在 CustomScoreProvider 中实现覆盖该方法

    public float customScore(int doc, float subQueryScore, float[] valSrcScores)

    您将在 valSrcScores 数组中获得 3 个值。

    【讨论】:

      猜你喜欢
      • 2016-02-01
      • 2016-09-12
      • 1970-01-01
      • 2013-04-17
      • 1970-01-01
      • 2015-01-27
      • 2011-08-20
      • 1970-01-01
      • 2017-03-04
      相关资源
      最近更新 更多