public static String joinWithOriginalWhiteSpace(List<CoreLabel> tokens) {
    if (tokens.size() == 0) {
      return "";
    }

    CoreLabel lastToken = tokens.get(0);
    StringBuffer buffer = new StringBuffer(lastToken.word());

    for (int i = 1; i < tokens.size(); i++) {
      CoreLabel currentToken = tokens.get(i);
      int numSpaces = currentToken.beginPosition() - lastToken.endPosition();
      if (numSpaces < 0) {
        numSpaces = 0;
      }

      buffer.append(repeat(' ', numSpaces) + currentToken.word());
      lastToken = currentToken;
    }

    return buffer.toString();
  }
public static String joinWithOriginalWhiteSpace ( List < CoreLabel > tokens ) { if ( tokens . size ( ) == 0 ) { return "" ; } CoreLabel lastToken = tokens . get ( 0 ) ; StringBuffer buffer = new StringBuffer ( lastToken . word ( ) ) ; for ( int i = 1 ; i < tokens . size ( ) ; i ++ ) { CoreLabel currentToken = tokens . get ( i ) ; int numSpaces = currentToken . beginPosition ( ) - lastToken . endPosition ( ) ; if ( numSpaces < 0 ) { numSpaces = 0 ; } buffer . append ( repeat ( ' ' , numSpaces ) + currentToken . word ( ) ) ; lastToken = currentToken ; } return buffer . toString ( ) ; }
Joins all the tokens together (more or less) according to their original whitespace.  It assumes all whitespace was " "
@param tokens list of tokens which implement {@link HasOffset} and {@link HasWord}
@return a string of the tokens with the appropriate amount of spacing