From b7527dd30056d82fb6de7660b5eb1ea11e90dff2 Mon Sep 17 00:00:00 2001 From: "Rodolfo M. Raya" Date: Mon, 25 Jul 2022 14:28:14 -0300 Subject: [PATCH] Updated for release 4.21.0 --- package.json | 4 +- src/com/maxprograms/swordfish/Constants.java | 4 +- .../swordfish/am/MatchAssembler.java | 3 +- src/com/maxprograms/swordfish/am/Term.java | 52 +++++++++++++++- .../swordfish/xliff/XliffStore.java | 61 ++++++++----------- 5 files changed, 83 insertions(+), 41 deletions(-) diff --git a/package.json b/package.json index 0406bd8..d0bef32 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "swordfish", "productName": "Swordfish", - "version": "4.20.0", + "version": "4.21.0", "description": "Swordfish Translation Editor", "main": "js/App.js", "scripts": { @@ -20,7 +20,7 @@ "url": "https://github.com/rmraya/Swordfish.git" }, "devDependencies": { - "electron": "^19.0.8", + "electron": "^19.0.9", "typescript": "^4.7.4" } } diff --git a/src/com/maxprograms/swordfish/Constants.java b/src/com/maxprograms/swordfish/Constants.java index bab5384..b3b4662 100644 --- a/src/com/maxprograms/swordfish/Constants.java +++ b/src/com/maxprograms/swordfish/Constants.java @@ -19,8 +19,8 @@ private Constants() { } public static final String APPNAME = "Swordfish"; - public static final String VERSION = "4.20.0"; - public static final String BUILD = "20220714_1219"; + public static final String VERSION = "4.21.0"; + public static final String BUILD = "20220725_0908"; public static final String REASON = "reason"; public static final String STATUS = "status"; diff --git a/src/com/maxprograms/swordfish/am/MatchAssembler.java b/src/com/maxprograms/swordfish/am/MatchAssembler.java index 45c9836..217244b 100644 --- a/src/com/maxprograms/swordfish/am/MatchAssembler.java +++ b/src/com/maxprograms/swordfish/am/MatchAssembler.java @@ -178,7 +178,8 @@ public static Match assembleMatch(String textOnly, List tmMatches, ITmEng List res = glossEngine.searchTranslation(term, srcLang, tgtLang, 100, true); if (!res.isEmpty()) { Match m = res.get(0); - terms.add(new Term(m.getSource().getText(), m.getTarget().getText())); + terms.add(new Term(m.getSource().getText(), m.getTarget().getText(), srcLang, tgtLang, + glossEngine.getName())); } } } diff --git a/src/com/maxprograms/swordfish/am/Term.java b/src/com/maxprograms/swordfish/am/Term.java index 041819c..849db61 100644 --- a/src/com/maxprograms/swordfish/am/Term.java +++ b/src/com/maxprograms/swordfish/am/Term.java @@ -12,14 +12,30 @@ package com.maxprograms.swordfish.am; +import org.json.JSONObject; + public class Term implements Comparable { private String source; private String target; + private String srcLang; + private String tgtLang; + private String origin; - public Term(String source, String target) { + public Term(String source, String target, String srcLang, String tgtLang, String origin) { this.source = source; this.target = target; + this.srcLang = srcLang; + this.tgtLang = tgtLang; + this.origin = origin; + } + + public Term(JSONObject json) { + this.source = json.getString("source"); + this.target = json.getString("target"); + this.srcLang = json.getString("srcLang"); + this.tgtLang = json.getString("tgtLang"); + this.origin = json.getString("origin"); } @Override @@ -54,4 +70,38 @@ public String getSource() { public String getTarget() { return target; } + + public String getSrcLang() { + return srcLang; + } + + public void setSrcLang(String srcLang) { + this.srcLang = srcLang; + } + + public String getTgtLang() { + return tgtLang; + } + + public void setTgtLang(String tgtLang) { + this.tgtLang = tgtLang; + } + + public void setOrigin(String origin) { + this.origin = origin; + } + + public String getOrigin() { + return origin; + } + + public JSONObject toJSON() { + JSONObject json = new org.json.JSONObject(); + json.put("source", source); + json.put("target", target); + json.put("srcLang", srcLang); + json.put("tgtLang", tgtLang); + json.put("origin", origin); + return json; + } } \ No newline at end of file diff --git a/src/com/maxprograms/swordfish/xliff/XliffStore.java b/src/com/maxprograms/swordfish/xliff/XliffStore.java index 0a6b3f4..489ff12 100644 --- a/src/com/maxprograms/swordfish/xliff/XliffStore.java +++ b/src/com/maxprograms/swordfish/xliff/XliffStore.java @@ -3500,18 +3500,14 @@ public JSONArray getSegmentTerms(JSONObject json) if (!visited.containsKey(term)) { visited.put(term, ""); List res = engine.searchAll(term, srcLang, similarity, caseSensitiveSearches); - for (int j = 0; j < res.size(); j++) { - JSONArray array = parseMatches(res); - for (int h = 0; h < array.length(); h++) { - JSONObject match = array.getJSONObject(h); - Term candidate = new Term(match.getString("source"), match.getString("target")); - if (!terms.contains(candidate)) { - terms.add(candidate); - match.put("origin", glossaryName); - result.put(match); - saveTerm(json.getString("file"), json.getString("unit"), json.getString("segment"), - glossaryName, match.getString("source"), match.getString("target")); - } + List array = parseMatches(res, glossaryName); + for (int h = 0; h < array.size(); h++) { + Term candidate = array.get(h); + if (!terms.contains(candidate)) { + terms.add(candidate); + result.put(candidate.toJSON()); + saveTerm(json.getString("file"), json.getString("unit"), json.getString("segment"), + glossaryName, candidate.getSource(), candidate.getTarget()); } } } @@ -3523,20 +3519,20 @@ public JSONArray getSegmentTerms(JSONObject json) } private JSONArray sortTerms(JSONArray array) { + if (array.length() == 0) { + return array; + } JSONArray result = new JSONArray(); List terms = new Vector<>(); - Map map = new Hashtable<>(); for (int i = 0; i < array.length(); i++) { - JSONObject obj = array.getJSONObject(i); - Term term = new Term(obj.getString("source"), obj.getString("target")); - terms.add(term); - map.put(term.hashCode(), obj); + terms.add(new Term(array.getJSONObject(i))); } + // sort ignoring term length Collections.sort(terms, (Term o1, Term o2) -> o1.getSource().compareToIgnoreCase(o2.getSource())); Iterator it = terms.iterator(); while (it.hasNext()) { Term term = it.next(); - result.put(map.get(term.hashCode())); + result.put(term.toJSON()); } return result; } @@ -3575,15 +3571,12 @@ public int getProjectTerms(String glossary) visited.put(term, ""); List res = engine.searchAll(term, srcLang, similarity, caseSensitiveSearches); - for (int j = 0; j < res.size(); j++) { - JSONArray array = parseMatches(res); - for (int h = 0; h < array.length(); h++) { - JSONObject match = array.getJSONObject(h); - match.put("origin", glossaryName); - saveTerm(file, unit, segment, glossaryName, match.getString("source"), - match.getString("target")); - added = true; - } + List array = parseMatches(res, glossaryName); + for (int h = 0; h < array.size(); h++) { + Term candidate = array.get(h); + saveTerm(file, unit, segment, glossaryName, candidate.getSource(), + candidate.getTarget()); + added = true; } } } @@ -3624,22 +3617,20 @@ private void saveTerm(String file, String unit, String segment, String origin, S } } - private JSONArray parseMatches(List matches) { - JSONArray result = new JSONArray(); + private List parseMatches(List matches, String glossaryName) { + List result = new Vector<>(); for (int i = 0; i < matches.size(); i++) { Map map = new Hashtable<>(); - Element element = matches.get(i); - List tuvs = element.getChildren("tuv"); + Element tu = matches.get(i); + List tuvs = tu.getChildren("tuv"); Iterator it = tuvs.iterator(); while (it.hasNext()) { Element tuv = it.next(); map.put(tuv.getAttributeValue("xml:lang"), MemoriesHandler.pureText(tuv.getChild("seg"))); } if (map.containsKey(tgtLang)) { - JSONObject obj = new JSONObject(); - obj.put("source", map.get(srcLang)); - obj.put("target", map.get(tgtLang)); - result.put(obj); + Term term = new Term(map.get(srcLang), map.get(tgtLang), srcLang, tgtLang, glossaryName); + result.add(term); } } return result;