【发布时间】:2014-04-18 15:10:24
【问题描述】:
我正在寻找一种简单的方法来获取包含 5-10 个描述特定文档的最重要术语的列表。它甚至可以基于特定的字段,比如项目描述。
我认为这应该很容易。 Solr 无论如何都会根据每个术语在文档中的相对出现次数与它在所有文档中的总体出现次数(tf-idf)进行评分
然而,我找不到如何将我想要的文档传递给 Solr 并获取我想要的术语列表的方法。
【问题讨论】:
我正在寻找一种简单的方法来获取包含 5-10 个描述特定文档的最重要术语的列表。它甚至可以基于特定的字段,比如项目描述。
我认为这应该很容易。 Solr 无论如何都会根据每个术语在文档中的相对出现次数与它在所有文档中的总体出现次数(tf-idf)进行评分
然而,我找不到如何将我想要的文档传递给 Solr 并获取我想要的术语列表的方法。
【问题讨论】:
您可能正在寻找MoreLikeThis component,特别是启用了 mlt.interestingTerms 标志。
【讨论】:
如果您只需要文档中的最重要的术语,您可以使用 Term Vector Component,假设您的字段有 termVectors="true"
可以查询 tv.tf_idf 并取前 n 条得分最高的词。
【讨论】:
我认为您可能想要使用某些类型的单词,通常名词用于此目的。我曾经为一个聚类例程做过类似的事情,我使用 OpenNLP 词性标注器来提取所有名词短语(使用分块器或词性标注器),然后简单地将每个术语放在 HashMap 中。 这是一些使用句子分块的代码,但是使用直接的词性可能会是一个微不足道的改编(但如果您需要帮助,请告诉我)。 代码所做的是提取每个词性,然后对词性进行分块,循环块以获得名词短语,然后添加到词频哈希图。真的很简单。您可以选择跳过所有 OpenNLP 的内容,但您需要进行大量的噪声消除等操作。无论如何...看看。
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import opennlp.tools.chunker.ChunkerME;
import opennlp.tools.chunker.ChunkerModel;
import opennlp.tools.postag.POSModel;
import opennlp.tools.postag.POSTaggerME;
import opennlp.tools.tokenize.TokenizerME;
import opennlp.tools.tokenize.TokenizerModel;
import opennlp.tools.util.Span;
/**
*
* Extracts noun phrases from a sentence. To create sentences using OpenNLP use
* the SentenceDetector classes.
*/
public class OpenNLPNounPhraseExtractor {
static final int N = 2;
public static void main(String[] args) {
try {
HashMap<String, Integer> termFrequencies = new HashMap<>();
String modelPath = "c:\\temp\\opennlpmodels\\";
TokenizerModel tm = new TokenizerModel(new FileInputStream(new File(modelPath + "en-token.zip")));
TokenizerME wordBreaker = new TokenizerME(tm);
POSModel pm = new POSModel(new FileInputStream(new File(modelPath + "en-pos-maxent.zip")));
POSTaggerME posme = new POSTaggerME(pm);
InputStream modelIn = new FileInputStream(modelPath + "en-chunker.zip");
ChunkerModel chunkerModel = new ChunkerModel(modelIn);
ChunkerME chunkerME = new ChunkerME(chunkerModel);
//this is your sentence
String sentence = "Barack Hussein Obama II is the 44th awesome President of the United States, and the first African American to hold the office.";
//words is the tokenized sentence
String[] words = wordBreaker.tokenize(sentence);
//posTags are the parts of speech of every word in the sentence (The chunker needs this info of course)
String[] posTags = posme.tag(words);
//chunks are the start end "spans" indices to the chunks in the words array
Span[] chunks = chunkerME.chunkAsSpans(words, posTags);
//chunkStrings are the actual chunks
String[] chunkStrings = Span.spansToStrings(chunks, words);
for (int i = 0; i < chunks.length; i++) {
String np = chunkStrings[i];
if (chunks[i].getType().equals("NP")) {
if (termFrequencies.containsKey(np)) {
termFrequencies.put(np, termFrequencies.get(np) + 1);
} else {
termFrequencies.put(np, 1);
}
}
}
System.out.println(termFrequencies);
} catch (IOException e) {
}
}
}
【讨论】: