@Override public Iterator<Segment> iterator(LocaleId trgLoc) { return getSource(trgLoc).getSegments().iterator(); }
@Override public Segment getCorrespondingSource(Segment trgSeg, LocaleId trgLoc) { ISegments srcSegs = getSource(trgLoc).getSegments(); return srcSegs.get(trgSeg.id); }
@Override public Segment getSource(int index, LocaleId trgLoc) { return getSource(trgLoc).getSegments().get(index); }
@Override public void segmentSource(ISegmenter segmenter, LocaleId targetLocale) { TextContainer theSource = getSource(targetLocale); segmenter.computeSegments(theSource); theSource.getSegments().create(segmenter.getRanges()); }
/** * Force one to one alignment. Assume that both source and target have the * same number of segments. * * @param trgLoc * target locale used to align with the source */ @Override public void align(LocaleId trgLoc) { Iterator<Segment> srcSegsIt = getSource(trgLoc).getSegments().iterator(); Iterator<Segment> trgSegsIt = parent.createTarget(trgLoc, false, IResource.COPY_SEGMENTATION).getSegments() .iterator(); while (srcSegsIt.hasNext()) { try { Segment srcSeg = srcSegsIt.next(); Segment trgSeg = trgSegsIt.next(); trgSeg.id = srcSeg.id; } catch (NoSuchElementException e) { throw new OkapiMisAlignmentException("Different number of source and target segments", e); } } // these target segments are now aligned with their source counterparts parent.getTargetSegments(trgLoc).setAlignmentStatus(AlignmentStatus.ALIGNED); }
@Override public void alignCollapseAll(LocaleId trgLoc) { ContainerIterator ci = new ContainerIterator(trgLoc); // keeping track of collapsed containers to check which to set to // ALIGNED LinkedList<TextContainer> collapsed = new LinkedList<TextContainer>(); if (ci.hasSource()) { ci.getSource().joinAll(); ci.getSource().setHasBeenSegmentedFlag(false); collapsed.add(ci.getSource()); } if (ci.hasTarget()) { ci.getTarget().joinAll(); ci.getTarget().setHasBeenSegmentedFlag(false); collapsed.add(ci.getTarget()); } // mark target/source pairs aligned if both have been collapsed TextContainer src, trg; for (LocaleId loc : parent.getTargetLocales()) { src = getSource(loc); if (collapsed.contains(src)) { trg = parent.getTarget(loc); if (collapsed.contains(trg)) { trg.getSegments().setAlignmentStatus(AlignmentStatus.ALIGNED); // TODO: check that this is the desired behavior trg.getFirstSegment().id = src.getFirstSegment().getId(); } } } }
@Override public Segment splitSource(LocaleId trgLoc, Segment srcSeg, int splitPos) { TextContainer theSource = getSource(trgLoc); ISegments srcSegs = theSource.getSegments(); int segIndex = srcSegs.getIndex(srcSeg.id); if (segIndex == -1) return null; // segment id not found in the container int partIndex = srcSegs.getPartIndex(segIndex); // split the source theSource.split(partIndex, splitPos, splitPos, false); Segment newSeg = srcSegs.get(segIndex + 1); ISegments currentSegs; Segment currentSeg; ContainerIterator ci = new ContainerIterator(trgLoc); // inserting new segments in all the places they go if (ci.hasTarget()) { currentSegs = ci.getTarget().getSegments(); currentSeg = currentSegs.get(srcSeg.id); if (currentSeg != null) currentSegs.insert(currentSegs.getIndex(srcSeg.id) + 1, new Segment(srcSeg.id, new TextFragment(""))); } return newSeg; }
@Override public void setSegment(int index, Segment seg, LocaleId trgLoc) { ISegments segs = getSource(trgLoc).getSegments(); String oldId = segs.get(index).id; String newId = seg.id; boolean idChanged = !newId.equals(oldId); int theIndex = index; Segment tempSeg; ContainerIterator ci = new ContainerIterator(trgLoc); if (ci.hasSource()) ci.getSource().getSegments().set(theIndex, seg.clone()); if (ci.hasTarget()) { segs = ci.getTarget().getSegments(); segs.set(segs.getIndex(oldId), seg.clone()); } // update the ids if (idChanged) { ci = new ContainerIterator(trgLoc); if (ci.hasSource()) { tempSeg = ci.getSource().getSegments().get(oldId); if (tempSeg != null) tempSeg.id = newId; } if (ci.hasTarget()) { tempSeg = ci.getTarget().getSegments().get(oldId); if (tempSeg != null) tempSeg.id = newId; } } }
private void insertOrAppend(boolean append, int index, Segment srcSeg, Segment trgSeg, LocaleId trgLoc) { Segment sourceSeg = (srcSeg != null ? srcSeg : new Segment(null, new TextFragment(""))); Segment targetSeg = (trgSeg != null ? trgSeg : new Segment(null, new TextFragment(""))); String originalId = null; String insertedId = null; ContainerIterator ci = new ContainerIterator(trgLoc); if (append) { targetSeg.id = sourceSeg.id; // make sure ids match if (ci.hasSource()) ci.getSource().getSegments().append(sourceSeg); if (ci.hasTarget()) ci.getTarget().getSegments().append(targetSeg); } else { // insert // get id at insertion location originalId = getSource(trgLoc).getSegments().get(index).id; if (ci.hasSource()) insertedId = doInsert(ci.getSource(), index, null, null, sourceSeg); if (ci.hasTarget()) insertedId = doInsert(ci.getTarget(), index, originalId, insertedId, targetSeg); } }
src = getSource(trgLoc); trg = parent.getTarget(trgLoc);