简体   繁体   English

解析器字符串布尔表达式

[英]parser String boolean expression


I should build a search engine which allows anyone to enter any Boolean expression like (word1 + word2) - !word3 - where + means and , - means or and ! 我应该构建一个搜索引擎,使任何人都可以输入任何布尔表达式,例如(word1 + word2) - !word3其中+表示and-表示or! means not . 意思not

then searches these words in database and return the documents that achieve the expression.. 然后在数据库中搜索这些单词并返回实现该表达式的文档。
so I used dictionary like this: 所以我用这样的字典:

  Dictionary<string, List<int>> WordsAndDoc = new Dictionary<string, List<int>>();

and I searched for each word in database then added it with list of document id in dictionary 我在数据库中搜索了每个单词,然后在字典中添加了文档ID列表
I declared two method. 我声明了两种方法。
first to merge two List: 首先合并两个列表:

public List<int> Intersect(List<int> P1, List<int> P2)

second to union two List: 仅次于联合两个列表:

public List<int> Union(List<int> P1, List<int> P2)

the problem is: How should I parse the boolean expression in TextBox where if there is + then merge two lists of words. 问题是:我应该如何解析TextBox中的布尔表达式,如果存在+则合并两个单词列表。 if there is - then union two list to produce finally One List of required documents id ... 如果存在-然后合并两个列表以最终生成所需文档ID的一个列表...
please help me and thank you alot. 请帮助我,非常感谢。

I think that you need to write a kind of grammar. 我认为您需要编写一种语法。 The ANTLR project can help you to generate C# code built onto a gramar, like you can do in LEX and YACC . ANTLR项目可以像在LEX和YACC中一样,帮助您生成基于gramar的C#代码。 The community is important, I hope you can find your grammar yet done. 社区很重要,我希望您能找到尚未完成的语法。

I hope it helps 希望对您有所帮助

JP J.P

Not totally sure I understand the question, but wouldn't this be easier just to do with a database query? 不能完全确定我了解这个问题,但是仅使用数据库查询会不会更容易? eg convert the text they enter to: 例如将他们输入的文本转换为:

(word1 + word2) - !word3 (word1 + word2)-!word3

SELECT id FROM table WHERE (field like '% word1 %' and field like '% word2 %') 
  and not (field like '% word3 %')

There are any number of other ways to do the query, eg if you have fulltext or whatever, depending on the database, but this seems more like something best done by constructing the right query rather than dealing with it after the fact. 还有许多其他方式来执行查询,例如,是否具有全文本或其他内容,取决于数据库,但这似乎更像是通过构造正确的查询而不是事后处理查询来最好地完成。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM