C++获取函数指针或使用存储在字符串中的函数名称评估函数

发布于 2024-11-03 05:11:19 字数 197 浏览 2 评论 0原文

我正在开发自然语言解析器。自然语言中的单词被解析为它们所代表的概念。目的是让机器人能够解释以自然语言给出的命令。执行这些命令的一种方法是创建大量的 if 语句,这些语句将变成每个概念的至少一个。我想知道是否有一种方法可以检索指向名称存储为字符串的函数的函数指针。然后,每个概念都可以包含一个字符串,该字符串表示如果该概念存在于解析中则需要执行的函数。这样,概念就已经知道要做什么。

I am working on a parser for natural language. Words from natural language are parsed to the concepts that they represent. The purpose is to allow a robot to interpret commands given in natural language. One way to execute these commands would be by creating an enormous amount of if statements, which would turn into at east one for each concept. I was wondering if there was a way to retrieve a function pointer to a function whose name is stored as string. Each concept could then contain a string that represented the function that would need to executed if the concept is present in the parse. This way the concept would already know what to do.

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

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

发布评论

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

评论(1

看春风乍起 2024-11-10 05:11:19

这并没有那么复杂。我会尝试(当然,理论上)实现这些“可能”的解决方案之一:

  1. 这非常 C++:阅读 从名称实例化类?并为每种“自然”语言创建一个类,确保这些类派生自公共接口,并且每个类都具有重写的方法将处理特定的单词。

  2. 这更C:如果您不害怕使用共享库,请创建一个共享库,其中包含您想要的命令的所有函数,确保函数名称与“单词”匹配,然后加载特定的您要使用的功能(单词)。这比上面的更容易,因为您不需要知道所有单词,并且维护也更少。

干杯

This is not so complicated. I would try to (theoretically, of course) implement one of these "possible" solutions:

  1. This is very C++: Read Instantiate class from name? and create a class for each of the "natural" language, make sure that these classes derive from a common interface, and each of the classes would have the overridden method would handle the specific word.

  2. This is more C: In case you are not afraid to use shared libraries, create a shared library, with all the functions for the commands you want, make sure the functions name match the "words" and then just load the specific function (word) you want to use. This is even easier than the one above, since you don't need to know all the words, and there is less maintenance.

Cheers

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