C++获取函数指针或使用存储在字符串中的函数名称评估函数
我正在开发自然语言解析器。自然语言中的单词被解析为它们所代表的概念。目的是让机器人能够解释以自然语言给出的命令。执行这些命令的一种方法是创建大量的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这并没有那么复杂。我会尝试(当然,理论上)实现这些“可能”的解决方案之一:
这非常 C++:阅读 从名称实例化类?并为每种“自然”语言创建一个类,确保这些类派生自公共接口,并且每个类都具有重写的方法将处理特定的单词。
这更C:如果您不害怕使用共享库,请创建一个共享库,其中包含您想要的命令的所有函数,确保函数名称与“单词”匹配,然后加载特定的您要使用的功能(单词)。这比上面的更容易,因为您不需要知道所有单词,并且维护也更少。
干杯
This is not so complicated. I would try to (theoretically, of course) implement one of these "possible" solutions:
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.
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