- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我有一个程序接受输入查询并根据其 TFIDF 得分对相似文档进行排名。问题是,我想添加一些关键字并将它们也视为“输入”。这些关键字对于每个查询都是不同的。
例如,如果查询是"Logic Based Knowledge Representation"
,则单词如下:
Level 0 keywords: [logic, base, knowledg, represent]
Level 1 keywords: [tempor, modal, logic, resolut, method, decis, problem,
reason, revis, hybrid, represent]
Level 2 keywords: [classif, queri, process, techniqu, candid, semant, data,
model, knowledg, base, commun, softwar, engin, subsumpt,
kl, undecid, classic, structur, object, field]
我想以不同的方式对待评分,例如,对于文档中等于 0 级单词的术语,我想将分数乘以 1。对于文档中等于 1 级单词的术语,乘以得分为0.8。最后,对于文档中等于第 2 级单词的术语,将分数乘以 0.64。
我的目的是扩展输入查询,同时确保包含更多级别 0 关键字的文档被视为更重要,而包含级别 1 和 2 关键字的文档较少(即使输入已扩展)。我没有把它包括在我的程序中。到目前为止,我的程序只计算查询中所有文档的 TFIDF 分数并对结果进行排名:
public class Ranking{
private static int maxHits = 2000000;
public static void main(String[] args) throws Exception {
System.out.println("Enter your paper title: ");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String paperTitle = null;
paperTitle = br.readLine();
// CitedKeywords ckeywords = new CitedKeywords();
// ckeywords.readDataBase(paperTitle);
String querystr = args.length > 0 ? args[0] :paperTitle;
StandardAnalyzer analyzer = new StandardAnalyzer(Version.LUCENE_42);
Query q = new QueryParser(Version.LUCENE_42, "title", analyzer)
.parse(querystr);
IndexReader reader = DirectoryReader.open(
FSDirectory.open(
new File("E:/Lucene/new_bigdataset_index")));
IndexSearcher searcher = new IndexSearcher(reader);
VSMSimilarity vsmSimiliarty = new VSMSimilarity();
searcher.setSimilarity(vsmSimiliarty);
TopDocs hits = searcher.search(q, maxHits);
ScoreDoc[] scoreDocs = hits.scoreDocs;
PrintWriter writer = new PrintWriter("E:/Lucene/result/1.txt", "UTF-8");
int counter = 0;
for (int n = 0; n < scoreDocs.length; ++n) {
ScoreDoc sd = scoreDocs[n];
float score = sd.score;
int docId = sd.doc;
Document d = searcher.doc(docId);
String fileName = d.get("title");
String year = d.get("pub_year");
String paperkey = d.get("paperkey");
System.out.printf("%s,%s,%s,%4.3f\n", paperkey, fileName, year, score);
writer.printf("%s,%s,%s,%4.3f\n", paperkey, fileName, year, score);
++counter;
}
writer.close();
}
}
--
import org.apache.lucene.index.FieldInvertState;
import org.apache.lucene.search.similarities.DefaultSimilarity;
public class VSMSimilarity extends DefaultSimilarity{
// Weighting codes
public boolean doBasic = true; // Basic tf-idf
public boolean doSublinear = false; // Sublinear tf-idf
public boolean doBoolean = false; // Boolean
//Scoring codes
public boolean doCosine = true;
public boolean doOverlap = false;
private static final long serialVersionUID = 4697609598242172599L;
// term frequency in document =
// measure of how often a term appears in the document
public float tf(int freq) {
// Sublinear tf weighting. Equation taken from [1], pg 127, eq 6.13.
if (doSublinear){
if (freq > 0){
return 1 + (float)Math.log(freq);
} else {
return 0;
}
} else if (doBoolean){
return 1;
}
// else: doBasic
// The default behaviour of Lucene is sqrt(freq),
// but we are implementing the basic VSM model
return freq;
}
// inverse document frequency =
// measure of how often the term appears across the index
public float idf(int docFreq, int numDocs) {
if (doBoolean || doOverlap){
return 1;
}
// The default behaviour of Lucene is
// 1 + log (numDocs/(docFreq+1)),
// which is what we want (default VSM model)
return super.idf(docFreq, numDocs);
}
// normalization factor so that queries can be compared
public float queryNorm(float sumOfSquaredWeights){
if (doOverlap){
return 1;
} else if (doCosine){
return super.queryNorm(sumOfSquaredWeights);
}
// else: can't get here
return super.queryNorm(sumOfSquaredWeights);
}
// number of terms in the query that were found in the document
public float coord(int overlap, int maxOverlap) {
if (doOverlap){
return 1;
} else if (doCosine){
return 1;
}
// else: can't get here
return super.coord(overlap, maxOverlap);
}
// Note: this happens an index time, which we don't take advantage of
// (too many indices!)
public float computeNorm(String fieldName, FieldInvertState state){
if (doOverlap){
return 1;
} else if (doCosine){
return super.computeNorm(state);
}
// else: can't get here
return super.computeNorm(state);
}
}
下面是我当前程序的示例输出(没有提升分数):
3086,Logic Based Knowledge Representation.,1999,5.165
33586,A Logic for the Representation of Spatial Knowledge.,1991,4.663
328937,Logic Programming for Knowledge Representation.,2007,4.663
219720,Logic for Knowledge Representation.,1984,4.663
487587,Knowledge Representation with Logic Programs.,1997,4.663
806195,Logic Programming as a Representation of Knowledge.,1983,4.663
806833,The Role of Logic in Knowledge Representation.,1983,4.663
744914,Knowledge Representation and Logic Programming.,2002,4.663
1113802,Knowledge Representation in Fuzzy Logic.,1989,4.663
984276,Logic Programming and Knowledge Representation.,1994,4.663
任何人都可以告诉我如何为我上面提到的条件添加分数吗? Lucene有提供这种功能吗?我可以将它集成到 VSMSimilarity 类中吗?
编辑:我在 Lucene 文档中找到了这个:
public void setBoost(float b)
将此查询子句的提升设置为 b。匹配此子句的文档将(除了正常权重之外)将其分数乘以 b。
不幸的是,这似乎增加了文档级别的分数。我想做一个学期水平的分数乘法,但我还没有找到这样做的方法。因此,如果文档包含来自 level0 和 level1 的单词,则只有来自 level1 的术语将乘以 0.8,例如
最佳答案
您可以使用 Lucene 术语提升。
像这样扩充您的查询(假设 OR 是默认运算符)
logic base knowledge representation temporal^0.8 modal^0.8 classification^0.64...
并使用标准相似性提供者之一。
PS:在您的示例中找到了 LUCENE_42
。几乎所有版本的 Lucene 都存在这个特性(我记得它在 2.4.9 中就有)。
关于java - 在 Lucene TFIDF 中为特定条件自定义分数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28470342/
好的,所以我编辑了以下... 只需将以下内容放入我的 custom.css #rt-utility .rt-block {CODE HERE} 但是当我尝试改变... 与 #rt-sideslid
在表格 View 中,我有一个自定义单元格(在界面生成器中高度为 500)。在该单元格中,我有一个 Collection View ,我按 (10,10,10,10) 固定到边缘。但是在 tablev
对于我的无能,我很抱歉,但总的来说,我对 Cocoa、Swift 和面向对象编程还很陌生。我的主要来源是《Cocoa Programming for OS X》(第 5 版),以及 Apple 的充满
我正在使用 meta-tegra 为我的 NVIDIA Jetson Nano 构建自定义图像。我需要 PyTorch,但没有它的配方。我在设备上构建了 PyTorch,并将其打包到设备上的轮子中。现
在 jquery 中使用 $.POST 和 $.GET 时,有没有办法将自定义变量添加到 URL 并发送它们?我尝试了以下方法: $.ajax({type:"POST", url:"file.php?
Traefik 已经默认实现了很多中间件,可以满足大部分我们日常的需求,但是在实际工作中,用户仍然还是有自定义中间件的需求,为解决这个问题,官方推出了一个 Traefik Pilot[1] 的功
我想让我的 CustomTextInputLayout 将 Widget.MaterialComponents.TextInputLayout.OutlinedBox 作为默认样式,无需在 XML 中
我在 ~/.emacs 中有以下自定义函数: (defun xi-rgrep (term) (grep-compute-defaults) (interactive "sSearch Te
我有下表: 考虑到每个月的权重,我的目标是在 5 个月内分散 10,000 个单位。与 10,000 相邻的行是我最好的尝试(我在这上面花了几个小时)。黄色是我所追求的。 我试图用来计算的逻辑如下:计
我的表单中有一个字段,它是文件类型。当用户点击保存图标时,我想自然地将文件上传到服务器并将文件名保存在数据库中。我尝试通过回显文件名来测试它,但它似乎不起作用。另外,如何将文件名添加到数据库中?是在模
我有一个 python 脚本来发送电子邮件,它工作得很好,但问题是当我检查我的电子邮件收件箱时。 我希望该用户名是自定义用户名,而不是整个电子邮件地址。 最佳答案 发件人地址应该使用的格式是: You
我想减小 ggcorrplot 中标记的大小,并减少文本和绘图之间的空间。 library(ggcorrplot) data(mtcars) corr <- round(cor(mtcars), 1)
GTK+ noob 问题在这里: 是否可以自定义 GtkFileChooserButton 或 GtkFileChooserDialog 以删除“位置”部分(左侧)和顶部的“位置”输入框? 我实际上要
我正在尝试在主页上使用 ajax 在 magento 中使用 ajax 显示流行的产品列表,我可以为 5 或“N”个产品执行此操作,但我想要的是将分页工具栏与结果集一起添加. 这是我添加的以显示流行产
我正在尝试使用 PasswordResetForm 内置函数。 由于我想要自定义表单字段,因此我编写了自己的表单: class FpasswordForm(PasswordResetForm):
据我了解,新的 Angular 7 提供了拖放功能。我搜索了有关 DnD 的 Tree 组件,但没有找到与树相关的内容。 我在 Stackblitz 上找到的一个工作示例.对比drag'ndrop功能
我必须开发一个自定义选项卡控件并决定使用 WPF/XAML 创建它,因为我无论如何都打算学习它。完成后应该是这样的: 到目前为止,我取得了很好的进展,但还有两个问题: 只有第一个/最后一个标签项应该有
我要定制xtable用于导出到 LaTeX。我知道有些问题是关于 xtable在这里,但我找不到我要找的具体东西。 以下是我的表的外观示例: my.table <- data.frame(Specif
用ejs在这里显示日期 它给我结果 Tue Feb 02 2016 16:02:24 GMT+0530 (IST) 但是我需要表现为 19th January, 2016 如何在ejs中执行此操作?
我想问在 JavaFX 中使用自定义对象制作 ListView 的最佳方法,我想要一个每个项目如下所示的列表: 我搜了一下,发现大部分人都是用细胞工厂的方法来做的。有没有其他办法?例如使用客户 fxm
我是一名优秀的程序员,十分优秀!