本文整理汇总了C++中CStateItem类的典型用法代码示例。如果您正苦于以下问题:C++ CStateItem类的具体用法?C++ CStateItem怎么用?C++ CStateItem使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了CStateItem类的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: item
void CDepParser::updateScoresForStates( const CStateItem *outout , const CStateItem *correct , SCORE_TYPE amount_add, SCORE_TYPE amount_subtract ) {
// do not update those steps where they are correct
static CStateItem item(&m_lCache);
static unsigned action, correct_action;
item.clear();
while ( item != *outout ) {
action = item.FollowMove( outout );
correct_action = item.FollowMove( correct );
if ( action == correct_action )
item.Move( action );
else break;
}
// for the necessary information for the correct and outout parsetree
updateScoreForState( item, correct , amount_add ) ;
updateScoreForState( item, outout , amount_subtract ) ;
m_nTotalErrors++;
}
示例2: getUTF8StringLength
void CConParser::work(const CTwoStringVector &sentence, const CSentenceParsed &correct, CCoNLLOutput *o_conll){
const int length = sentence.size();
static int tmp_i,tmp_j;
static CAction correct_action;
m_lCache.clear();
m_lWordLen.clear();
for (tmp_i=0; tmp_i<length; tmp_i++ ) {
m_lCache.push_back( CTaggedWord<CTag, TAG_SEPARATOR>(sentence[tmp_i].first , sentence[tmp_i].second) );
m_lWordLen.push_back( getUTF8StringLength(sentence[tmp_i].first) );
}
std::vector<CStateItem> p(MAX_SENTENCE_SIZE*(2+UNARY_MOVES)+2);
CStateItem *correctState = &p[0];
//getLabeledBrackets(correct, correctState->gold_lb);
correctState->clear();
correctState->words = (&m_lCache);
tmp_i = 1;
while(true){
correctState->StandardMove(correct, correct_action);
//std::cerr<<correct_action<<std::endl;
correctState->Move(&p[tmp_i],correct_action);
correctState = &p[tmp_i];
tmp_i ++;
if (correctState == 0 || correctState->IsTerminated()) break; // while
}
correctState->GenerateStanford(sentence, o_conll);
p.clear();
}
示例3: GetOrUpdateStackScore
void
CDepParser::extract_features(const CDependencyParse &input0, const CDependencyParse &input1) {
CStateItem item;
unsigned action;
CPackedScoreType<SCORE_TYPE, action::kMax> empty;
// word and pos
m_lCache.clear();
#ifdef LABELED
m_lCacheLabel0.clear();
m_lCacheLabel1.clear();
#endif
for (int i = 0; i < input0.size(); ++ i) {
m_lCache.push_back(CTaggedWord<CTag, TAG_SEPARATOR>(input0[i].word,
input0[i].tag));
#ifdef LABELED
m_lCacheLabel0.push_back(CDependencyLabel(input0[i].label));
m_lCacheLabel1.push_back(CDependencyLabel(input1[i].label));
#endif
}
// make standard item
item.clear(); item.len_ = input0.size();
for (int i = 0; i < input0.size() * 4; ++ i) {
unsigned action = item.StandardMove(input0, input1
#ifdef LABELED
, m_lCacheLabel0, m_lCacheLabel1
#endif
);
GetOrUpdateStackScore(&item, empty, action, 1, 1);
item.Move(item.nextactionindex(), action);
}
}
示例4: clock
void CDepParser::work( const bool bTrain , const CTwoStringVector &sentence , CDependencyParse *retval , const CDependencyParse &correct , int nBest , SCORE_TYPE *scores ) {
#ifdef DEBUG
clock_t total_start_time = clock();
#endif
static int index;
const int length = sentence.size() ;
const CStateItem *pGenerator ;
static CStateItem pCandidate(&m_lCache) ;
// used only for training
static bool bCorrect ; // used in learning for early update
static bool bContradictsRules;
static CStateItem correctState(&m_lCache) ;
static CPackedScoreType<SCORE_TYPE, action::MAX> packed_scores;
ASSERT(length<MAX_SENTENCE_SIZE, "The size of the sentence is larger than the system configuration.");
TRACE("Initialising the decoding process...") ;
// initialise word cache
bContradictsRules = false;
m_lCache.clear();
for ( index=0; index<length; ++index ) {
m_lCache.push_back( CTaggedWord<CTag, TAG_SEPARATOR>(sentence[index].first , sentence[index].second) );
// filter std::cout training examples with rules
if (bTrain && m_weights->rules()) {
// the root
if ( correct[index].head == DEPENDENCY_LINK_NO_HEAD && canBeRoot(m_lCache[index].tag.code())==false) {
TRACE("Rule contradiction: " << m_lCache[index].tag.code() << " can be root.");
bContradictsRules = true;
}
// head left
if ( correct[index].head < index && hasLeftHead(m_lCache[index].tag.code())==false) {
TRACE("Rule contradiction: " << m_lCache[index].tag.code() << " has left head.");
bContradictsRules = true;
}
// head right
if ( correct[index].head > index && hasRightHead(m_lCache[index].tag.code())==false) {
TRACE("Rule contradiction: " << m_lCache[index].tag.code() << " has right head.");
bContradictsRules = true;
}
}
}
// initialise agenda
m_Agenda->clear();
pCandidate.clear(); // restore state using clean
m_Agenda->pushCandidate(&pCandidate); // and push it back
m_Agenda->nextRound(); // as the generator item
if (bTrain) correctState.clear();
// verifying supertags
if (m_supertags) {
ASSERT(m_supertags->getSentenceSize()==length, "Sentence size does not match supertags size");
}
#ifdef LABELED
unsigned long label;
m_lCacheLabel.clear();
if (bTrain) {
for (index=0; index<length; ++index) {
m_lCacheLabel.push_back(CDependencyLabel(correct[index].label));
if (m_weights->rules() && !canAssignLabel(m_lCache, correct[index].head, index, m_lCacheLabel[index])) {
TRACE("Rule contradiction: " << correct[index].label << " on link head " << m_lCache[correct[index].head].tag.code() << " dep " << m_lCache[index].tag.code());
bContradictsRules = true;
}
}
}
#endif
// skip the training example if contradicts
if (bTrain && m_weights->rules() && bContradictsRules) {
std::cout << "Skipping training example because it contradicts rules..." <<std::endl;
return;
}
TRACE("Decoding started");
// loop with the next word to process in the sentence
for (index=0; index<length*2; ++index) {
if (bTrain) bCorrect = false ;
// none can this find with pruning ???
if (m_Agenda->generatorSize() == 0) {
WARNING("parsing failed");
return;
}
pGenerator = m_Agenda->generatorStart();
// iterate generators
for (int j=0; j<m_Agenda->generatorSize(); ++j) {
// for the state items that already contain all words
m_Beam->clear();
packed_scores.reset();
getOrUpdateStackScore( pGenerator, packed_scores, action::NO_ACTION );
if ( pGenerator->size() == length ) {
assert( pGenerator->stacksize() != 0 );
if ( pGenerator->stacksize()>1 ) {
//.........这里部分代码省略.........
示例5: clock
int
CDepParser::work(const bool is_train,
const CTwoStringVector & sentence,
CDependencyParse * retval0, CDependencyParse * retval1,
const CDependencyParse & oracle_tree0, const CDependencyParse & oracle_tree1,
int nbest,
SCORE_TYPE *scores) {
#ifdef DEBUG
clock_t total_start_time = clock();
#endif
const int length = sentence.size();
const int max_round = length * 4 + 1;
const int max_lattice_size = (kAgendaSize + 1) * max_round;
ASSERT(length < MAX_SENTENCE_SIZE,
"The size of sentence is too long.");
CStateItem * lattice = GetLattice(max_lattice_size);
CStateItem * lattice_wrapper[max_lattice_size];
CStateItem ** lattice_index[max_round];
CStateItem * correct_state = lattice;
for (int i = 0; i < max_lattice_size; ++ i) {
lattice_wrapper[i] = lattice + i;
lattice[i].len_ = length;
}
lattice[0].clear();
correct_state = lattice;
lattice_index[0] = lattice_wrapper;
lattice_index[1] = lattice_index[0] + 1;
static CPackedScoreType<SCORE_TYPE, action::kMax> packed_scores;
TRACE("Initialising the decoding process ...");
m_lCache.clear();
for (int i = 0; i < length; ++ i) {
m_lCache.push_back(CTaggedWord<CTag, TAG_SEPARATOR>(sentence[i].first,
sentence[i].second));
#ifdef LABELED
if (is_train) {
if (i == 0) { m_lCacheLabel0.clear(); m_lCacheLabel1.clear(); }
m_lCacheLabel0.push_back(CDependencyLabel(oracle_tree0[i].label));
m_lCacheLabel1.push_back(CDependencyLabel(oracle_tree1[i].label));
}
#endif
}
int num_results = 0;
int round = 0;
bool is_correct; // used for training to specify correct state in lattice
// loop with the next word to process in the sentence,
// `round` represent the generators, and the condidates should be inserted
// into the `round + 1`
for (round = 1; round < max_round; ++ round) {
if (lattice_index[round - 1] == lattice_index[round]) {
// there is nothing in generators, the proning has cut all legel
// generator. actually, in this kind of case, we should raise a
// exception. however to achieve a parsing tree, an alternative
// solution is go back to the previous round
WARNING("Parsing Failed!");
-- round;
break;
}
int current_beam_size = 0;
// loop over the generator states
// std::cout << "round : " << round << std::endl;
for (CStateItem ** q = lattice_index[round - 1];
q != lattice_index[round];
++ q) {
const CStateItem * generator = (*q);
m_Beam->clear(); packed_scores.reset();
GetOrUpdateStackScore(generator, packed_scores, action::kNoAction);
Transit(generator, packed_scores);
for (unsigned i = 0; i < m_Beam->size(); ++ i) {
CStateItem candidate; candidate = (*generator);
// generate candidate state according to the states in beam
int curIndex = candidate.nextactionindex();
candidate.Move(curIndex, m_Beam->item(i)->action);
candidate.score = m_Beam->item(i)->score;
candidate.previous_ = generator;
current_beam_size += InsertIntoBeam(lattice_index[round],
&candidate,
current_beam_size,
kAgendaSize);
}
}
//.........这里部分代码省略.........
示例6: clock
/*---------------------------------------------------------------
*
* work - the working process shared by training and parsing
*
* Returns: makes a new instance of CDependencyParse
*
*--------------------------------------------------------------*/
int
CDepParser::work(const bool is_train,
const CTwoStringVector & sentence,
CDependencyParse * retval,
const CDependencyParse & oracle_tree,
int nbest,
SCORE_TYPE *scores) {
#ifdef DEBUG
clock_t total_start_time = clock();
#endif
const int length = sentence.size();
const int max_round = length * 2 + 1;
const int max_lattice_size = (kAgendaSize + 1) * max_round;
ASSERT(length < MAX_SENTENCE_SIZE,
"The size of sentence is too long.");
CStateItem * lattice = GetLattice(max_lattice_size);
CStateItem * lattice_index[max_round];
CStateItem * correct_state = lattice;
for (int i = 0; i < max_lattice_size; ++ i) {
lattice[i].len_ = length;
}
lattice[0].clear();
correct_state = lattice;
lattice_index[0] = lattice;
lattice_index[1] = lattice_index[0] + 1;
static CPackedScore packed_scores;
TRACE("Initialising the decoding process ...");
m_lCache.clear();
for (int i = 0; i < length; ++ i) {
m_lCache.push_back(CTaggedWord<CTag, TAG_SEPARATOR>(sentence[i].first,
sentence[i].second));
#ifdef LABELED
if (is_train) {
if (i == 0) {
m_lCacheLabel.clear();
}
m_lCacheLabel.push_back(CDependencyLabel(oracle_tree[i].label));
}
#endif
}
int num_results = 0;
int round = 0;
bool is_correct; // used for training to specify correct state in lattice
// loop with the next word to process in the sentence, 'round' represent the
// generators, and the condidates should be inserted into the 'round + 1'
for (round = 1; round < max_round; ++ round) {
if (lattice_index[round - 1] == lattice_index[round]) {
// There is nothing in generators, the proning has cut all legel
// generator. Actually, in this kind of case, we should raise a
// exception. However to achieve a parsing tree, an alternative
// solution is go back to the previous round
WARNING("Parsing Failed!");
-- round;
break;
}
current_beam_size_ = 0;
// loop over the generator states
// std::cout << "round : " << round << std::endl;
for (CStateItem * q = lattice_index[round - 1]; q != lattice_index[round];
++ q) {
const CStateItem * generator = q;
packed_scores.reset();
GetOrUpdateStackScore(generator, packed_scores, action::kNoAction);
Transit(generator, packed_scores);
}
for (unsigned i = 0; i < current_beam_size_; ++ i) {
const CScoredTransition& transition = m_kBestTransitions[i];
CStateItem* target = lattice_index[round]+ i;
(*target) = (*transition.source);
// generate candidate state according to the states in beam
target->Move(transition.action);
target->score = transition.score;
target->previous_ = transition.source;
}
lattice_index[round + 1] = lattice_index[round] + current_beam_size_;
if (is_train) {
CStateItem next_correct_state(*correct_state);
next_correct_state.StandardMoveStep(oracle_tree
#ifdef LABELED
, m_lCacheLabel
//.........这里部分代码省略.........
示例7: clock
void CSegmentor::segment(const CStringVector* sentence_input, CStringVector *vReturn, double *out_scores, int nBest) {
clock_t total_start_time = clock();;
const CStateItem *pGenerator, *pCandidate;
CStateItem tempState;
unsigned index; // the index of the current char
unsigned j, k; // temporary index
int subtract_score; // the score to be subtracted (previous item)
static CStateItem best_bigram;
int start_index;
int word_length;
int generator_index;
static CStringVector sentence;
static CRule rules(m_Feature->m_bRule);
rules.segment(sentence_input, &sentence);
const unsigned length = sentence.size();
assert(length<MAX_SENTENCE_SIZE);
assert(vReturn!=NULL);
//clock_t start_time = clock();
TRACE("Initialising the segmentation process...");
vReturn->clear();
clearWordCache();
m_Chart.clear();
tempState.clear();
m_Chart[0]->insertItem(&tempState);
TRACE("Segmenting started");
for (index=0; index<length; index++) {
// m_Chart index 1 correspond to the first char
m_Chart[index+1];
// control for the ending character of the candidate
if ( index < length-1 && rules.canSeparate(index+1)==false )
continue ;
start_index = index-1 ; // the end index of last word
word_length = 1 ; // current word length
// enumerating the start index
// ===========================
// the start index of the word is actually start_index + 1
while( start_index >= -1 && word_length <= MAX_WORD_SIZE ) {
// control for the starting character of the candidate
// ---------------------------------------------------
while ( start_index >= 0 && rules.canSeparate(start_index+1)==false )
start_index-- ;
// start the search process
// ------------------------
for ( generator_index = 0 ; generator_index < m_Chart[ start_index+1 ]->size() ; ++ generator_index ) {
pGenerator = m_Chart[ start_index+1 ]->item( generator_index ) ;
tempState.copy( pGenerator ) ;
tempState.append( index ) ;
tempState.m_nScore += m_Feature->getLocalScore( &sentence, &tempState, tempState.m_nLength-1 ) ;
if (nBest==1) {
if ( generator_index == 0 || tempState.m_nScore > best_bigram.m_nScore ) {
best_bigram.copy(&tempState); //@@@
}
}
else {
m_Chart[ index+1 ]->insertItem( &tempState );
}
}
if (nBest==1) {
m_Chart[ index+1 ]->insertItem( &best_bigram ); //@@@
} //@@@
// control the first character of the candidate
if ( rules.canAppend(start_index+1)==false )
break ;
// update start index and word len
--start_index ;
++word_length ;
}//start_index
}
// now generate outout sentence
// n-best list will be stored in array
// from the addr vReturn
TRACE("Outputing sentence");
for (k=0; k<nBest; ++k) {
// clear
vReturn[k].clear();
if (out_scores!=NULL)
out_scores[k] = 0;
// assign retval
if (k<m_Chart[length]->size()) {
pGenerator = m_Chart[length]->bestItem(k);
for (j=0; j<pGenerator->m_nLength; j++) {
std::string temp = "";
for (unsigned l = pGenerator->getWordStart(j); l <= pGenerator->getWordEnd(j); ++l) {
assert(sentence.at(l)!=" "); // [SPACE]
temp += sentence.at(l);
}
//.........这里部分代码省略.........