void CopyWordCounts( WordCount& targetWordCount, WordCount& sourceWordCount, int weight ) { for( wciter iwc=sourceWordCount.begin(); iwc!=sourceWordCount.end(); iwc++ ) { if( targetWordCount.find( iwc->first ) == targetWordCount.end() ) targetWordCount[ iwc->first ] = 0; targetWordCount[ iwc->first ] += weight * iwc->second; } }
int main( int argc, char* argv[] ) { bool isEnd; string triplet, pair, token, word; string peripenultimate = WORD_KEY; string penultimate = WORD_KEY; string previous = BEGIN_KEY; WordCount mainWordCount; while( true ) { cin >> token; if( token == END_KEY ) break; word = Convert( token, isEnd ); cout << "READ WORD FROM INPUT: " << word << endl; if( mainWordCount.find( word ) == mainWordCount.end() ) { mainWordCount[ word ] = 0; } ++mainWordCount[ word ]; AddCount( PreText, word, previous ); AddCount( PostText, previous, word ); if( penultimate != WORD_KEY ) { pair = previous + " " + word; AddCount( PrePairText, pair, penultimate ); pair = penultimate + " " + previous; AddCount( PostPairText, pair, word ); if( penultimate != WORD_KEY ) { triplet = penultimate + " " + previous + " " + word; AddCount( PreTripletText, triplet, peripenultimate ); triplet = peripenultimate + " " + penultimate + " " + previous; AddCount( PostTripletText, triplet, word ); } } if( isEnd ) { AddCount( PostText, word, END_KEY ); if( previous != WORD_KEY ) { pair = previous + " " + word; AddCount( PostPairText, pair, END_KEY ); if( penultimate != WORD_KEY ) { triplet = penultimate + " " + previous + " " + word; AddCount( PostTripletText, triplet, END_KEY ); } } previous = BEGIN_KEY; peripenultimate = WORD_KEY; penultimate = WORD_KEY; } else { peripenultimate = penultimate; penultimate = previous; previous = word; } } // cout << endl; cout << "PreText" << endl; DumpPositionTable( PreText ); cout << "PostText" << endl; DumpPositionTable( PostText ); cout << "PrePairText" << endl; DumpPositionTable( PrePairText ); cout << "PostPairText" << endl; DumpPositionTable( PostPairText ); cout << "PreTripletText" << endl; DumpPositionTable( PreTripletText ); cout << "PostTripletText" << endl; DumpPositionTable( PostTripletText ); while(1) { cin >> word; if( word == END_KEY ) break; word = Convert( word, isEnd ); // cout << endl << "create utterance for word: " << word << endl; penultimate = WORD_KEY; previous = WORD_KEY; BackTrack( penultimate, previous, word ); int accumulation = 0; while( word != END_KEY ) { cout << word; pair = previous + " " + word; triplet = penultimate + " " + previous + " " + word; // cout << endl << "pair='" << pair << "'" << endl; // cout << "triplet='" << triplet << "'" << endl; peripenultimate = penultimate; penultimate = previous; previous = word; WordCount wordCount; if( PostTripletText.find(triplet) != PostTripletText.end() ) { // cout << " -> call GetRandWord( PostTripletText[" << triplet << "] )" << endl; // word = GetRandWord( *PostTripletText[triplet], accumulation++ ); CopyWordCounts( wordCount, *PostTripletText[triplet], 9 ); } else if( PostPairText.find(pair) != PostPairText.end() ) { // cout << " -> call GetRandWord( PostPairText[" << pair << "] )" << endl; // word = GetRandWord( *PostPairText[pair], accumulation++ ); CopyWordCounts( wordCount, *PostPairText[pair], 3 ); } else { // cout << " -> call GetRandWord( PostText[" << word << "] )" << endl; // word = GetRandWord( *PostText[word], accumulation++ ); CopyWordCounts( wordCount, *PostText[word], 1 ); } word = GetRandWord( wordCount, accumulation++ ); if( word == END_KEY ) cout << "." << endl << endl; else cout << " "; } } }