int tree_noopenQl(TreeHist* treeh) { InputTree* tree = treeh->tree; int pos = treeh->pos; int hpos = treeh->hpos; InputTree *subTree; InputTrees::reverse_iterator subTreeIter = tree->subTrees().rbegin(); int i = tree->subTrees().size()-1; bool sawOpen = false; bool sawClosed = false; for( ; ; subTreeIter++ ) { if(i == pos) break; if(i > hpos) { i-- ; continue; } assert(i >= 0); subTree = *subTreeIter; const Term* trm = Term::get(subTree->term()); if(trm->isClosed() && !sawOpen) sawOpen = true; else if(trm->isOpen() && sawOpen) sawOpen = false; i--; } if(sawOpen) return 0; else return 1; }
int tree_ngram(TreeHist* treeh, int n, int l) { static int stopTermInt = -1; if(stopTermInt < 0) { ECString stopStr("STOP"); const Term* stopTerm = Term::get(stopStr); stopTermInt = stopTerm->toInt(); } int pos = treeh->pos; int hp = treeh->hpos; int m = pos + (n * l); if(m < 0) return stopTermInt; InputTree* tree = treeh->tree; if(m >= tree->subTrees().size()) return stopTermInt; if(m > hp && l > 0) return stopTermInt; InputTree *subTree; InputTreesIter subTreeIter = tree->subTrees().begin(); int i = 0; for( ; subTreeIter != tree->subTrees().end() ; subTreeIter++ ) { if(i == m) { subTree = *subTreeIter; const Term* trm = Term::get(subTree->term()); return trm->toInt(); } i++; } assert("should never get here"); return -1; }
int tree_term_after(TreeHist* treeh) { static int stopint = 0; if(!stopint) { ECString stopnm("STOP"); stopint = Term::get(stopnm)->toInt(); } InputTree* tree = treeh->tree; InputTree* par = tree->parent(); if(!par) return stopint; InputTreesIter iti = par->subTrees().begin(); for( ; iti != par->subTrees().end() ; iti++ ) { InputTree* st = *iti; if(st != tree) continue; iti++; if(iti == par->subTrees().end()) return stopint; st = *iti; const ECString& trmStr = st->term(); const Term* trm = Term::get(trmStr); assert(trm); return trm->toInt(); } error("Should never get here"); return -1; }
int tree_B(TreeHist* treeh, int blInd) { InputTree* tree = treeh->tree; int i; int pos = treeh->pos; int hpos = treeh->hpos; //cerr << "tb1 " << pos << " " << hpos << " " << *tree << endl; int sz = tree->subTrees().size(); int wpos; assert(pos <= sz); //cerr << "tb " << pos << " " << hpos << " " << sz << endl; if(pos < 0) wpos = tree->start()-1; else if(sz == 0) wpos = tree->start()-1; else if(pos == sz) wpos = tree->finish(); else { InputTreesIter iti = tree->subTrees().begin(); i = 0; for( ; iti != tree->subTrees().end() ; iti++) { if(i < pos) { i++; continue; } InputTree* st = *iti; if(pos < hpos) wpos = st->start()-1; else if(pos > hpos) wpos = st->finish(); else if(blInd) wpos = st->start()-1; else wpos = st->finish(); //cerr << "tbf " << *st << " " << wpos << endl; break; } } //cerr << "tb2 " << wpos << endl; assert(wpos <= endPos); if(wpos < 0 || wpos == endPos) return Term::stopTerm->toInt(); else return Term::get(sentence[wpos]->term())->toInt(); }
int tree_noopenQr(TreeHist* treeh) { InputTree* tree = treeh->tree; int pos = treeh->pos; int sz = tree->subTrees().size(); InputTree *subTree; InputTreesIter subTreeIter = tree->subTrees().begin(); int i = 0; bool sawOpen = false; for( ; ; subTreeIter++ ) { if(i == pos) break; subTree = *subTreeIter; assert(i < sz); const Term* trm = Term::get(subTree->term()); if(trm->isOpen() && !sawOpen) sawOpen=true; if(trm->isClosed() && sawOpen ) sawOpen = false; i++; } if(sawOpen) return 0; else return 1; }
InputTree* tree_find(TreeHist* treeh, int n) { int pos = treeh->pos; int hp = treeh->hpos; int m = pos + n; assert(m >= 0); InputTree* tree = treeh->tree; assert(!(m >= tree->subTrees().size())); InputTree *subTree; InputTreesIter subTreeIter = tree->subTrees().begin(); int i = 0; for( ; subTreeIter != tree->subTrees().end() ; subTreeIter++ ) { if(i == m) { subTree = *subTreeIter; return subTree; } i++; } assert("should never get here"); return NULL; }
InputTree* tree_2rel_tree(TreeHist* treeh) { //cerr << "t1r " << *treeh->tree << endl; int pos = treeh->pos; int hpos = treeh->hpos; if(pos == hpos || pos < hpos-1 || pos > hpos+1) return NULL; //cerr << "t2r " << *treeh->tree << endl; InputTree* sib; if(pos < hpos) { sib = tree_find(treeh, +1); int sibhp = headPosFromTree(sib); InputTree* sibch; if(sibhp > 0) { sibch = sib->subTrees().front(); } else if(sib->subTrees().size() < 2) return NULL; else { InputTreesIter iti = sib->subTrees().begin(); iti++; sibch = *iti; } return sibch; } else { sib = tree_find(treeh, -1); int sibhp = headPosFromTree(sib); InputTree* sibch; if(sibhp < sib->subTrees().size()-1) { sibch = sib->subTrees().back(); } else if(sib->subTrees().size() < 2) return NULL; else { InputTrees::reverse_iterator iti = sib->subTrees().rbegin(); iti++; sibch = *iti; } return sibch; } }