Skip to content

Commit

Permalink
feat: introduce matchSource match template variable
Browse files Browse the repository at this point in the history
- Change default fuzzy match pane template with matchSource
- Add method MatchesVarExpansion#expandMatchSource
- Extend NearString.MATCH_SOURCE to have TM_SUBSEG
- Update test expectations of MatchesTextAreaTest, and FindMatchesTest
- Add human-readable names of MATCH_SOURCE in Bundle.properties

Signed-off-by: Hiroshi Miura <[email protected]>
  • Loading branch information
miurahr committed Dec 17, 2024
1 parent ecb65e3 commit 53854ed
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 7 deletions.
5 changes: 5 additions & 0 deletions src/org/omegat/Bundle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2947,3 +2947,8 @@ DICTIONARY_LOAD_FILE=Loaded dictionary from '{0}': {1} ms
DICTIONARY_LOAD_ERROR=Error load dictionary from '{0}': {1}
DICTIONARY_MANAGER_ERROR_SAVE_IGNORE=Error saving ignore words"
EDITOR_CONTROLLER_EXCEPTION=bad location exception when changing case

MATCHES_COMES_FROM_TM=From TM
MATCHES_COMES_FROM_FILES=Files
MATCHES_COMES_FROM_MEMORY=From Project
MATCHES_COMES_FROM_TM_SUBSEG=Sub-segmented match
7 changes: 5 additions & 2 deletions src/org/omegat/core/matching/NearString.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,11 @@
*/
public class NearString {
public enum MATCH_SOURCE {
MEMORY, TM, FILES
};
MEMORY,
TM,
FILES,
TM_SUBSEG;
}

public enum SORT_KEY {
SCORE, SCORE_NO_STEM, ADJUSTED_SCORE
Expand Down
5 changes: 3 additions & 2 deletions src/org/omegat/core/statistics/FindMatches.java
Original file line number Diff line number Diff line change
Expand Up @@ -235,8 +235,8 @@ public List<NearString> search(String searchText, boolean requiresTranslation, b
for (ITMXEntry tmen : en.getValue().getEntries()) {
checkStopped(stop);
if (tmen.getSourceText() == null) {
// Not all TMX entries have a source; in that case there can
// be no meaningful match, so skip.
// Not all TMX entries have a source; skip it in
// the case, because of no meaningful.
continue;
}
if (requiresTranslation && tmen.getTranslationText() == null) {
Expand All @@ -249,6 +249,7 @@ public List<NearString> search(String searchText, boolean requiresTranslation, b
processEntry(null, tmen, en.getKey(), NearString.MATCH_SOURCE.TM, false, tmenPenalty);
}
}

// travel by all entries for check source file translations
for (SourceTextEntry ste : project.getAllEntries()) {
checkStopped(stop);
Expand Down
25 changes: 23 additions & 2 deletions src/org/omegat/gui/matches/MatchesVarExpansion.java
Original file line number Diff line number Diff line change
Expand Up @@ -90,19 +90,21 @@ public class MatchesVarExpansion extends VarExpansion<NearString> {
public static final String VAR_DIFF_REVERSED = "${diffReversed}";
public static final String VAR_SOURCE_LANGUAGE = "${sourceLanguage}";
public static final String VAR_TARGET_LANGUAGE = "${targetLanguage}";
public static final String VAR_MATCH_SOURCE = "${matchSource}";

private static final String[] MATCHES_VARIABLES = { VAR_ID, VAR_SOURCE_TEXT, VAR_DIFF, VAR_DIFF_REVERSED,
VAR_TARGET_TEXT, VAR_SCORE_BASE, VAR_SCORE_NOSTEM, VAR_SCORE_ADJUSTED, VAR_FILE_NAME_ONLY,
VAR_FILE_PATH, VAR_FILE_SHORT_PATH, VAR_INITIAL_CREATION_ID, VAR_INITIAL_CREATION_DATE,
VAR_CHANGED_ID, VAR_CHANGED_DATE, VAR_FUZZY_FLAG, VAR_SOURCE_LANGUAGE, VAR_TARGET_LANGUAGE };
VAR_CHANGED_ID, VAR_CHANGED_DATE, VAR_FUZZY_FLAG, VAR_SOURCE_LANGUAGE, VAR_TARGET_LANGUAGE,
VAR_MATCH_SOURCE };

public static List<String> getMatchesVariables() {
return Collections.unmodifiableList(Arrays.asList(MATCHES_VARIABLES));
}

public static final String DEFAULT_TEMPLATE = VAR_ID + ". " + VAR_FUZZY_FLAG + VAR_SOURCE_TEXT + "\n"
+ VAR_TARGET_TEXT + "\n" + "<" + VAR_SCORE_BASE + "/" + VAR_SCORE_NOSTEM + "/"
+ VAR_SCORE_ADJUSTED + "% " + VAR_FILE_PATH + ">";
+ VAR_SCORE_ADJUSTED + "%" + VAR_MATCH_SOURCE + VAR_FILE_PATH + ">";

public static final Pattern PATTERN_SINGLE_PROPERTY = Pattern.compile("@\\{(.+?)\\}");
public static final Pattern PATTERN_PROPERTY_GROUP = Pattern
Expand Down Expand Up @@ -222,6 +224,22 @@ private String getPropValue(List<TMXProp> props, String type) {
return null;
}

private String expandMatchSource(String localTemplate, NearString.MATCH_SOURCE comesFrom) {
switch(comesFrom) {
case TM:
return localTemplate.replace(VAR_MATCH_SOURCE, OStrings.getString("MATCHES_COMES_FROM_TM") + " ");
case FILES:
return localTemplate.replace(VAR_MATCH_SOURCE, OStrings.getString("MATCHES_COMES_FROM_FILES"));
case MEMORY:
return localTemplate.replace(VAR_MATCH_SOURCE, OStrings.getString( "MATCHES_COMES_FROM_MEMORY"));
case TM_SUBSEG:
return localTemplate.replace(VAR_MATCH_SOURCE, OStrings.getString(
"MATCHES_COMES_FROM_TM_SUBSEG") + " ");
default:
return localTemplate.replace(VAR_MATCH_SOURCE, "");
}
}

@Override
public String expandVariables(NearString match) {
// do not modify template directly, so that we can reuse for another
Expand Down Expand Up @@ -285,6 +303,9 @@ public String expandVariables(NearString match) {
} else {
localTemplate = localTemplate.replace(VAR_TARGET_TEXT, match.translation);
}

localTemplate = expandMatchSource(localTemplate, match.comesFrom);

return localTemplate;
}

Expand Down
3 changes: 2 additions & 1 deletion test/src/org/omegat/gui/matches/MatchesVarExpansionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
import org.omegat.core.data.ProjectProperties;
import org.omegat.core.data.SourceTextEntry;
import org.omegat.core.matching.NearString;
import org.omegat.core.matching.NearString.MATCH_SOURCE;
import org.omegat.gui.editor.IEditor;
import org.omegat.gui.editor.IEditorFilter;
import org.omegat.gui.editor.IEditorSettings;
Expand Down Expand Up @@ -208,7 +209,7 @@ public NearString getMockNearString() {
entry.changeDate = 20020523;
entry.otherProperties = testProps;
NearString.Scores scores = new NearString.Scores(20, 40, 60);
return new NearString(null, entry, null, false, scores, null, "mock testing project");
return new NearString(null, entry, MATCH_SOURCE.TM, false, scores, null, "mock testing project");
};

private void setupProject(Language sourceLanguage, Language targetLanguage) {
Expand Down

0 comments on commit 53854ed

Please sign in to comment.