Class Segments
- java.lang.Object
-
- net.sf.okapi.common.resource.Segments
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from interface net.sf.okapi.common.resource.ISegments
ISegments.MetaCopyStrategy
-
-
Constructor Summary
Constructors Constructor Description Segments()Segments(TextContainer parent)Creates an uninitialized Segments object.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidappend(Segment segment)Appends a segment at the end of this container.voidappend(Segment segment, boolean collapseIfPreviousEmpty)Appends a segment at the end of this container.voidappend(Segment segment, String textBefore)Appends a segment at the end of this container, with an optional non-segment part just before.voidappend(Segment segment, String textBefore, boolean collapseIfPreviousEmpty)Appends a segment at the end of this container, with an optional non-segment part just before.voidappend(TextFragment fragment)Appends a TextFragment as a segment at the end of this container.voidappend(TextFragment fragment, boolean collapseIfPreviousEmpty)Appends a TextFragment as a segment at the end of this container.List<Segment>asList()Get all segments.intcount()Gets the number of segments in this container.intcreate(int start, int end)Creates a segment in this container.intcreate(List<Range> ranges)Helper method to create segments without empty segments.intcreate(List<Range> ranges, boolean allowEmptySegments)Creates a set of segments in this container.intcreate(List<Range> ranges, boolean allowEmptySegments, ISegments.MetaCopyStrategy strategy)Segmentget(int index)Gets the segment for a given segment index.Segmentget(String id)Gets the segment for a given identifier.AlignmentStatusgetAlignmentStatus()Returns theAlignmentStatusfor these segments.TextFragmentgetFirstContent()Gets the content of the first segment of this container.intgetIndex(String segId)Gets the segment index for a given segment id.SegmentgetLast()Gets the lastSegmentof the container.TextFragmentgetLastContent()Gets the content of last segment of this container.TextContainergetParent()intgetPartIndex(int segIndex)Gets the part index for a given segment index.List<TextPart>getParts()List<Range>getRanges()Gets the list of the boundaries for the current segments in this container.List<Range>getRanges(boolean keepCodeIds)Gets the list of the boundaries for the current segments in this container.voidinsert(int index, Segment seg)Inserts a given segment at the specified position.Iterator<Segment>iterator()Gets an iterator for the segments of this container.voidjoinAll()Merges back together all segments of this TextContainer object, and clear the list of segments.voidjoinAll(boolean keepCodeIds)Merges back together all segments of this TextContainer object, and clear the list of segments.voidjoinAll(List<Range> ranges)Merges back together all segments of this TextContainer object, and clear the list of segments.intjoinWithNext(int segmentIndex)Joins to a given segment all the parts between that segment and the next, as well as the next segment.voidset(int index, Segment seg)Sets a new segment at a given segment index.voidsetAlignmentStatus(AlignmentStatus alignmentStatus)Sets theAlignmentStatusfor these segments.voidsetParts(List<TextPart> parts)Sets the list of TextPart objects in which the segments for this Segments object are located.voidswap(int segIndex1, int segIndex2)Swaps two segments in this container.voidvalidateSegmentId(Segment seg)Checks if the id of a given segment is empty, null or a duplicate.-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface java.lang.Iterable
forEach, spliterator
-
-
-
-
Constructor Detail
-
Segments
public Segments()
-
Segments
public Segments(TextContainer parent)
Creates an uninitialized Segments object.IMPORTANT: setParts() must be called with a non-null argument before calling any other methods.
- Parameters:
parent- the parentTextContainer.
-
-
Method Detail
-
setParts
public void setParts(List<TextPart> parts)
Sets the list of TextPart objects in which the segments for this Segments object are located. Parts must be set after construction before any other methods are invoked.- Parameters:
parts- the list ofTextParts where the segments are stored.
-
iterator
public Iterator<Segment> iterator()
Description copied from interface:ISegmentsGets an iterator for the segments of this container. This iterator does not iterate through non-segment parts of the content. UseISegments.iterator()for accessing both segments and non-segments parts.
-
asList
public List<Segment> asList()
Description copied from interface:ISegmentsGet all segments. Excludes inter-segment parts.
-
swap
public void swap(int segIndex1, int segIndex2)Description copied from interface:ISegmentsSwaps two segments in this container.For example, if you have a container "[segment1] [segment2]" and call
swap(0,1)the resulting container becomes: "[segment2] [segment1]".Note that the segments identifiers stay with their segment.
-
append
public void append(Segment segment, boolean collapseIfPreviousEmpty)
Description copied from interface:ISegmentsAppends a segment at the end of this container. If there is no content after the last segment, and the last segment is empty, the new segment replaces the last one (including its id, and the new id is validated). Otherwise the new segment is appended to the content as a new segment part and its id is validated.
-
append
public void append(Segment segment)
Description copied from interface:ISegmentsAppends a segment at the end of this container.This call is the same as calling
ISegments.append(Segment, boolean)with collapseIfPreviousEmpty set to true.
-
append
public void append(Segment segment, String textBefore, boolean collapseIfPreviousEmpty)
Description copied from interface:ISegmentsAppends a segment at the end of this container, with an optional non-segment part just before.If collapseIfPreviousEmpty is true and collapseIfPreviousEmpty is empty or null, and if the last part of the container is an empty segment, this new segment replaces the last one (including its id, and the new id is validated). Otherwise the new segment is appended to the content as a new segment part and its id is validated.
- Specified by:
appendin interfaceISegments- Parameters:
segment- the segment to append.textBefore- the text of the non-segment part before the segment (can be null).collapseIfPreviousEmpty- true to collapse the new segment if the last part of the container is an empty segment (that is if textBefore is also empty or null).
-
append
public void append(Segment segment, String textBefore)
Description copied from interface:ISegmentsAppends a segment at the end of this container, with an optional non-segment part just before.This call is the same as calling
ISegments.append(Segment, String, boolean)with collapseIfPreviousEmpty set to true.
-
append
public void append(TextFragment fragment, boolean collapseIfPreviousEmpty)
Description copied from interface:ISegmentsAppends a TextFragment as a segment at the end of this container.If collapseIfPreviousEmpty is true and if the last part of the container is an empty segment, this new segment replaces the last one. Otherwise the new segment is appended to the content as a new segment part. In all case the id of the new segment is set automatically.
-
append
public void append(TextFragment fragment)
Description copied from interface:ISegmentsAppends a TextFragment as a segment at the end of this container.This call is the same as calling
ISegments.append(TextFragment, boolean)with collapseIfPreviousEmpty set to true.
-
set
public void set(int index, Segment seg)Description copied from interface:ISegmentsSets a new segment at a given segment index.If the new segment's id exists already in the container, it is replaced by a valid id.
-
insert
public void insert(int index, Segment seg)Description copied from interface:ISegmentsInserts a given segment at the specified position.If the segment to insert has no id or an id that is already used in the text container, the id is automatically changed to a new valid id.
- Specified by:
insertin interfaceISegments- Parameters:
index- the segment index position. If the given position is the same as the number of segments in the container, the call is the same asISegments.append(Segment, boolean)with collapseIfPreviousEmpty set to true.seg- the segment to insert.
-
create
public int create(List<Range> ranges)
Description copied from interface:ISegmentsHelper method to create segments without empty segments. This is the same as callingISegments.create(List, boolean)with false.- Specified by:
createin interfaceISegments- Parameters:
ranges- the ranges of the segments to create.- Returns:
- the number of parts (segments and non-segments) created during the operation.
- See Also:
ISegments.create(List, boolean)
-
create
public int create(List<Range> ranges, boolean allowEmptySegments)
Description copied from interface:ISegmentsCreates a set of segments in this container. UseTextContainer.getCodedText()to get the coded text to use as the base for the segment boundaries. If the content is already segmented, it is automatically un-segmented before the new segmentation is applied.- Specified by:
createin interfaceISegments- Parameters:
ranges- the ranges of the segments to create. The ranges must be ordered from the lesser position to the higher one (i.e. from left to right). If this parameter is empty or null, no modification is done.allowEmptySegments- true to allow empty segments to be created, false to skip them.- Returns:
- the number of parts (segments and non-segments) created during the operation.
-
create
public int create(List<Range> ranges, boolean allowEmptySegments, ISegments.MetaCopyStrategy strategy)
-
create
public int create(int start, int end)Description copied from interface:ISegmentsCreates a segment in this container. UseTextContainer.getCodedText()to get the coded text to use as the base for the segment boundaries. If the content is already segmented, it is automatically un-segmented before the new segmentation is applied. If start and end position are the same, no segment is created for those boundaries.For example:
- calling createSegment(2,3) on "a b c" will result in: "a [b] c".
- calling createSegment(2,3) on "[a b] [c]" will result in: "a [b] c".
-
count
public int count()
Description copied from interface:ISegmentsGets the number of segments in this container. This method always returns at least 1.- Specified by:
countin interfaceISegments- Returns:
- the number of segments in the container.
- See Also:
TextContainer.count()
-
getFirstContent
public TextFragment getFirstContent()
Description copied from interface:ISegmentsGets the content of the first segment of this container.- Specified by:
getFirstContentin interfaceISegments- Returns:
- the content of the first segment of this container.
- See Also:
TextContainer.getFirstContent(),ISegments.getLastContent(),TextContainer.getLastContent()
-
getLastContent
public TextFragment getLastContent()
Description copied from interface:ISegmentsGets the content of last segment of this container.- Specified by:
getLastContentin interfaceISegments- Returns:
- the content of the last segment of this container.
- See Also:
ISegments.getLastContent(),TextContainer.getFirstContent(),TextContainer.getFirstContent()
-
getLast
public Segment getLast()
Description copied from interface:ISegmentsGets the lastSegmentof the container.
-
get
public Segment get(String id)
Description copied from interface:ISegmentsGets the segment for a given identifier.
-
get
public Segment get(int index)
Description copied from interface:ISegmentsGets the segment for a given segment index.- Specified by:
getin interfaceISegments- Parameters:
index- the index of the segment to retrieve. The first segment has the index 0, the second has the index 1, etc. Note that the index value used here is not necessarily the same index as for a part. That is:getSegment(0)returns the same segment asgetPart(0)only if the first part of the container is a segment.- Returns:
- the segment for the given index.
Use
ISegments.get(String)to retrieve by segment identifier. - See Also:
ISegments.get(String),#see
-
joinAll
public void joinAll()
Description copied from interface:ISegmentsMerges back together all segments of this TextContainer object, and clear the list of segments. The content becomes a single segment content.- Specified by:
joinAllin interfaceISegments- See Also:
ISegments.joinAll(List)
-
joinAll
public void joinAll(boolean keepCodeIds)
Description copied from interface:ISegmentsMerges back together all segments of this TextContainer object, and clear the list of segments. The content becomes a single segment content.- Specified by:
joinAllin interfaceISegments- Parameters:
keepCodeIds- if true retain original code ids, otherwise rebalance- See Also:
ISegments.joinAll()
-
joinAll
public void joinAll(List<Range> ranges)
Description copied from interface:ISegmentsMerges back together all segments of this TextContainer object, and clear the list of segments. If required, the existing segment boundaries are saved in a given list of ranges. The content becomes a single segment content.
-
getRanges
public List<Range> getRanges()
Description copied from interface:ISegmentsGets the list of the boundaries for the current segments in this container.
-
getRanges
public List<Range> getRanges(boolean keepCodeIds)
Description copied from interface:ISegmentsGets the list of the boundaries for the current segments in this container.
-
joinWithNext
public int joinWithNext(int segmentIndex)
Description copied from interface:ISegmentsJoins to a given segment all the parts between that segment and the next, as well as the next segment.For example for the content: " [seg1] [seg2] ", the call joinWithNext(0) will give the result: " [seg1 seg2] ". And the call joinWithNext(1) will give the result: " [seg1] [seg2] " (no change because there is no segment after the segment 1.
- Specified by:
joinWithNextin interfaceISegments- Parameters:
segmentIndex- index of the segment (not the part) where to append the next segment.- Returns:
- the number of parts joined to the given segment (and removed from the list of parts).
-
getPartIndex
public int getPartIndex(int segIndex)
Description copied from interface:ISegmentsGets the part index for a given segment index.For example in the container "[segment1] [segment2] [segment3]" the segment index for "[segment2]" is 1 and its part index is 2 because there is one non-segment part before.
- Specified by:
getPartIndexin interfaceISegments- Parameters:
segIndex- the segment index to convert to part index.- Returns:
- the index of the part for the given segment index, or -1 if the segment was not found.
-
getIndex
public int getIndex(String segId)
Description copied from interface:ISegmentsGets the segment index for a given segment id.
-
getAlignmentStatus
public AlignmentStatus getAlignmentStatus()
Description copied from interface:ISegmentsReturns theAlignmentStatusfor these segments.- Specified by:
getAlignmentStatusin interfaceISegments- Returns:
- the
AlignmentStatus
-
setAlignmentStatus
public void setAlignmentStatus(AlignmentStatus alignmentStatus)
Description copied from interface:ISegmentsSets theAlignmentStatusfor these segments.- Specified by:
setAlignmentStatusin interfaceISegments- Parameters:
alignmentStatus- the newAlignmentStatusvalue.
-
validateSegmentId
public void validateSegmentId(Segment seg)
Checks if the id of a given segment is empty, null or a duplicate. If it is, the id is automatically set to a new value auto-generated.- Parameters:
seg- the segment to verify and possibly modify.
-
getParent
public TextContainer getParent()
-
-