跟进英语语法分析器 [英] Follow up to English Grammar Parser
问题描述
我最近问一个关于写一个非常基本的英语语法解析器的问题。我认为显示一些代码(虽然很丑陋)来指定我在实现语法时需要帮助的地方可能会有所帮助。这里是代码。注释表示问题。
I recently asked a question regarding writing a very basic English grammar parser. I thought it might be helpful to show some code (though very ugly) to specify where I need assistance in implementing the grammar. Here is the code. Comments indicate questions.
#include "std_lib_facilities.h"
string sentence();
string noun();
string verb();
string article()
{
string words = sentence(); // not sure how to check for words
if (words == "the")
words = sentence();
else return words;
}
string verb()
{
string words = sentence(); // not sure how to check for words
if (words == "rules" || words == "fly" || words == "swim")
words = sentence();
else return words;
}
string noun()
{
string words = sentence(); // not sure how to check for words
if (words == "birds" || words == "fish" || words == "C++")
words = sentence();
else return words;
}
string conjunction()
{
string words = sentence(); // not sure how to check for words
if (words == "and" || words == "but" || words == "or")
words = sentence();
else return words;
}
string sentence()
{
if (noun()){ // this fails to compile, not sure how to check this
// error message says could not convert noun to bool
if (verb())
cout << "OK.\n";
else cout << "Not OK.\n";}
else if (article()){
if (sentence()) // will this create a loop?
cout << "Ok.\n";
else cout << "Not Ok.\n";}
else if (conjunction()){
if (sentence()) // actually needs to be sentence conjunction sentence
cout << "Ok.\n";
else cout << "Not Ok.\n";
else cout << "Not OK.\n";
}
int main()
{
string words;
cout << "Enter sentence.\n";
while(cin >> words){
sentence();
}
keep_window_open(); // this function is part of the facilities library
}
代码非常不完整,但是基本上是一个需要发生的框架。主要的问题是,我需要检查一个函数是否为true,我需要知道如何比较用户输入到被调用的函数(除了strcasecmp()之外的任何东西它还没有在书中使用,所以必须我也担心可能有多个输出OK或不OK,但我会担心以后。
The code is very incomplete, but is basically a framework for what needs to happen. The main problems are that I need to check if a function is true, and I need to know how to compare the user input to the called function (anything besides strcasecmp()? it hasn't been used in the book yet so there must be another way to it.) I'm also worried about there possibly being multiple outputs of "OK" or "Not OK", but I'll worry about that later.
推荐答案
解析自然语言是一项艰巨的任务。
Parsing natural languages is a really hard task. Especially to do it completely from scratch.
您可以考虑使用现有的库,例如链接语法分析器(C,自定义GPL兼容的免费软件许可证,看起来很像BSD给我,已绑定到许多语言)或 RelEx (Java,Apache许可证)。
You may consider using existing libraries, like Link Grammar Parser (C, custom GPL-compatible free software license which seems a lot like BSD to me, has bindings to many languages) or RelEx (Java, Apache License).
这篇关于跟进英语语法分析器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!