arj*_*nes 13 java scala textmatching booleanquery
我正在寻找一个可以接受用户查询和文本的Java/Scala库,如果有匹配则返回.
我正在处理一个信息流,即:Twitter Stream,并且无法负担使用批处理过程,我需要实时评估每条推文,而不是通过Lucene RAMDisk对其进行索引并稍后查询.
可以使用ANTLR创建一个解析器/词法分析器,但这是常见的用法,我不相信之前没有人创建过lib.
来自TextQuery Ruby库的一些示例正是我所需要的:
TextQuery.new("'to be' OR NOT 'to_be'").match?("to be") # => true
TextQuery.new("-test").match?("some string of text") # => true
TextQuery.new("NOT test").match?("some string of text") # => true
TextQuery.new("a AND b").match?("b a") # => true
TextQuery.new("a AND b").match?("a c") # => false
q = TextQuery.new("a AND (b AND NOT (c OR d))")
q.match?("d a b") # => false
q.match?("b") # => false
q.match?("a b cdefg") # => true
TextQuery.new("a~").match?("adf") # => true
TextQuery.new("~a").match?("dfa") # => true
TextQuery.new("~a~").match?("daf") # => true
TextQuery.new("2~a~1").match?("edaf") # => true
TextQuery.new("2~a~2").match?("edaf") # => false
TextQuery.new("a", :ignorecase => true).match?("A b cD") # => true
Run Code Online (Sandbox Code Playgroud)
一旦它在Ruby中实现它不适合我的平台,我也不能仅仅为我们的解决方案使用JRuby:
我发现了一个类似的问题,但无法从中得到答案: 布尔查询/表达式到一个具体的语法树
谢谢!
鉴于您正在进行文本搜索,我会尝试利用 Lucene 提供的一些基础设施。也许您可以创建一个QueryParser并调用parse以返回一个Query. Query 的可实例化子类有:
TermQuery
MultiTermQuery
BooleanQuery
WildcardQuery
PhraseQuery
PrefixQuery
MultiPhraseQuery
FuzzyQuery
TermRangeQuery
NumericRangeQuery
SpanQuery
Run Code Online (Sandbox Code Playgroud)
然后您可以使用模式匹配来实现匹配对您的应用程序的含义:
def match_?(tweet: String, query: Query): Boolean = query match {
case q: TermQuery => tweet.contains(q.getTerm.text)
case q: BooleanQuery =>
// return true if all must clauses are satisfied
// call match_? recursively
// you need to cover all subclasses above
case _ => false
}
val q = queryParser.parse(userQuery)
val res = match_?(tweet, q)
Run Code Online (Sandbox Code Playgroud)
这是一个实现。它肯定有错误,但你会明白这个想法,它显示了一个有效的概念证明。它重新使用默认 Lucene QueryParser 的语法、文档和语法。
| 归档时间: |
|
| 查看次数: |
1940 次 |
| 最近记录: |