2016-03-25 16 views
-2

Bu kod örneğinde, bir vektöre bir ref düzeltmesi kullanıyorum ve dört işlevdeki koşulları kontrol ediyorum. Koşulların her birinin büyük bir listesi varsa (örneğin, bir veritabanındaki tüm fiiller (örneğin, bir ingilizce sözlük), fonksiyona geçmeden önce bir Token sınıfında bunu kontrol etmenin daha iyi olacağı (böylece fonksiyonun sadece değer kazanacağı) fiil) VEYA daha iyi işlev içinde kontrol etmek (böylece fonksiyon kendisi) veritabanını kontrol etmek zorundadır?Önemsiz dizge ayrıştırma algoritması

#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 c Ompiling ve iyi çalışıyor, _... _Bu şık bir çözüm mü? Tam olarak ne arıyorsun? Sadece açık bir forumda fikirlere dayalı fikir birliği imkansızdır. – ryyker

+0

Geliştirmek istediğiniz belirli bir şey var mı? – DimChtz

+0

Çok doğru, sanırım cümle üzerinde geriye doğru çalışmaya değer olup olmadığını merak ediyordum. İleriye doğru yapılabilir, ancak eğer birden fazla cümleyi ayrıştırmak için kullanılmışsa, bu şekilde onları tek tek ısırtabilirdi. Ne aradığımı tam olarak bilmiyorum, sadece yararlı bir yorum ya da iki. Şerefe – alexi2

cevap

1

bağlam ücretsiz dil tanıyıcıları ötesinde teori genellikle yineleme ile pushdown automaton (devlet makinesinin dayanmaktadır). Öyle el ile başa çıkmak için oldukça can sıkıcı ama otomatize etmek kolaydır, bu yüzden yacc, ANTLR ya da BOOST Ruhu gibi ayrıştırıcı jeneratörler çok az var.Onlar sadece dil dilbilgisi alıp karşılık gelen ayrıştırıcı kodu üretiyorlar.

İlgili konular