@Override public TextFragment getFirstContent() { for ( TextPart part : parts ) { if ( part.isSegment() ) { return part.getContent(); } } // Should never occur return null; }
/** * Appends a part with a given text at the end of this container. * <p> * If collapseIfPreviousEmpty is true and if the current last part (segment or non-segment) * is empty, the new text is appended to the last part part. * Otherwise the text is appended to the content as a new non-segment part. * * @param text the text to append. * @param collapseIfPreviousEmpty true to collapse the previous part if it is empty. */ public void append (String text, boolean collapseIfPreviousEmpty) { append(new TextPart(text), collapseIfPreviousEmpty); }
public static void convertTextPart_whitespaceCodesToText(TextPart textPart) { if (textPart.isSegment()) return; TextFragment tf = textPart.getContent(); if (tf.hasText()) return; if (Util.isEmpty(tf.toText().trim())) { // Move all codes into text textPart.setContent(new TextFragment(tf.toText())); } }
@Override public Segment clone () { TextPart part = super.clone(); Segment newSeg = new Segment(id, part.getContent()); newSeg.properties = part.properties; newSeg.annotations = part.annotations; return newSeg; }
/** * Returns the first {@link Segment} of this container. * @return the first {@link Segment} of this container or null if there is no {@link Segment} */ public Segment getFirstSegment() { for ( TextPart part : parts ) { if (part.isSegment()) { return (Segment) part; } } return null; }
if (( parts.get(parts.size()-1).getContent().isEmpty() ) && !parts.get(parts.size()-1).isSegment() ) parts.set(parts.size()-1, new TextPart(textBefore)); parts.add(new TextPart(textBefore)); if (( parts.get(parts.size()-1).getContent().isEmpty() ) && parts.get(parts.size()-1).isSegment() )
public static void renumberCodes(TextContainer tc) { for (TextPart textPart : tc) { textPart.getContent().renumberCodes(1); } }
/** * Changes the type of a given part. * If the part was a segment this makes it a non-segment (except if this is the only part * in the content. In that case the part remains unchanged). If this part was not a segment * this makes it a segment (with its identifier automatically set). * * @param partIndex the index of the part to change. Note that even if the part is a segment * this index must be the part index not the segment index. */ public void changePart(int partIndex) { if ( parts.get(partIndex).isSegment() ) { // If it's a segment, make it a non-segment if ( hasOnlyOneSegment() ) { // Except if it's the only segment, to ensure at-least-1-segment return; } parts.set(partIndex, new TextPart(parts.get(partIndex).text)); } else { // If it's a non-segment, make it a segment (with auto-id) Segment seg = new Segment(null, parts.get(partIndex).text); segments.validateSegmentId(seg); parts.set(partIndex, seg); segApplied = true; } }
/** * Appends a part at the end of this container. * <p> * If collapseIfPreviousEmpty and if the current last part (segment or non-segment) * is empty, the text fragment is appended to the last part. * Otherwise the text fragment is appended to the content as a new non-segment part. * <p> * Important: If the container is empty, the appended part becomes * a segment, as the container has always at least one segment. * * @param fragment the text fragment to append. * @param collapseIfPreviousEmpty true to collapse the previous part if it is empty. * @param keepCodeIds true to block code balancing. */ public void append(TextFragment fragment, boolean collapseIfPreviousEmpty, boolean keepCodeIds) { if ( collapseIfPreviousEmpty ) { // If the last part is empty we append to it if ( parts.get(parts.size()-1).getContent().isEmpty() ) { // Append the fragment to the segment or non-segment part parts.get(parts.size()-1).text.append(fragment, keepCodeIds); } else { // Else: like appending a TextPart append(new TextPart(fragment), false); } } else { append(new TextPart(fragment), false); } }
protected void removeTrailingWhitespace(ITextUnit textUnit) { TextContainer targetTextContainer = textUnit.getTarget(targetLocale); /** * If whitespace trimming was enabled during segmentation, the * whitespace will be trapped in non-Segment TextParts. So * we need to check everything in the container, not just the * results of tu.getTargetSegments(); */ for (TextPart targetTextPart : targetTextContainer.getParts()) { TextFragment textFragment = findAndRemoveWhitespacesAfterPunctuation(targetTextPart.getContent()); targetTextPart.setContent(textFragment); } }
/** * Clones this container, with or without its properties. * @param cloneProperties indicates if the properties should be cloned. * @return A new TextContainer object that is a copy of this one. */ public TextContainer clone (boolean cloneProperties) { TextContainer newCont = new TextContainer(); // Clone segments newCont.resetParts(); for ( TextPart part : parts ) { newCont.parts.add(part.clone()); } newCont.segApplied = segApplied; newCont.segments.setAlignmentStatus(segments.getAlignmentStatus()); // Clone the properties if ( cloneProperties && ( properties != null )) { newCont.properties = new LinkedHashMap<String, Property>(); for ( Property prop : properties.values() ) { newCont.properties.put(prop.getName(), prop.clone()); } } // Clone the annotations if ( annotations != null ) { newCont.annotations = annotations.clone(); } // Returns the new container return newCont; }
@Override public int count() { int count = 0; for ( TextPart part : parts ) { if ( part.isSegment() ) { count++; } } return count; }
while (index < tc.count()) { TextPart part = tc.get(index); if (part.isSegment()) { // Trimming only segments TextFragment tf = part.getContent(); trimLeading(tf, skel1); if (!skel1.isEmpty()) { tc.insert(index, new TextPart(skel1.toString())); trimTrailing(tf, skel2); if (!skel2.isEmpty()) { tc.insert(index + 1, new TextPart(skel2.toString()));
/** * Indicates if this container is empty (no text and no codes). * * @return true if this container is empty. */ public boolean isEmpty() { for ( TextPart part : parts ) { if ( !part.getContent().isEmpty() ) return false; } return true; }
List<TextPart> newParts = new LinkedList<>(); for (TextPart p : tc.getParts()) { if (p.isSegment()) { TextFragment[] res = simplifyCodes(p.text, rules, removeLeadingTrailingCodes, mergeCodes, true); if (removeLeadingTrailingCodes && res != null) { newParts.add(new TextPart(expandCodes(res[0]))); newParts.add(new TextPart(expandCodes(res[1])));
holder = parts.get(0).getContent(); parts.add(new TextPart(holder.subSequence(start, range.start))); if ( start == 0 ) { // If the remain is the whole content: make it a segment if ( parts.size() > 0 ) { parts.add(new TextPart(holder.subSequence(start, -1))); parts.add(new TextPart(holder.subSequence(start, -1)));
@Override public TextFragment getLastContent() { for ( int i=parts.size()-1; i>=0; i-- ) { if ( parts.get(i).isSegment() ) { return parts.get(i).getContent(); } } // Should never occur return null; }
@Override public Segment get(String id) { for ( TextPart part : parts ) { if ( part.isSegment() ) { if ( ((Segment)part).id.equals(id) ) return (Segment)part; } } // Should never occur return null; }
/** * Indicates if this container contains at least one character that is not a whitespace. * All parts (segments and non-segments) are checked. * * @param whiteSpacesAreText indicates if whitespaces should be considered * text characters or not. * @return true if this container contains at least one character that is not a whitespace. */ public boolean hasText (boolean whiteSpacesAreText) { for ( TextPart part : parts ) { if ( part.getContent().hasText(whiteSpacesAreText) ) return true; } return false; }
newPartIsSegment = part.isSegment(); parts.add(newPartIndex, new TextPart(part.text.subSequence(start, end))); parts.add(newPartIndex, new TextPart(part.text.subSequence(start, end))); if ( part.isSegment() ) { parts.add(newPartIndex+1, new Segment(null, part.text.subSequence(end, -1))); segments.validateSegmentId((Segment)parts.get(newPartIndex+1)); parts.add(newPartIndex+1, new TextPart(part.text.subSequence(end, -1)));