Java 在Lucene中处理带否定项的文档 是否可以在Lucene中建立索引,以考虑负面上下文?< /P>
因此,如果文本包含“非黑车”通过查询搜索“黑车”将不会返回相关文档 我需要自己写吗?还是有现有的解决方案或更好的方法Java 在Lucene中处理带否定项的文档 是否可以在Lucene中建立索引,以考虑负面上下文?< /P>,java,lucene,full-text-search,nlp,Java,Lucene,Full Text Search,Nlp,因此,如果文本包含“非黑车”通过查询搜索“黑车”将不会返回相关文档 我需要自己写吗?还是有现有的解决方案或更好的方法 如果重要文档是芬兰语。此时,我正在使用以下带有自定义筛选器的解决方案,但仍然对更好的解决方案感兴趣: 我已经编写了名为NegationFilter的定制TokenFilter,它读取底层TokenStream中的术语,如果它发现一些否定词(“ei”,“eivät”在芬兰语中;“no”,“not”在英语中),它会在以下几个术语后面加上后缀“-not”(由negationTraceS
如果重要文档是芬兰语。此时,我正在使用以下带有自定义筛选器的解决方案,但仍然对更好的解决方案感兴趣: 我已经编写了名为
NegationFilter
的定制TokenFilter
,它读取底层TokenStream
中的术语,如果它发现一些否定词(“ei”,“eivät”在芬兰语中;“no”,“not”在英语中),它会在以下几个术语后面加上后缀“-not”(由negationTraceSize
变量控制的术语数量)
因此,“非黑车”将被解析为[黑车非,车]
或[黑车非,车非]
,使用否定traceSize=2
它仍然需要改进,因为它不会识别诸如“不是真正的黑色汽车”或“不是黑色或黄色汽车”之类的短语,但在简单的情况下可以工作
如果感兴趣,使用scala编写代码:
final class NegationFilter(input: TokenStream,
negations: Set[String], negationTraceSize: Int = 2
) extends TokenFilter(input) {
private val termAtt = addAttribute(classOf[CharTermAttribute])
private val posInc = addAttribute(classOf[org.apache.lucene.analysis.tokenattributes.PositionIncrementAttribute])
private var inNegative: Boolean = false
private var negativeOffset: Int = 0
override def reset(): Unit = {
super.reset()
inNegative = false
negativeOffset = 0
}
override def incrementToken(): Boolean = {
if (input.incrementToken) {
if (negations(termAtt.toString)) {
inNegative = true
negativeOffset = 0
return incrementToken()
} else {
negativeOffset = negativeOffset + posInc.getPositionIncrement
if (negativeOffset > negationTraceSize) {
inNegative = false
}
}
if (inNegative) {
termAtt.append("-not")
}
return true
}
else {
return false
}
}
}
目前,我正在使用以下带有自定义过滤器的解决方案,但仍然对更好的解决方案感兴趣: 我已经编写了名为
NegationFilter
的定制TokenFilter
,它读取底层TokenStream
中的术语,如果它发现一些否定词(“ei”,“eivät”在芬兰语中;“no”,“not”在英语中),它会在以下几个术语后面加上后缀“-not”(由negationTraceSize
变量控制的术语数量)
因此,“非黑车”将被解析为[黑车非,车]
或[黑车非,车非]
,使用否定traceSize=2
它仍然需要改进,因为它不会识别诸如“不是真正的黑色汽车”或“不是黑色或黄色汽车”之类的短语,但在简单的情况下可以工作
如果感兴趣,使用scala编写代码:
final class NegationFilter(input: TokenStream,
negations: Set[String], negationTraceSize: Int = 2
) extends TokenFilter(input) {
private val termAtt = addAttribute(classOf[CharTermAttribute])
private val posInc = addAttribute(classOf[org.apache.lucene.analysis.tokenattributes.PositionIncrementAttribute])
private var inNegative: Boolean = false
private var negativeOffset: Int = 0
override def reset(): Unit = {
super.reset()
inNegative = false
negativeOffset = 0
}
override def incrementToken(): Boolean = {
if (input.incrementToken) {
if (negations(termAtt.toString)) {
inNegative = true
negativeOffset = 0
return incrementToken()
} else {
negativeOffset = negativeOffset + posInc.getPositionIncrement
if (negativeOffset > negationTraceSize) {
inNegative = false
}
}
if (inNegative) {
termAtt.append("-not")
}
return true
}
else {
return false
}
}
}