| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| #include "alignment.h" |
| #include "token.h" |
| #include "segment.h" |
| #include "alignedspeechiterator.h" |
|
|
| Logger* Alignment::logger = Logger::getLogger(); |
|
|
| Alignment::Alignment() |
| { |
| m_references = map< Speech*, AlignedSpeech* >(); |
| } |
|
|
| Alignment::~Alignment() |
| { |
| map< Speech* , AlignedSpeech* >::iterator i, ei; |
| |
| i = m_references.begin(); |
| ei = m_references.end(); |
| |
| while(i !=ei) |
| { |
| AlignedSpeech* ptr_elt = i->second; |
| |
| if(ptr_elt) |
| delete ptr_elt; |
| |
| ++i; |
| } |
| |
| m_references.clear(); |
| systems.clear(); |
| } |
|
|
| string Alignment::ToString() |
| { |
| string result = "Alignment:\n"; |
| AlignedSpeech* reference; |
| AlignedSpeechIterator* references = AlignedSpeeches(); |
| |
| while(references->Current(&reference)) |
| { |
| result += reference->ToString() + "\n"; |
| } |
| |
| return result; |
| } |
|
|
| AlignedSpeech* Alignment::GetOrCreateAlignedSpeechFor(Speech* referenceSpeech, const bool& doCreate) |
| { |
| AlignedSpeech* result = m_references[referenceSpeech]; |
| |
| if(result == NULL && doCreate) |
| { |
| result = new AlignedSpeech(referenceSpeech); |
| m_references[referenceSpeech] = result; |
| } |
| |
| return result; |
| } |
|
|
| void Alignment::AddGraphAlignedSegment(GraphAlignedSegment* gas, const string& hyp_key, SegmentsGroup* segmentsGroup) |
| { |
| if(gas == NULL) |
| return; |
| |
| Token* ref = NULL; |
| Token* hyp = NULL; |
| Token* lastNonNullRef = NULL; |
| Token* nextNonNullRef = NULL; |
| Speech* refSpeech = NULL; |
| Segment* refSegment = NULL; |
| AlignedSpeech* alignedSpeech = NULL; |
| AlignedSegment* alignedSegment = NULL; |
| bool didLookRight = false; |
| bool didLookLeft = false; |
| |
| for(size_t i = 0; i < gas->GetNbOfGraphAlignedToken(); ++i) |
| { |
| refSegment = NULL; |
| ref = gas->GetNonNullReference(i); |
| hyp = gas->GetNonNullHypothesis(i); |
| |
| |
| if(ref == NULL) |
| { |
| didLookLeft = false; |
| didLookRight = false; |
| |
| do |
| { |
| refSegment = NULL; |
| |
| |
| |
| |
| if( (lastNonNullRef != NULL) && !didLookLeft ) |
| { |
| refSegment = lastNonNullRef->GetParentSegment(); |
| didLookLeft = true; |
| |
| } |
| else if (!didLookRight) |
| { |
| refSegment = LookRight(i, &nextNonNullRef, gas); |
| didLookRight = true; |
| |
| } |
| |
| if(refSegment == NULL) |
| { |
| if(didLookRight) |
| { |
| |
| refSegment = segmentsGroup->GetRefSegmentByTime(hyp); |
| |
| } |
| else |
| { |
| |
| refSegment = LookRight(i, &nextNonNullRef, gas); |
| didLookRight = true; |
| |
| } |
| } |
| |
| |
| } |
| while (!hyp->OverlapWith(refSegment) && hyp->GetStartTime() >= 0 && hyp->GetEndTime() >= 0); |
| } |
| else |
| { |
| refSegment = ref->GetParentSegment(); |
| lastNonNullRef = ref; |
| } |
| |
| refSpeech = refSegment->GetParentSpeech(); |
| alignedSpeech = this->GetOrCreateAlignedSpeechFor(refSpeech, true); |
| alignedSegment = alignedSpeech->GetOrCreateAlignedSegmentFor(refSegment, true); |
| alignedSegment->AddTokenAlignment(ref, hyp_key, hyp); |
| alignedSegment->SetSegGrpID(segmentsGroup->GetsID()); |
| } |
| |
| |
| if (gas->GetNbOfGraphAlignedToken() == 0 && segmentsGroup->GetNumberOfReferences() != 0) |
| { |
| for (size_t i=0 ; i < segmentsGroup->GetNumberOfReferences() ; ++i) |
| { |
| vector<Segment*> refsSeg = segmentsGroup->GetReference(i); |
| |
| for (size_t j=0 ; j < refsSeg.size() ; ++j) |
| { |
| refSpeech = refsSeg[j]->GetParentSpeech(); |
| alignedSpeech = this->GetOrCreateAlignedSpeechFor(refSpeech, true); |
| alignedSpeech->GetOrCreateAlignedSegmentFor(refsSeg[j], true); |
| } |
| } |
| } |
| } |
|
|
| Segment* Alignment::LookRight(const size_t& gatIndex, Token** nextNonNullRef, GraphAlignedSegment* gas) |
| { |
| *nextNonNullRef = gas->GetNextNonNullReference(gatIndex); |
| |
| if(*nextNonNullRef != NULL) |
| { |
| return (*nextNonNullRef)->GetParentSegment(); |
| } |
| |
| return NULL; |
| } |
|
|
| AlignedSpeechIterator* Alignment::AlignedSpeeches() |
| { |
| return new AlignedSpeechIterator(this); |
| } |
|
|