java - 在 Lucene 中使用 WikipediaTokenizer 的示例

标签 java parsing programming-languages lucene wikipedia

我想在 lucene 项目中使用 WikipediaTokenizer - http://lucene.apache.org/java/3_0_2/api/contrib-wikipedia/org/apache/lucene/wikipedia/analysis/WikipediaTokenizer.html但是我从来没有用过lucene。我只想将维基百科字符串转换为标记列表。但是,我看到这个类中只有四个方法可用,end、incrementToken、reset、reset(reader)。谁能给我一个例子来使用它。

谢谢。

最佳答案

在 Lucene 3.0 中,next() 方法被移除。现在您应该使用 incrementToken 遍历标记,当您到达输入流的末尾时它返回 false。要获取每个 token ,您应该使用 AttributeSource 的方法类(class)。根据您想要获取的属性(term、type、payload 等),您需要使用 addAttribute 方法将相应属性的类类型添加到您的 tokenizer。

以下部分代码示例来自WikipediaTokenizer的测试类,下载Lucene的源码即可找到。

...
WikipediaTokenizer tf = new WikipediaTokenizer(new StringReader(test));
int count = 0;
int numItalics = 0;
int numBoldItalics = 0;
int numCategory = 0;
int numCitation = 0;
TermAttribute termAtt = tf.addAttribute(TermAttribute.class);
TypeAttribute typeAtt = tf.addAttribute(TypeAttribute.class);

while (tf.incrementToken()) {
  String tokText = termAtt.term();
  //System.out.println("Text: " + tokText + " Type: " + token.type());
  String expectedType = (String) tcm.get(tokText);
  assertTrue("expectedType is null and it shouldn't be for: " + tf.toString(), expectedType != null);
  assertTrue(typeAtt.type() + " is not equal to " + expectedType + " for " + tf.toString(), typeAtt.type().equals(expectedType) == true);
  count++;
  if (typeAtt.type().equals(WikipediaTokenizer.ITALICS)  == true){
    numItalics++;
  } else if (typeAtt.type().equals(WikipediaTokenizer.BOLD_ITALICS)  == true){
    numBoldItalics++;
  } else if (typeAtt.type().equals(WikipediaTokenizer.CATEGORY)  == true){
    numCategory++;
  }
  else if (typeAtt.type().equals(WikipediaTokenizer.CITATION)  == true){
    numCitation++;
  }
}
...

关于java - 在 Lucene 中使用 WikipediaTokenizer 的示例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3924543/

相关文章:

java - 对象作为数组

java - 输出 JSON 相对于纯 HTML 的好处

http - 页面在解析时返回不同的内容

ms-access - MS Access 编程概述

java - Java进程中出现OutOfMemoryException,但Used Heap大约是Used Size的一半

java - 不同型号的TableView

python - 使用 BeautifulSoup 更改元素值返回空元素

可以确定特定节点的字体大小的 HTML 解析器

php - Python/Django 到 PHP 的过渡?学习曲线错误?

clojure - 怎么包含?在 Clojure 工作中?