if (!sourcePhrase.startsWithNonterminal() && !sourcePhrase.endsWithNonterminal()) {
if (logger.isLoggable(Level.FINER)) logger.finer("Case 1: Source phrase !startsWithNT && !endsWithNT");
// Get target span
Span sourceSpan = sourcePhrase.getSpan(sourcePhraseIndex);
Span targetSpan = alignments.getConsistentTargetSpan(sourceSpan);
// If target span and source span are consistent
if (targetSpan!=null && targetSpan.size()>=sourcePhrase.arity()+1 && targetSpan.size()<=maxPhraseSpan) {
// Construct a translation
HierarchicalPhrase translation = constructTranslation(sourcePhrase, sourcePhraseIndex, sourceSpan, targetSpan, false, false);
if (translation != null) {
if (logger.isLoggable(Level.FINEST)) logger.finest("\tCase 1: Adding translation: '" + translation + "' for target span " + targetSpan + " from source span " + sourceSpan);
return translation;
} else if (logger.isLoggable(Level.FINER)) {
logger.finer("No valid translation returned from attempt to construct translation for source span " + sourceSpan + ", target span " + targetSpan);
}
}
}
// Case 2: If sourcePhrase startsWithNT && !endsWithNT
else if (sourcePhrase.startsWithNonterminal() && !sourcePhrase.endsWithNonterminal()) {
if (logger.isLoggable(Level.FINER)) logger.finer("Case 2: Source phrase startsWithNT && !endsWithNT");
int sentenceNumber = sourcePhrase.getSentenceNumber(sourcePhraseIndex);
int startOfSentence = sourceSuffixArray.getCorpus().getSentencePosition(sentenceNumber);
int startOfTerminalSequence = sourcePhrase.getFirstTerminalIndex(sourcePhraseIndex);
int endOfTerminalSequence = sourcePhrase.getLastTerminalIndex(sourcePhraseIndex);
// Start by assuming the initial source nonterminal starts one word before the first source terminal
Span possibleSourceSpan = new Span(startOfTerminalSequence-1, endOfTerminalSequence);
// Loop over all legal source spans
// (this is variable because we don't know the length of the NT span)
// looking for a source span with a consistent translation
while (possibleSourceSpan.start >= startOfSentence &&
startOfTerminalSequence-possibleSourceSpan.start<=maxNonterminalSpan &&
endOfTerminalSequence-possibleSourceSpan.start<=maxPhraseSpan) {
// Get target span
Span targetSpan = alignments.getConsistentTargetSpan(possibleSourceSpan);
// If target span and source span are consistent
if (targetSpan!=null && targetSpan.size()>=sourcePhrase.arity()+1 && targetSpan.size()<=maxPhraseSpan) {
// Construct a translation
HierarchicalPhrase translation = constructTranslation(sourcePhrase, sourcePhraseIndex, possibleSourceSpan, targetSpan, true, false);
if (translation != null) {
if (logger.isLoggable(Level.FINEST)) logger.finest("\tCase 2: Adding translation: '" + translation + "' for target span " + targetSpan + " from source span " + possibleSourceSpan);
return translation;
}
}
possibleSourceSpan.start--;
}
}
// Case 3: If sourcePhrase !startsWithNT && endsWithNT
else if (!sourcePhrase.startsWithNonterminal() && sourcePhrase.endsWithNonterminal()) {
if (logger.isLoggable(Level.FINER)) logger.finer("Case 3: Source phrase !startsWithNT && endsWithNT");
int endOfSentence = sourceSuffixArray.getCorpus().getSentenceEndPosition(sourcePhrase.getSentenceNumber(sourcePhraseIndex));
int startOfTerminalSequence = sourcePhrase.getFirstTerminalIndex(sourcePhraseIndex);
int endOfTerminalSequence = sourcePhrase.getLastTerminalIndex(sourcePhraseIndex);
// Start by assuming the initial source nonterminal starts one word after the last source terminal
Span possibleSourceSpan =
new Span(startOfTerminalSequence, endOfTerminalSequence+1);
// Loop over all legal source spans
// (this is variable because we don't know the length of the NT span)
// looking for a source span with a consistent translation
while (possibleSourceSpan.end <= endOfSentence &&
possibleSourceSpan.end - endOfTerminalSequence <= maxNonterminalSpan &&
possibleSourceSpan.size()<=maxPhraseSpan) {
// Get target span
Span targetSpan = alignments.getConsistentTargetSpan(possibleSourceSpan);
// If target span and source span are consistent
if (targetSpan!=null && targetSpan.size()>=sourcePhrase.arity()+1 && targetSpan.size()<=maxPhraseSpan) {
// Construct a translation
HierarchicalPhrase translation = constructTranslation(sourcePhrase, sourcePhraseIndex, possibleSourceSpan, targetSpan, false, true);
if (translation != null) {
if (logger.isLoggable(Level.FINEST)) logger.finest("\tCase 3: Adding translation: '" + translation + "' for target span " + targetSpan + " from source span " + possibleSourceSpan);
return translation;
}
}
possibleSourceSpan.end++;
}
}
// Case 4: If sourcePhrase startsWithNT && endsWithNT
else if (sourcePhrase.startsWithNonterminal() && sourcePhrase.endsWithNonterminal()) {
if (logger.isLoggable(Level.FINER)) logger.finer("Case 4: Source phrase startsWithNT && endsWithNT");
int sentenceNumber = sourcePhrase.getSentenceNumber(sourcePhraseIndex);
int startOfSentence = sourceSuffixArray.getCorpus().getSentencePosition(sentenceNumber);
int endOfSentence = sourceSuffixArray.getCorpus().getSentenceEndPosition(sentenceNumber);
int startOfTerminalSequence = sourcePhrase.getFirstTerminalIndex(sourcePhraseIndex);
int endOfTerminalSequence = sourcePhrase.getLastTerminalIndex(sourcePhraseIndex);
// Start by assuming the initial source nonterminal
// starts one word before the first source terminal and
// ends one word after the last source terminal
Span possibleSourceSpan =
new Span(startOfTerminalSequence-1, endOfTerminalSequence+1);
// Loop over all legal source spans
// (this is variable because we don't know the length of the NT span)
// looking for a source span with a consistent translation
while (possibleSourceSpan.start >= startOfSentence &&
possibleSourceSpan.end <= endOfSentence &&
startOfTerminalSequence-possibleSourceSpan.start<=maxNonterminalSpan &&
possibleSourceSpan.end-endOfTerminalSequence<=maxNonterminalSpan &&
possibleSourceSpan.size()<=maxPhraseSpan) {
// Get target span
Span targetSpan = alignments.getConsistentTargetSpan(possibleSourceSpan);
// If target span and source span are consistent
if (targetSpan!=null && targetSpan.size()>=sourcePhrase.arity()+1 && targetSpan.size()<=maxPhraseSpan) {
// Construct a translation
HierarchicalPhrase translation = constructTranslation(sourcePhrase, sourcePhraseIndex, possibleSourceSpan, targetSpan, true, true);
if (translation != null) {