2016-03-25 22 views
-2

このコード例では、ベクトルにconst refを使用し、4つの関数で条件をチェックします。それぞれの条件の大きなリスト(たとえば、データベース内のすべての動詞(例えば、英語辞書))があった場合、関数に渡す前にクラストークンでそのトークンをチェックするほうがよいでしょう関数内でチェックする動詞)ORより良い(その関数自体は、データベースをチェックする必要があります)?トリビアル文字列解析アルゴリズム

#include <iostream> 
#include <string> 
#include <vector> 
using namespace std; 

bool full_stop_check(vector<string> &sentence); 
bool verb_check(vector<string> &sentence, int index); 
bool noun_check(vector<string> &sentence, int index); 
bool conj_arti_check(vector<string> &sentence, int index); 

int main() 
{ 
    vector<string> sentence; 
    string temp_word; 
    while(cin >> temp_word) 
    { 
     sentence.push_back(temp_word); 
    } 
    // Output test (commented out) 
    // for (string x : sentence) 
    // cout << x << '\n'; 

    // Check for sentence 
    if (full_stop_check(sentence)) 
     cout << "It is a sentence." << '\n'; 
    else 
     cout << "It is not a sentence." << '\n'; 
    return 0; 
} 
bool full_stop_check(vector<string> &sentence) 
{ 
    int index = sentence.size()-1; 
    // Full Stop Check 
    if (sentence[index] != ".") 
     return false; 
    --index; 
    if (index < 0) 
     return false; 
    return verb_check(sentence, index);   // full stop (not first character) 
} 
bool verb_check(vector<string> &sentence, int index) 
{ 
    // Verb Check 
    if (sentence[index] != "verb") 
     return false; 
    --index; 
    if (index < 0) 
     return false; 
    return noun_check(sentence, index);   // verb (not first word) 
} 
bool noun_check(vector<string> &sentence, int index) 
{ 
    // Noun Check 
    if (sentence[index] != "noun") 
     return false; 
    --index; 
    if (index < 0)        // first word is a noun 
     return true; 
    return conj_arti_check(sentence, index); // noun (not first word) 
} 
bool conj_arti_check(vector<string> &sentence, int index) 
{ 
    // Conjugation & Article Check 
    if (sentence[index] != "conjugation" && sentence[index] != "article") 
     return false; 

    // If it is either an article or conjugation 
    if (index == 0 && sentence[index] == "article") // first word is an article 
     return true; 
    else if (index == 0)       // first word not article (or noun) 
     return false; 
    else if (sentence[index] == "conjugation") { // conjugation 
     --index;   
     return verb_check(sentence, index); 
    } 
    else {           // article (not first word) 
     --index; 
     return conj_arti_check(sentence, index); // recursion 
    } 
} 
+0

_It is c ompiling and working okay、_... _それは洗練されたソリューションですか?あなたは何を探していますか?オープンフォーラムでしか意見に基づく合意は不可能である。 – ryyker

+0

改善したいことは何ですか? – DimChtz

+0

非常に真実ですが、私はそれが文章を逆にする価値があるのだろうかと思っていました。それは前進することができますが、複数の文を解析するために使用された場合、これを1つずつこのように噛むことができます。私は何を探しているのか正確にはわかりませんが、役に立つコメントや2つしかありません。乾杯 – alexi2

答えて

1

文脈自由言語認識器を越えた理論は通常、再帰とpushdown automaton(ステートマシンに基づいています)。それはあります手で扱うのは面倒ですが、自動化が簡単なので、yacc、ANTLR、BOOST Spiritのようなパーサージェネレータがかなり存在します。それらは単に文法を取り、対応するパーサコードを生成します。