Skip to content

Commit

Permalink
Improved resegmenter
Browse files Browse the repository at this point in the history
  • Loading branch information
rmraya committed Mar 31, 2023
1 parent df37f93 commit dede967
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 6 deletions.
Binary file modified lib/openxliff.jar
Binary file not shown.
2 changes: 1 addition & 1 deletion src/com/maxprograms/converters/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ private Constants() {
public static final String TOOLID = "OpenXLIFF";
public static final String TOOLNAME = "OpenXLIFF Filters";
public static final String VERSION = "3.5.0";
public static final String BUILD = "20230329_0558";
public static final String BUILD = "2023031_0958";

public static final String SUCCESS = "0";
public static final String ERROR = "1";
Expand Down
20 changes: 15 additions & 5 deletions src/com/maxprograms/xliff2/Resegmenter.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@
public class Resegmenter {

private static Segmenter segmenter;
private static boolean canResegment;
private static boolean translate;

private Resegmenter() {
// do not instantiate this class
Expand All @@ -50,6 +52,7 @@ public static List<String> run(String xliff, String srx, String srcLang, Catalog
try {
segmenter = new Segmenter(srx, srcLang, catalog);
SAXBuilder builder = new SAXBuilder();
builder.setEntityResolver(catalog);
Document doc = builder.build(xliff);
Element root = doc.getRootElement();
recurse(root);
Expand Down Expand Up @@ -84,16 +87,25 @@ private static boolean surroundedWithTags(Element e) {
}

private static void recurse(Element root) throws SAXException, IOException, ParserConfigurationException {
if ("file".equals(root.getName())) {
canResegment = "yes".equals(root.getAttributeValue("canResegment", "yes"));
translate = "yes".equals(root.getAttributeValue("translate", "yes"));
} else if (root.hasAttribute("canResegment")) {
canResegment = "yes".equals(root.getAttributeValue("canResegment", canResegment ? "yes" : "no"));
translate = "yes".equals(root.getAttributeValue("translate", translate ? "yes" : "no"));
}
if ("unit".equals(root.getName())) {
if (root.getChildren("segment").size() == 1) {
if (translate && canResegment && root.getChildren("segment").size() == 1) {
Element segment = root.getChild("segment");
String originalId = segment.getAttributeValue("id");
String unitId = root.getAttributeValue("id");
Element source = segment.getChild("source");
Element target = segment.getChild("target");
boolean isSourceCopy = target != null && source.getContent().equals(target.getContent());
boolean isEmpty = target != null && target.getContent().isEmpty();
if (target == null || isSourceCopy || isEmpty) {
root.removeAttribute("canResegment");
Element segSource = segmenter.segment(source);
int newSegments = segSource.getChildren("mrk").size();
int id = 0;
root.removeChild(segment);
List<XMLNode> content = segSource.getContent();
Expand All @@ -109,7 +121,6 @@ private static void recurse(Element root) throws SAXException, IOException, Pars
Element firstTag = e.getChildren().get(0);
if (!hasText(firstTag)) {
Element ignorable = new Element("ignorable");
ignorable.setAttribute("id", root.getAttributeValue("id") + '-' + id++);
Element ignorableSource = new Element("source");
ignorableSource.setAttribute("xml:space", "preserve");
ignorable.addContent(ignorableSource);
Expand All @@ -136,7 +147,7 @@ private static void recurse(Element root) throws SAXException, IOException, Pars
if (!hasText(e)) {
newSeg = new Element("ignorable");
}
newSeg.setAttribute("id", root.getAttributeValue("id") + '-' + id++);
newSeg.setAttribute("id", newSegments == 1 ? originalId : unitId + '-' + id++);
root.addContent(newSeg);
Element newSource = new Element("source");
newSource.setAttribute("xml:space", source.getAttributeValue("xml:space", "default"));
Expand All @@ -153,7 +164,6 @@ private static void recurse(Element root) throws SAXException, IOException, Pars
newTarget.addContent(e.getContent());
}
if (lastIgnorable != null) {
lastIgnorable.setAttribute("id", root.getAttributeValue("id") + '-' + id++);
root.addContent(lastIgnorable);
}
} else {
Expand Down

0 comments on commit dede967

Please sign in to comment.