public void getReferenceTableTexts(TableCandidate tc,
	    ArrayList<ArrayList<TextPiece>> linesByPage) {
	/*
	 * Gets the table caption beginning part, e.g., "Table 4"
	 */
	String thisCaption = tc.getCaption().replace(" ", "");
	char lastCharInThisKwd = tc.getKeyword().charAt(
		tc.getKeyword().length() - 1);
	int indexAfterKwd = thisCaption.indexOf(lastCharInThisKwd);
	int ii = indexAfterKwd + 1;
	int captionIndex_start = ii;
	int captionIndex_end = ii + 1;
	while ((ii < thisCaption.length() - 1)
		&& (captionIndex_end - captionIndex_start < 2)
		&& (captionIndex_end >= captionIndex_start)) { // for: Table 12
							       // 4 datasets.
							       // for such case,
							       // we may get
							       // "Tabl124" to
							       // start the
							       // reference text
							       // search,
							       // without the
							       // 2nd condition.
	    String nextChar = thisCaption.substring(ii, ii + 1);
	    if ((nextChar.compareToIgnoreCase(".") != 0)
		    && (nextChar.matches("[0-9]") == true)
		    && (nextChar.compareToIgnoreCase(":") != 0)
		    && (nextChar.compareToIgnoreCase(",") != 0))
		ii++;
	    else {
		captionIndex_end = ii;
		break;
	    }
	}
	String thisTableCaptionBeginning = thisCaption.substring(0, ii);
	/*
	 * Gets each character in the table caption beginning part
	 */
	int cationBeginningLength = thisTableCaptionBeginning.length();
	char[] charsInCaptionBeginning = new char[cationBeginningLength];
	for (int i = 0; i < cationBeginningLength; i++) {
	    charsInCaptionBeginning[i] = thisTableCaptionBeginning.charAt(i);
	}

	if (linesByPage != null && linesByPage.size() != 0
		&& thisTableCaptionBeginning.length() > 5) {
	    for (int k = 0; k < linesByPage.size(); k++) {
		ArrayList<TextPiece> linesThisPage = linesByPage.get(k);
		for (int j = 0; j < linesThisPage.size(); j++) {
		    String thisLine = linesThisPage.get(j).getText()
			    .replace(" ", "");
		    if ((k == tc.getPageId_thisTable())
			    && (j >= tc.getCaptionStartLine())
			    && (j <= tc.getCaptionEndLine())) {
		    }// filter out the table caption itself
		    else {
			if ((thisLine).contains(thisTableCaptionBeginning) == true) {
			    /*
			     * Finds the Index of the Table Caption beginning
			     * part in the matched document line. E.g., the "2"
			     * in "Table 2."
			     */
			    String thisLine_Orig = linesThisPage.get(j)
				    .getText();
			    int kk = thisLine
				    .indexOf(thisTableCaptionBeginning);

			    int beginIndex = thisLine_Orig.indexOf(
				    charsInCaptionBeginning[0], kk);
			    int endIndex = thisLine_Orig
				    .indexOf(
					    charsInCaptionBeginning[cationBeginningLength - 1],
					    beginIndex) + 1;
			    String thisPiece = "";
			    while (beginIndex < thisLine_Orig.length()
				    && endIndex < thisLine_Orig.length()
				    && (endIndex - beginIndex) > 5
				    && beginIndex >= 0 && endIndex > 0) {
				thisPiece = thisLine_Orig.substring(beginIndex,
					endIndex).replace(" ", "");
				/*
				 * for "Table 11", "thisPiece" can be "Table1"
				 */
				if (thisTableCaptionBeginning
					.startsWith(thisPiece)) {
				    while ((thisPiece.length() < thisTableCaptionBeginning
					    .length())
					    && (endIndex < thisLine_Orig
						    .length() - 1)) {
					endIndex++;
					thisPiece = thisLine_Orig.substring(
						beginIndex, endIndex).replace(
						" ", "");
				    }
				    break;
				} else {
				    beginIndex = thisLine_Orig.indexOf(
					    charsInCaptionBeginning[0],
					    endIndex);
				    endIndex = thisLine_Orig
					    .indexOf(
						    charsInCaptionBeginning[cationBeginningLength - 1],
						    beginIndex);
				}
			    }

			    if ((endIndex < thisLine_Orig.length() - 3)
				    && (endIndex > 0)
				    && (beginIndex >= 0)
				    && (thisLine_Orig.substring(endIndex,
					    endIndex + 2).matches("[ ][0-9]") == false)
				    && (thisLine_Orig.substring(endIndex,
					    endIndex + 2).matches("[0-9][.]") == false)
				    && (thisLine_Orig.substring(endIndex,
					    endIndex + 2).matches("[0-9][ ]") == false)
				    && (thisLine_Orig.substring(endIndex,
					    endIndex + 2).matches("[.][0-9]") == false)) {
				String firstHalfLine = thisLine_Orig.substring(
					0, beginIndex);
				String secondHalfLine = thisLine_Orig
					.substring(endIndex);

				/*
				 * Looks up all text of the reference
				 */
				int totalPeriodNum = 0;
				int periodNum_thisLine = 0;
				String curRef = "";
				int nextIndex = j;
				String line = secondHalfLine;
				while (nextIndex < linesThisPage.size() - 1) {
				    if ((k == tc.getPageId_thisTable())
					    && nextIndex <= tc.getBodyEndLine()
					    && nextIndex >= tc
						    .getBodyStartLine()) {
					break;
				    }
				    /*
				     * Gets the number of periods in the line.
				     * If the total period number < 2, get the
				     * whole line, otherwise, cut and break
				     */
				    for (int qq = 0; qq < line.length(); qq++) {
					if (line.substring(qq, qq + 1)
						.contentEquals(".") == true)
					    periodNum_thisLine++;
				    }
				    totalPeriodNum = totalPeriodNum
					    + periodNum_thisLine;
				    if (totalPeriodNum < 2) {
					curRef = curRef + " " + line;
					nextIndex++;
					line = linesThisPage.get(nextIndex)
						.getText();
					periodNum_thisLine = 0;
				    } else {
					if (totalPeriodNum == 2) {
					    if (periodNum_thisLine == 1) {
						String lines = line.substring(
							0,
							line.indexOf(".") + 1);
						curRef = curRef + " " + lines;
						break;
					    }
					    if (periodNum_thisLine == 2) {
						String lines = line
							.substring(
								0,
								line.lastIndexOf(".") + 1);
						curRef = curRef + " " + lines;
						break;
					    }
					}
					/*
					 * totalPeriodNum>2. In this case,
					 * periodNum_thisLine will >=2
					 */
					else {
					    int firstDot = line.indexOf(".");
					    int secondDot = line.indexOf(".",
						    firstDot);
					    String lines = line.substring(0,
						    secondDot + 1);
					    curRef = curRef + " " + lines;
					    break;
					}
				    }
				}

				/*
				 * Looks up the previous period "."
				 */
				totalPeriodNum = 0;
				periodNum_thisLine = 0;
				String preRef = "";
				int preIndex = j;
				line = firstHalfLine;
				while (preIndex > 0) {
				    if ((k == tc.getPageId_thisTable())
					    && preIndex <= tc.getBodyEndLine()
					    && preIndex >= tc
						    .getBodyStartLine()) {
					break;
				    }
				    /*
				     * Gets the period number in line, if total
				     * period number < 2, get the whole line,
				     * otherwise, cut and break
				     */
				    for (int qq = 0; qq < line.length(); qq++) {
					if (line.substring(qq, qq + 1)
						.contentEquals(".") == true)
					    periodNum_thisLine++;
				    }
				    totalPeriodNum = totalPeriodNum
					    + periodNum_thisLine;
				    if (totalPeriodNum < 2) {
					preRef = line + " " + preRef;
					preIndex--;
					line = linesThisPage.get(preIndex)
						.getText();
					periodNum_thisLine = 0;
				    } else {
					/*
					 * No matter this line has 1 or 2 ".",
					 * we get the part after the first "."
					 */
					if (totalPeriodNum == 2) {
					    String lines = line.substring(
						    line.indexOf(".") + 1,
						    line.length());
					    preRef = lines + " " + preRef;
					    break;
					}
					/*
					 * totalPeriodNum>2. In this case,
					 * periodNum_thisLine will >=2
					 */
					else {
					    int lastDot = line.lastIndexOf(".");
					    int secondLastDot = line.substring(
						    lastDot).lastIndexOf(".");
					    String lines = line.substring(
						    secondLastDot + 1,
						    line.length());
					    preRef = lines + " " + preRef;
					    break;
					}
				    }
				}

				String thisReference = preRef
					+ thisTableCaptionBeginning + curRef;
				// thisReference =
				// tc.replaceAllSpecialChracters(thisReference);

				String referenceText = tc.getRefText()
					+ "\n In PAGE " + (k + 1) + ": ..."
					+ thisReference + "...";
				tc.setRefText(referenceText);
			    }
			}
		    }
		}// end for
	    }
	} else {
	    tc.setRefText("");
	}

    }
public void getReferenceTableTexts ( TableCandidate tc , ArrayList < ArrayList < TextPiece > > linesByPage ) { String thisCaption = tc . getCaption ( ) . replace ( " " , "" ) ; char lastCharInThisKwd = tc . getKeyword ( ) . charAt ( tc . getKeyword ( ) . length ( ) - 1 ) ; int indexAfterKwd = thisCaption . indexOf ( lastCharInThisKwd ) ; int ii = indexAfterKwd + 1 ; int captionIndex_start = ii ; int captionIndex_end = ii + 1 ; while ( ( ii < thisCaption . length ( ) - 1 ) && ( captionIndex_end - captionIndex_start < 2 ) && ( captionIndex_end >= captionIndex_start ) ) { String nextChar = thisCaption . substring ( ii , ii + 1 ) ; if ( ( nextChar . compareToIgnoreCase ( "." ) != 0 ) && ( nextChar . matches ( "[0-9]" ) == true ) && ( nextChar . compareToIgnoreCase ( ":" ) != 0 ) && ( nextChar . compareToIgnoreCase ( "," ) != 0 ) ) ii ++ ; else { captionIndex_end = ii ; break ; } } String thisTableCaptionBeginning = thisCaption . substring ( 0 , ii ) ; int cationBeginningLength = thisTableCaptionBeginning . length ( ) ; char [ ] charsInCaptionBeginning = new char [ cationBeginningLength ] ; for ( int i = 0 ; i < cationBeginningLength ; i ++ ) { charsInCaptionBeginning [ i ] = thisTableCaptionBeginning . charAt ( i ) ; } if ( linesByPage != null && linesByPage . size ( ) != 0 && thisTableCaptionBeginning . length ( ) > 5 ) { for ( int k = 0 ; k < linesByPage . size ( ) ; k ++ ) { ArrayList < TextPiece > linesThisPage = linesByPage . get ( k ) ; for ( int j = 0 ; j < linesThisPage . size ( ) ; j ++ ) { String thisLine = linesThisPage . get ( j ) . getText ( ) . replace ( " " , "" ) ; if ( ( k == tc . getPageId_thisTable ( ) ) && ( j >= tc . getCaptionStartLine ( ) ) && ( j <= tc . getCaptionEndLine ( ) ) ) { } else { if ( ( thisLine ) . contains ( thisTableCaptionBeginning ) == true ) { String thisLine_Orig = linesThisPage . get ( j ) . getText ( ) ; int kk = thisLine . indexOf ( thisTableCaptionBeginning ) ; int beginIndex = thisLine_Orig . indexOf ( charsInCaptionBeginning [ 0 ] , kk ) ; int endIndex = thisLine_Orig . indexOf ( charsInCaptionBeginning [ cationBeginningLength - 1 ] , beginIndex ) + 1 ; String thisPiece = "" ; while ( beginIndex < thisLine_Orig . length ( ) && endIndex < thisLine_Orig . length ( ) && ( endIndex - beginIndex ) > 5 && beginIndex >= 0 && endIndex > 0 ) { thisPiece = thisLine_Orig . substring ( beginIndex , endIndex ) . replace ( " " , "" ) ; if ( thisTableCaptionBeginning . startsWith ( thisPiece ) ) { while ( ( thisPiece . length ( ) < thisTableCaptionBeginning . length ( ) ) && ( endIndex < thisLine_Orig . length ( ) - 1 ) ) { endIndex ++ ; thisPiece = thisLine_Orig . substring ( beginIndex , endIndex ) . replace ( " " , "" ) ; } break ; } else { beginIndex = thisLine_Orig . indexOf ( charsInCaptionBeginning [ 0 ] , endIndex ) ; endIndex = thisLine_Orig . indexOf ( charsInCaptionBeginning [ cationBeginningLength - 1 ] , beginIndex ) ; } } if ( ( endIndex < thisLine_Orig . length ( ) - 3 ) && ( endIndex > 0 ) && ( beginIndex >= 0 ) && ( thisLine_Orig . substring ( endIndex , endIndex + 2 ) . matches ( "[ ][0-9]" ) == false ) && ( thisLine_Orig . substring ( endIndex , endIndex + 2 ) . matches ( "[0-9][.]" ) == false ) && ( thisLine_Orig . substring ( endIndex , endIndex + 2 ) . matches ( "[0-9][ ]" ) == false ) && ( thisLine_Orig . substring ( endIndex , endIndex + 2 ) . matches ( "[.][0-9]" ) == false ) ) { String firstHalfLine = thisLine_Orig . substring ( 0 , beginIndex ) ; String secondHalfLine = thisLine_Orig . substring ( endIndex ) ; int totalPeriodNum = 0 ; int periodNum_thisLine = 0 ; String curRef = "" ; int nextIndex = j ; String line = secondHalfLine ; while ( nextIndex < linesThisPage . size ( ) - 1 ) { if ( ( k == tc . getPageId_thisTable ( ) ) && nextIndex <= tc . getBodyEndLine ( ) && nextIndex >= tc . getBodyStartLine ( ) ) { break ; } for ( int qq = 0 ; qq < line . length ( ) ; qq ++ ) { if ( line . substring ( qq , qq + 1 ) . contentEquals ( "." ) == true ) periodNum_thisLine ++ ; } totalPeriodNum = totalPeriodNum + periodNum_thisLine ; if ( totalPeriodNum < 2 ) { curRef = curRef + " " + line ; nextIndex ++ ; line = linesThisPage . get ( nextIndex ) . getText ( ) ; periodNum_thisLine = 0 ; } else { if ( totalPeriodNum == 2 ) { if ( periodNum_thisLine == 1 ) { String lines = line . substring ( 0 , line . indexOf ( "." ) + 1 ) ; curRef = curRef + " " + lines ; break ; } if ( periodNum_thisLine == 2 ) { String lines = line . substring ( 0 , line . lastIndexOf ( "." ) + 1 ) ; curRef = curRef + " " + lines ; break ; } } else { int firstDot = line . indexOf ( "." ) ; int secondDot = line . indexOf ( "." , firstDot ) ; String lines = line . substring ( 0 , secondDot + 1 ) ; curRef = curRef + " " + lines ; break ; } } } totalPeriodNum = 0 ; periodNum_thisLine = 0 ; String preRef = "" ; int preIndex = j ; line = firstHalfLine ; while ( preIndex > 0 ) { if ( ( k == tc . getPageId_thisTable ( ) ) && preIndex <= tc . getBodyEndLine ( ) && preIndex >= tc . getBodyStartLine ( ) ) { break ; } for ( int qq = 0 ; qq < line . length ( ) ; qq ++ ) { if ( line . substring ( qq , qq + 1 ) . contentEquals ( "." ) == true ) periodNum_thisLine ++ ; } totalPeriodNum = totalPeriodNum + periodNum_thisLine ; if ( totalPeriodNum < 2 ) { preRef = line + " " + preRef ; preIndex -- ; line = linesThisPage . get ( preIndex ) . getText ( ) ; periodNum_thisLine = 0 ; } else { if ( totalPeriodNum == 2 ) { String lines = line . substring ( line . indexOf ( "." ) + 1 , line . length ( ) ) ; preRef = lines + " " + preRef ; break ; } else { int lastDot = line . lastIndexOf ( "." ) ; int secondLastDot = line . substring ( lastDot ) . lastIndexOf ( "." ) ; String lines = line . substring ( secondLastDot + 1 , line . length ( ) ) ; preRef = lines + " " + preRef ; break ; } } } String thisReference = preRef + thisTableCaptionBeginning + curRef ; String referenceText = tc . getRefText ( ) + "\n In PAGE " + ( k + 1 ) + ": ..." + thisReference + "..." ; tc . setRefText ( referenceText ) ; } } } } } } else { tc . setRefText ( "" ) ; } }
Gets the reference text of this table in the whole document

@param tc
the object of the table candidate
@param linesByPage
the list of all lines in a document page