解析有关搜索条件的用户输入

发布于 2024-08-12 09:59:10 字数 304 浏览 11 评论 0原文

我正在寻找一种方法来解析一些用户输入。输入应显示必须执行哪些搜索以及如何组合它们。

  • 1 AND 2
  • (3 AND 2) OR 1
  • (3 AND 2) OR (1 AND 4)
  • ( (3 OR 4) AND 1) OR 2
  • 等。

第一个示例应将搜索 1 和 2 的结果合并为 AND-时尚。第二个示例应以 AND 方式组合搜索 3 和 2 的结果,并以 OR 方式将此组合的结果与搜索 1 的结果组合。等等。

关于如何做到这一点有什么想法吗?

I'm looking for a way to parse some user-input. The input should show which searches have to be performed and how they have to be combined.

  • 1 AND 2
  • (3 AND 2) OR 1
  • (3 AND 2) OR (1 AND 4)
  • ( (3 OR 4) AND 1) OR 2
  • etc.

The first example should combine the results of search 1 and 2 in an AND-fashion. The second example should combine the results of search 3 and 2 in an AND-fashion, and combine the results of this combination to the results of search 1 in an OR-fashion. Etc.

Any ideas on how to do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

罪#恶を代价 2024-08-19 09:59:10

将您的“结果”视为一个对象,它在以下界面中提供 和 或类似的方法:

public interface AndOrCapable<T> {
  public T and(T anOtherResult);
  public T or(T anOtherResult);
}

然后您可以将用户输入转换为类似的内容:

Result total = r2.or(r1.and(r3.or(r4))); // your fourth example

这只是为了澄清概念 - 在您的情况下,您需要一个动态评估器,因为您使用用户输入。

因此,您仍然需要一个验证器/解析器将用户输入转换为(语法)树,这将是您用来计算总数的模型。

希望它有一点帮助!

Think of your 'result' as an Object that offers methods for and and or like in the following interface:

public interface AndOrCapable<T> {
  public T and(T anOtherResult);
  public T or(T anOtherResult);
}

Then you can translate your user input into something like:

Result total = r2.or(r1.and(r3.or(r4))); // your fourth example

This is just to clarify the concept - in your case you need a dynamic evaluator because you use user input.

So you still need a validator/parser to transform the user input into an (syntax) tree, which will be the model you'd use to calculate the total.

Hope it helped a bit!

春风十里 2024-08-19 09:59:10

JavaCC 是一个很好的工具,可以用来为此生成解析器。或者,如果您可以稍微更改语法,您也许可以使用方案解释器使用 java 编写脚本工具,例如

( (3 OR 4) AND 1) OR 2

变为

(OR (AND (OR 3 4) 1) 2)

然后您只需要实现 AND/OR

JavaCC is a good tool to use to generate a parser for this. Alternately, if you can change the syntax a little you may be able to the scripting facilities with java using a scheme interpreter, e.g.

( (3 OR 4) AND 1) OR 2

becomes

(OR (AND (OR 3 4) 1) 2)

Then you just need to implement the AND/OR

随风而去 2024-08-19 09:59:10

干净的解决方案是编写一个中缀解析器;网上有很多代码示例。然而,在您的示例中,更简单的算法可能就足够了,因为您不需要运算符优先级等。

作为编码备注:StreamTokenizer 类可能会帮助您解析输入字符串。

The clean solution would be to write an infix parser; there are quite a few code examples online. In your example, a simpler algorithm might suffice, however, since you do not need operator precedence etc.

As a coding remark: The StreamTokenizer class might help you in parsing the input string.

缱绻入梦 2024-08-19 09:59:10

在实现端(一旦有了解析器,组织并执行搜索):

  1. 创建一个 Condition 树怎么样,其中 Condition 对象可能是简单的条件,或者复合条件用布尔值连接 2 个简单条件(即 ANDCondition 父节点以及子节点 RangeConditionEqualsCondition)。

    然后,您根据每个项目评估树的顶部。此解决方案的复杂度为 O(mn),其中 m 是条件数,n 是要搜索的项目数,但您可以通过删除冗余条件来优化此解决方案。如果第一个条件消除了大多数项目,速度会快得多。

  2. 版本 2:为每个项目分配一个唯一的键(例如数组索引),并对每个条件执行搜索,为每个条件构建一个 HashSet。然后,从最小的所需键集开始,删除或添加每个条件的键,直到获得最终结果。这可能比上面的更快,具体取决于。

注意:这些方法模仿 SQL 数据库的操作方式——如果您的系统足够大或足够复杂,您可能应该研究使用数据库而不是编写自己的代码来完成同样的事情。

On the implementation end (once you have a parser, organizing and performing searches):

  1. What about creating a Condition tree, where Condition objects may be simple conditions, or compound conditions joining 2 simple conditions with a boolean (IE ANDCondition parent node with children RangeCondition and EqualsCondition).

    Then you evaluate the top of the tree against each item. This solution is O(mn), where m is number of conditions, and n is number of of items to search, but you can optimize this by removing redundant conditions. It is much faster if the first condition eliminates most items.

  2. Version 2: assign a unique key to each item (say, an array index), and perform the searches for each condition, building a HashSet<Key> for each condition. Then, starting with the smallest set of required keys, remove or add keys for each condition until you have final results. This may be faster than the above, depending.

Note: these approaches mimic how an SQL Database will operate -- if your system is sufficiently large or complex, you should probably investigate using a database instead of writing your own code to do the same thing.

梦初启 2024-08-19 09:59:10

只是关于如何为通用关键字搜索创建解析器的一些灵感...

即使您标记了问题 java,这里还是一个 python 中的搜索解析器的示例。它使用 pyparsing(一个解析器生成器),它接受语法并创建代码,可以运行该代码来解析用户输入。

https://github.com/pyparsing/pyparsing/blob/master/examples/searchparser。 py

293 行代码,包括一个测试套件。也许它可以帮助您作为起点......

Just some inspiration on how to create parsers for generic keyword search ...

Even though you tagged the question java, here's an example of a searchparser in python. It uses pyparsing, a parser generator, which takes a grammar and creates code, which can be run to parser user input.

https://github.com/pyparsing/pyparsing/blob/master/examples/searchparser.py

293 lines of code, including a test suite. Maybe it helps you as a starting point ...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文