/** * @brief Parser::findFirstIncomplete Finds the first incomplete node * (The first phrase who has yet to go through all of its definitions)) * @param S The tree * @return True if there is, False otherwise */ bool Parser::findFirstIncomplete(SyntaxTree& S){ if(S.atLeaf()) return false; for(std::size_t i = 0; i < S.childNum(); ++i){ S.shiftDown(i); if(findFirstIncomplete(S)) return true; S.shiftUp(); } return !getNextDef(S.getPhrase(),S.getDef()).empty(); }
/** * @brief Parser::recDescent recursively parses the sentence * using the recursive descent top-down parsing method * @param S the tree * @param c the cutoff (How many words into the sentence does it start) * @return True if it is successful, False otherwise */ bool Parser::recDescent(SyntaxTree& S, std::size_t& c){ // cin.get(); // cout << "S so far: " << S << endl << endl; // cout << "c = " << c << endl; // cout << "Scur: " << *S.getCurrent() << endl; // cout << "The phrase is " << phraseLookUp[S.getPhrase()] << endl; GPlist def = getNextDef(S.getPhrase(),S.getDef()); if(def.empty() && S.getDef().empty()){ Word W; if(S.atLastLeaf()){ // cout << "S is at the last leaf" << endl; W = getNextWord(c + 1); // cout << "The word at c+1: " << W << endl; if(*W.getTypes().begin() != IGNORETHIS){ // --c; return false; } W = getNextWord(c); } else W = getNextWord(c); // cout << "Got the next word: " << W << endl; for(WordType cT = getNextType(W,IGNORETHIS); cT != IGNORETHIS; cT = getNextType(W,cT)){ GrammarPhrase g = WTtoGP[cT]; // cout << "The WT converted to gp: " << phraseLookUp[g] << endl; // cout << "Match this: " << phraseLookUp[S.getPhrase()] << endl; if(S.getPhrase() == g){ // cout << "THERE IS A MATCH" << endl; ++c; return true; } } return false; } if(!S.getDef().empty()) S.removeDef(); while(!def.empty()){ if(S.getDef().empty()) S.addDef(def); // cout << "S with added def: " << S << endl; bool check = false; for(std::size_t i = 0; i < S.childNum(); ++i){ // cout << "i: " << i << endl; S.shiftDown(i); // cout << "The CHild: " << *S.getCurrent() << endl; check = recDescent(S,c); if(!check && i != 0){ S.shiftUp(); // cout << "Not first child" << endl; // cout << "Now C: " << c << endl; // cout << "i here: " << i << endl; if(S.getChildAt(i-1) && !S.getChildAt(i-1)->isLeaf()){ c -= rt::leaves(S.getChildAt(i-1)); i -= 2; } else{ while(i > 0 && S.getChildAt(i-1) && S.getChildAt(i-1)->isLeaf()){ --c; --i; } if(i == 0){ S.removeDef(); break; } } // cout << "C: " << c << endl; // cout << "I: " << i << endl; S.shiftDown(i); //Previously 0 } else if(!check && i == 0){ // cout << "First child" << endl; S.shiftUp(); S.removeDef(); break; } if(!S.getParent(S.getCurrent())){ // cout <<"For some reason it is already all the way at root" << endl; if(check && S.leafNum() == _words.size()) return true; } S.shiftUp(); } if(check){ // cout << "recur returned true for all children" << endl; return true; } def = getNextDef(S.getPhrase(),def); } // cout << "No more defs for this phrase and no successful recur, returning false" << endl; return false; }