开发者

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 ! means 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

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. if there is - then union two list to produce final开发者_StackOverflow中文版ly One List of required documents 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 . The community is important, I hope you can find your grammar yet done.

I hope it helps

JP


Not totally sure I understand the question, but wouldn't this be easier just to do with a database query? e.g. convert the text they enter to:

(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, e.g. 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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜