コード例 #1
0
ファイル: parser.cpp プロジェクト: NLP/Grammatica
/**
 * @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;
}
コード例 #2
0
void doneATask(Task* taskGrid, int M, int N, Task t)
{
	int k, j, p, q;
	enum Type tType, tTypeNext;

	p = t.l;
	q = t.m;
	k = tgrid(p,q).k;
	tType = getNextType(p, q, k);
	tgrid(p,q).taskStatus = DONE;
	
	switch(tType)
	{
		case QRS:
		{
			for(j = k + 1; j < N; j ++)//check along row
			{
				if(candoSAPP(taskGrid, M, N, p, j, k))
					makeTask(taskGrid, M, p, j, SAPP, READY, k);
			}

			if(candoQRD(taskGrid, M, N, p+1, q, k))//check one below
				makeTask(taskGrid, M, p+1, q, QRD, READY, k);
			break;
		}
		case SAPP:
		{
			if(candoDAPP(taskGrid, M, N, p+1, q, k))//check one below
				makeTask(taskGrid, M, p+1, q, DAPP, READY, k);
			break;
		}
		case QRD:
		{
			for(j = k + 1; j < N; j ++)
			{
				if(candoDAPP(taskGrid, M, N, p, j, k))//check along row
					makeTask(taskGrid, M, p, j, DAPP, READY, k);
			}
			
			if(candoQRD(taskGrid, M, N, p+1, q, k))//check one below
				makeTask(taskGrid, M, p+1, q, QRD, READY, k);
			break;
		}
		case DAPP:
		{
			tTypeNext = getNextType(p, q, k + 1);

			switch(tTypeNext)//check whether can activate any for next step
			{
				case QRS:
				{
					if(candoQRS(taskGrid, M, N, p, q, k + 1))
						makeTask(taskGrid, M, p, q, QRS, READY, k + 1);
					break;
				}
				case SAPP:
				{
					if(candoSAPP(taskGrid, M, N, p, q, k + 1))
						makeTask(taskGrid, M, p, q, SAPP, READY, k + 1);
					break;
				}
				case QRD:
				{
					if(candoQRD(taskGrid, M, N, p, q, k + 1))
						makeTask(taskGrid, M, p, q, QRD, READY, k + 1);
					break;
				}
				case DAPP:
				{
					if(candoDAPP(taskGrid, M, N, p, q, k + 1))
						makeTask(taskGrid, M, p, q, DAPP, READY, k + 1);
					break;
				}
			}
			
			if(candoDAPP(taskGrid, M, N, p + 1, q, k))//check one below in current step
				makeTask(taskGrid, M, p + 1, q, DAPP, READY, k);
			break;
		}
	}
}