Skip to content

Commit

Permalink
Updated for release 4.27.0
Browse files Browse the repository at this point in the history
  • Loading branch information
rmraya committed Jul 6, 2023
1 parent 5ab1eb0 commit 16d38fe
Show file tree
Hide file tree
Showing 14 changed files with 129 additions and 113 deletions.
Binary file modified jars/openxliff.jar
Binary file not shown.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "swordfish",
"productName": "Swordfish",
"version": "4.26.0",
"version": "4.27.0",
"description": "Swordfish Translation Editor",
"main": "js/Swordfish.js",
"scripts": {
Expand Down
4 changes: 2 additions & 2 deletions src/com/maxprograms/swordfish/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ private Constants() {
}

public static final String APPNAME = "Swordfish";
public static final String VERSION = "4.26.0";
public static final String BUILD = "20230630_1439";
public static final String VERSION = "4.27.0";
public static final String BUILD = "20230706_1059";

public static final String REASON = "reason";
public static final String STATUS = "status";
Expand Down
14 changes: 4 additions & 10 deletions src/com/maxprograms/swordfish/ProjectsHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Iterator;
Expand Down Expand Up @@ -489,15 +488,10 @@ private void sortProjects() throws IOException {
Project project = new Project(array.getJSONObject(i));
list.add(project);
}
Collections.sort(list, new Comparator<Project>() {

@Override
public int compare(Project o1, Project o2) {
Long long1 = Long.parseLong(o1.getId());
Long long2 = Long.parseLong(o2.getId());
return Long.compare(long2, long1);
}

Collections.sort(list, (o1, o2) -> {
Long long1 = Long.parseLong(o1.getId());
Long long2 = Long.parseLong(o2.getId());
return Long.compare(long2, long1);
});
array = new JSONArray();
for (int i = 0; i < list.size(); i++) {
Expand Down
35 changes: 6 additions & 29 deletions src/com/maxprograms/swordfish/ServicesHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
Expand Down Expand Up @@ -226,14 +225,7 @@ private JSONObject getXmlFilters(String request) {
list.add(files[i]);
}
}
Collections.sort(list, new Comparator<String>() {

@Override
public int compare(String o1, String o2) {
return o1.compareToIgnoreCase(o2);
}

});
Collections.sort(list, (o1, o2) -> o1.compareToIgnoreCase(o2));
for (int i = 0; i < list.size(); i++) {
array.put(list.get(i));
}
Expand Down Expand Up @@ -314,16 +306,14 @@ private JSONObject addFilter(String request) throws IOException {

private JSONObject getFilterData(String request)
throws SAXException, IOException, ParserConfigurationException, URISyntaxException {
JSONObject result = new JSONObject();
JSONObject json = new JSONObject(request);
File appFolder = new File(json.getString("path"));
File xmlFiltersFolder = new File(appFolder, "xmlfilter");
File configFile = new File(xmlFiltersFolder, json.getString("file"));
SAXBuilder builder = new SAXBuilder();
builder.setEntityResolver(new Catalog(TmsServer.getCatalogFile()));
Document doc = builder.build(configFile);
result = toJSON(doc.getRootElement());
return result;
return toJSON(doc.getRootElement());
}

private JSONObject saveElement(String request)
Expand Down Expand Up @@ -381,14 +371,7 @@ private JSONObject saveElement(String request)
}
tags.add(tag);
}
Collections.sort(tags, new Comparator<Element>() {

@Override
public int compare(Element o1, Element o2) {
return o1.getText().compareTo(o2.getText());
}

});
Collections.sort(tags, (o1, o2) -> o1.getText().compareTo(o2.getText()));
root.setChildren(tags);
Indenter.indent(root, 0);
XMLOutputter outputter = new XMLOutputter();
Expand Down Expand Up @@ -424,14 +407,7 @@ private JSONObject removeElements(String request)
newList.add(tag);
}
}
Collections.sort(newList, new Comparator<Element>() {

@Override
public int compare(Element o1, Element o2) {
return o1.getText().compareTo(o2.getText());
}

});
Collections.sort(newList, (o1, o2) -> o1.getText().compareTo(o2.getText()));
root.setChildren(newList);
Indenter.indent(root, 0);
XMLOutputter outputter = new XMLOutputter();
Expand Down Expand Up @@ -543,7 +519,8 @@ private static JSONObject getSubjects() throws IOException {
private static JSONObject getSystemInformation() {
JSONObject result = new JSONObject();
result.put("swordfish", Constants.VERSION + " Build: " + Constants.BUILD);
result.put("openxliff", com.maxprograms.converters.Constants.VERSION + " Build: " + com.maxprograms.converters.Constants.BUILD);
result.put("openxliff",
com.maxprograms.converters.Constants.VERSION + " Build: " + com.maxprograms.converters.Constants.BUILD);
result.put("xmljava", com.maxprograms.xml.Constants.VERSION + " Build: " + com.maxprograms.xml.Constants.BUILD);
result.put("java", System.getProperty("java.version") + " Vendor: " + System.getProperty("java.vendor"));
return result;
Expand Down
15 changes: 7 additions & 8 deletions src/com/maxprograms/swordfish/TmsServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
Expand All @@ -31,13 +30,13 @@
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

import org.json.JSONException;
import org.json.JSONObject;

import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;
import com.sun.net.httpserver.HttpServer;

import org.json.JSONException;
import org.json.JSONObject;

public class TmsServer implements HttpHandler {

private static Logger logger = System.getLogger(TmsServer.class.getName());
Expand Down Expand Up @@ -204,7 +203,7 @@ public static String getCatalogFile() throws IOException, JSONException {
public static File getProjectsFolder() throws IOException, JSONException {
JSONObject json = getPreferences();
if (!json.has("projectsFolder")) {
json.put("projectsFolder",new File(getWorkFolder(), "projects").getAbsolutePath());
json.put("projectsFolder", new File(getWorkFolder(), "projects").getAbsolutePath());
writeJSON(new File(getWorkFolder(), "preferences.json"), json);
}
return new File(json.getString("projectsFolder"));
Expand All @@ -213,7 +212,7 @@ public static File getProjectsFolder() throws IOException, JSONException {
public static File getMemoriesFolder() throws IOException, JSONException {
JSONObject json = getPreferences();
if (!json.has("memoriesFolder")) {
json.put("memoriesFolder",new File(getWorkFolder(), "memories").getAbsolutePath());
json.put("memoriesFolder", new File(getWorkFolder(), "memories").getAbsolutePath());
writeJSON(new File(getWorkFolder(), "preferences.json"), json);
}
return new File(json.getString("memoriesFolder"));
Expand All @@ -222,7 +221,7 @@ public static File getMemoriesFolder() throws IOException, JSONException {
public static File getGlossariesFolder() throws IOException, JSONException {
JSONObject json = getPreferences();
if (!json.has("glossariesFolder")) {
json.put("glossariesFolder",new File(getWorkFolder(), "glossaries").getAbsolutePath());
json.put("glossariesFolder", new File(getWorkFolder(), "glossaries").getAbsolutePath());
writeJSON(new File(getWorkFolder(), "preferences.json"), json);
}
return new File(json.getString("glossariesFolder"));
Expand All @@ -249,7 +248,7 @@ public static JSONObject readJSON(File json) throws IOException, JSONException {
return new JSONObject(builder.toString());
}

public static synchronized void writeJSON(File jsonFile, JSONObject json) throws FileNotFoundException, IOException, JSONException {
public static synchronized void writeJSON(File jsonFile, JSONObject json) throws IOException, JSONException {
try (FileOutputStream out = new FileOutputStream(jsonFile)) {
out.write(json.toString(2).getBytes(StandardCharsets.UTF_8));
}
Expand Down
13 changes: 7 additions & 6 deletions src/com/maxprograms/swordfish/mt/MT.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
package com.maxprograms.swordfish.mt;

import java.io.IOException;
import java.text.MessageFormat;
import java.util.List;

import org.json.JSONException;
Expand Down Expand Up @@ -145,10 +146,10 @@ private void loadDefaults() throws IOException, JSONException {
deeplTgtLang = deepl.getString("tgtLang");

if (json.has("chatGpt")) {
JSONObject chatGpt = json.getJSONObject("chatGpt");
chatGptEnabled = chatGpt.getBoolean("enabled");
chatGptKey = chatGpt.getString("apiKey");
switch (chatGpt.getString("model")) {
JSONObject chatGptOption = json.getJSONObject("chatGpt");
chatGptEnabled = chatGptOption.getBoolean("enabled");
chatGptKey = chatGptOption.getString("apiKey");
switch (chatGptOption.getString("model")) {
case "Davinci":
model = ChatGptTranslator.DAVINCI;
break;
Expand All @@ -162,9 +163,9 @@ private void loadDefaults() throws IOException, JSONException {
model = ChatGptTranslator.ADA;
break;
default:
throw new JSONException("Invalid ChatGPT model: " + chatGpt.getString("model"));
MessageFormat mf = new MessageFormat("Invalid ChatGPT model: {0}");
throw new JSONException(mf.format(new String[] { chatGptOption.getString("model") }));
}
;
} else {
chatGptEnabled = false;
}
Expand Down
33 changes: 33 additions & 0 deletions src/com/maxprograms/swordfish/tm/TMUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -108,10 +108,43 @@ public static Element buildTuv(String lang, String seg)
tuv.setAttribute("xml:lang", lang);
SAXBuilder builder = new SAXBuilder();
Element e = builder.build(new ByteArrayInputStream(seg.getBytes(StandardCharsets.UTF_8))).getRootElement();
checkAttributes(e);
tuv.addContent(e);
return tuv;
}

private static void checkAttributes(Element e) {
if (e.hasAttribute("x")) {
String x = e.getAttributeValue("x");
if (!isNumber(x)) {
e.setAttribute("x", "" + x.hashCode());
}
}
if (e.hasAttribute("i")) {
String i = e.getAttributeValue("i");
if (!isNumber(i)) {
e.setAttribute("i", "" + i.hashCode());
}
}
List<XMLNode> l = e.getContent();
Iterator<XMLNode> i = l.iterator();
while (i.hasNext()) {
XMLNode o = i.next();
if (o.getNodeType() == XMLNode.ELEMENT_NODE) {
checkAttributes((Element) o);
}
}
}

private static boolean isNumber(String s) {
try {
Double.parseDouble(s);
return true;
} catch (NumberFormatException e) {
return false;
}
}

public static String creationDate() {
Calendar calendar = Calendar.getInstance(Locale.US);
String sec = (calendar.get(Calendar.SECOND) < 10 ? "0" : "") + calendar.get(Calendar.SECOND);
Expand Down
80 changes: 44 additions & 36 deletions src/com/maxprograms/swordfish/xliff/XliffStore.java
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ public class XliffStore {

public static final int THRESHOLD = 60;
public static final int MAXTERMLENGTH = 5;
public static final int BATCHSIZE = 100;

public static final String SVG_BLANK = "<svg xmlns='http://www.w3.org/2000/svg' height='24' viewBox='0 0 24 24' width='24'></svg>";
public static final String SVG_UNTRANSLATED = "<svg xmlns:svg='http://www.w3.org/2000/svg' height='24' viewBox='0 0 24 24' width='24' version='1.1'><path d='M 19,5 V 19 H 5 V 5 H 19 M 19,3 H 5 C 3.9,3 3,3.9 3,5 v 14 c 0,1.1 0.9,2 2,2 h 14 c 1.1,0 2,-0.9 2,-2 V 5 C 21,3.9 20.1,3 19,3 Z' /></svg>";
Expand Down Expand Up @@ -2403,47 +2404,54 @@ public int tmTranslateAll(String memory, int penalization, Map<String, JSONObjec
total = rs.getInt(1);
}
}
sql = "SELECT file, unitId, segId, source, sourceText, target FROM segments WHERE type = 'S' AND state <> 'final'";
int count = 0;
if (total == 0) {
return 0;
}
int processed = 0;
try (ResultSet rs = stmt.executeQuery(sql)) {
JSONObject params = new JSONObject();
params.put("srcLang", srcLang);
params.put("tgtLang", tgtLang);
JSONArray array = new JSONArray();
while (rs.next()) {
String file = rs.getString(1);
String unit = rs.getString(2);
String segment = rs.getString(3);
String pure = TMUtils.getString(rs.getNCharacterStream(5));
JSONObject json = new JSONObject();
json.put("file", file);
json.put("unit", unit);
json.put("segment", segment);
json.put("pure", pure);
array.put(json);
processed++;
if (processed % 20 == 0) {
int percentage = Math.round(processed * 100f / total);
if (percentage == 100) {
percentage = 99;
int offset = 0;
do {
processed += translateBatch(offset, engine, memoryName, penalization);
int percentage = Math.round(offset * 100f / total);
if (percentage == 100) {
percentage = 99;
}
processes.get(processId).put("percentage", percentage);
offset += BATCHSIZE;
} while (offset < total);
MemoriesHandler.close(memory);
return processed;
}

private synchronized int translateBatch(int offset, ITmEngine engine, String memoryName, int penalization)
throws IOException, SQLException, SAXException, ParserConfigurationException {
String sql = "SELECT file, unitId, segId, source, sourceText, target FROM segments WHERE type = 'S' AND state <> 'final' OFFSET ?";
JSONObject params = new JSONObject();
JSONArray array = new JSONArray();
try (PreparedStatement prepared = conn.prepareStatement(sql)) {
prepared.setInt(1, offset);
try (ResultSet rs = prepared.executeQuery()) {
params.put("srcLang", srcLang);
params.put("tgtLang", tgtLang);
while (rs.next()) {
String file = rs.getString(1);
String unit = rs.getString(2);
String segment = rs.getString(3);
String pure = TMUtils.getString(rs.getNCharacterStream(5));
JSONObject json = new JSONObject();
json.put("file", file);
json.put("unit", unit);
json.put("segment", segment);
json.put("pure", pure);
array.put(json);
if (array.length() == BATCHSIZE) {
break;
}
processes.get(processId).put("percentage", percentage);
}
if (array.length() == 250) {
params.put("segments", array);
JSONArray translations = engine.batchTranslate(params);
count += storeMatches(translations, memoryName, penalization);
array = new JSONArray();
}
params.put("segments", array);
}
params.put("segments", array);
JSONArray translations = engine.batchTranslate(params);
count += storeMatches(translations, memoryName, penalization);
processes.get(processId).put("percentage", 100);
}
MemoriesHandler.close(memory);
return count;
JSONArray translations = engine.batchTranslate(params);
return storeMatches(translations, memoryName, penalization);
}

private int storeMatches(JSONArray translations, String memoryName, int penalization)
Expand Down
2 changes: 1 addition & 1 deletion src/com/maxprograms/swordfish/xliff/XliffUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ private static List<XMLNode> toTmx(Element element, Map<String, String> tags) {
if ("ph".equals(e.getName())) {
Element ph = new Element("ph");
String id = e.getAttributeValue("id");
ph.setAttribute("x", id);
ph.setAttribute("x", "" + id.hashCode());
if (tags.containsKey(id)) {
ph.setText(tags.get(id));
}
Expand Down
Loading

0 comments on commit 16d38fe

Please sign in to comment.