Search in sources :

Example 16 with TMXEntry

use of org.omegat.core.data.TMXEntry in project omegat by omegat-org.

the class MainWindowMenuHandler method editExportSelectionMenuItemActionPerformed.

public void editExportSelectionMenuItemActionPerformed() {
    if (!Core.getProject().isProjectLoaded()) {
        return;
    }
    String selection = Core.getEditor().getSelectedText();
    if (selection == null) {
        SourceTextEntry ste = Core.getEditor().getCurrentEntry();
        TMXEntry te = Core.getProject().getTranslationInfo(ste);
        if (te.isTranslated()) {
            selection = te.translation;
        } else {
            selection = ste.getSrcText();
        }
    }
    SegmentExportImport.exportCurrentSelection(selection);
}
Also used : SourceTextEntry(org.omegat.core.data.SourceTextEntry) NearString(org.omegat.core.matching.NearString) TMXEntry(org.omegat.core.data.TMXEntry)

Example 17 with TMXEntry

use of org.omegat.core.data.TMXEntry in project omegat by omegat-org.

the class TagIssue method doFix.

/**
 * Fix all errors in a given report, and commit the changed translation to
 * the project. Checks to make sure the translation has not been changed in
 * the meantime.
 *
 * @param report
 *            The report to fix
 * @return Whether or not the fix succeeded
 */
private static boolean doFix(ErrorReport report, String fixed) {
    // Make sure the translation hasn't changed in the editor.
    TMXEntry prevTrans = Core.getProject().getTranslationInfo(report.ste);
    if (!report.translation.equals(prevTrans.translation)) {
        return false;
    }
    // Put modified translation back into project.
    Core.getEditor().gotoEntry(report.entryNum);
    Core.getEditor().replaceEditTextAndMark(fixed);
    return true;
}
Also used : TMXEntry(org.omegat.core.data.TMXEntry)

Example 18 with TMXEntry

use of org.omegat.core.data.TMXEntry in project omegat by omegat-org.

the class Searcher method searchMemory.

private void searchMemory() throws SearchLimitReachedException {
    // search the Memory, if requested
    if (searchExpression.memory) {
        // search through all project entries
        List<SourceTextEntry> allEntries = m_project.getAllEntries();
        for (int i = 0; i < allEntries.size(); i++) {
            // stop searching if the max. nr of hits has been reached
            if (m_numFinds >= searchExpression.numberOfResults) {
                throw new SearchLimitReachedException();
            }
            // get the source and translation of the next entry
            SourceTextEntry ste = allEntries.get(i);
            TMXEntry te = m_project.getTranslationInfo(ste);
            checkEntry(ste.getSrcText(), te.translation, te.note, ste.getRawProperties(), te, i, null);
            checkStop.checkInterrupted();
        }
        // search in orphaned
        if (!searchExpression.excludeOrphans) {
            m_project.iterateByDefaultTranslations(new IProject.DefaultTranslationsIterator() {

                final String file = OStrings.getString("CT_ORPHAN_STRINGS");

                public void iterate(String source, TMXEntry en) {
                    // stop searching if the max. nr of hits has been reached
                    if (m_numFinds >= searchExpression.numberOfResults) {
                        return;
                    }
                    checkStop.checkInterrupted();
                    if (m_project.isOrphaned(source)) {
                        checkEntry(en.source, en.translation, en.note, null, en, ENTRY_ORIGIN_ORPHAN, file);
                    }
                }
            });
            m_project.iterateByMultipleTranslations(new IProject.MultipleTranslationsIterator() {

                final String file = OStrings.getString("CT_ORPHAN_STRINGS");

                public void iterate(EntryKey source, TMXEntry en) {
                    // reached
                    if (m_numFinds >= searchExpression.numberOfResults) {
                        return;
                    }
                    checkStop.checkInterrupted();
                    if (m_project.isOrphaned(source)) {
                        checkEntry(en.source, en.translation, en.note, null, en, ENTRY_ORIGIN_ORPHAN, file);
                    }
                }
            });
        }
    }
}
Also used : EntryKey(org.omegat.core.data.EntryKey) SourceTextEntry(org.omegat.core.data.SourceTextEntry) PrepareTMXEntry(org.omegat.core.data.PrepareTMXEntry) ITMXEntry(org.omegat.core.data.ITMXEntry) TMXEntry(org.omegat.core.data.TMXEntry) IProject(org.omegat.core.data.IProject)

Example 19 with TMXEntry

use of org.omegat.core.data.TMXEntry in project omegat by omegat-org.

the class FindMatches method search.

public List<NearString> search(final String searchText, final boolean requiresTranslation, final boolean fillSimilarityData, final IStopped stop) throws StoppedException {
    result = new ArrayList<>(OConsts.MAX_NEAR_STRINGS + 1);
    srcText = searchText;
    removedText = "";
    // of the translatable text
    if (removePattern != null) {
        StringBuilder removedBuffer = new StringBuilder();
        Matcher removeMatcher = removePattern.matcher(srcText);
        while (removeMatcher.find()) {
            removedBuffer.append(removeMatcher.group());
        }
        srcText = removeMatcher.replaceAll("");
        removedText = removedBuffer.toString();
    }
    // get tokens for original string
    strTokensStem = tokenizeStem(srcText);
    strTokensNoStem = tokenizeNoStem(srcText);
    strTokensAll = tokenizeAll(srcText);
    // travel by project entries, including orphaned
    if (project.getProjectProperties().isSupportDefaultTranslations()) {
        project.iterateByDefaultTranslations(new DefaultTranslationsIterator() {

            public void iterate(String source, TMXEntry trans) {
                checkStopped(stop);
                if (!searchExactlyTheSame && source.equals(searchText)) {
                    // skip original==original entry comparison
                    return;
                }
                if (requiresTranslation && trans.translation == null) {
                    return;
                }
                String fileName = project.isOrphaned(source) ? ORPHANED_FILE_NAME : null;
                processEntry(null, source, trans.translation, NearString.MATCH_SOURCE.MEMORY, false, 0, fileName, trans.creator, trans.creationDate, trans.changer, trans.changeDate, null);
            }
        });
    }
    project.iterateByMultipleTranslations(new MultipleTranslationsIterator() {

        public void iterate(EntryKey source, TMXEntry trans) {
            checkStopped(stop);
            if (!searchExactlyTheSame && source.sourceText.equals(searchText)) {
                // skip original==original entry comparison
                return;
            }
            if (requiresTranslation && trans.translation == null) {
                return;
            }
            String fileName = project.isOrphaned(source) ? ORPHANED_FILE_NAME : null;
            processEntry(source, source.sourceText, trans.translation, NearString.MATCH_SOURCE.MEMORY, false, 0, fileName, trans.creator, trans.creationDate, trans.changer, trans.changeDate, null);
        }
    });
    /*
          Penalty applied for fuzzy matches in another language (if no match in the
          target language was found).
         */
    int foreignPenalty = Preferences.getPreferenceDefault(Preferences.PENALTY_FOR_FOREIGN_MATCHES, Preferences.PENALTY_FOR_FOREIGN_MATCHES_DEFAULT);
    // travel by translation memories
    for (Map.Entry<String, ExternalTMX> en : project.getTransMemories().entrySet()) {
        int penalty = 0;
        Matcher matcher = SEARCH_FOR_PENALTY.matcher(en.getKey());
        if (matcher.find()) {
            penalty = Integer.parseInt(matcher.group(1));
        }
        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.
                continue;
            }
            if (requiresTranslation && tmen.getTranslationText() == null) {
                continue;
            }
            int tmenPenalty = penalty;
            if (tmen.hasPropValue(ExternalTMFactory.TMXLoader.PROP_FOREIGN_MATCH, "true")) {
                tmenPenalty += foreignPenalty;
            }
            processEntry(null, tmen.getSourceText(), tmen.getTranslationText(), NearString.MATCH_SOURCE.TM, false, tmenPenalty, en.getKey(), tmen.getCreator(), tmen.getCreationDate(), tmen.getChanger(), tmen.getChangeDate(), tmen.getProperties());
        }
    }
    // travel by all entries for check source file translations
    for (SourceTextEntry ste : project.getAllEntries()) {
        checkStopped(stop);
        if (ste.getSourceTranslation() != null) {
            processEntry(ste.getKey(), ste.getSrcText(), ste.getSourceTranslation(), NearString.MATCH_SOURCE.MEMORY, ste.isSourceTranslationFuzzy(), 0, ste.getKey().file, "", 0, "", 0, null);
        }
    }
    if (separateSegmentMatcher != null) {
        // split paragraph even when segmentation disabled, then find matches for every segment
        List<StringBuilder> spaces = new ArrayList<StringBuilder>();
        List<Rule> brules = new ArrayList<Rule>();
        Language sourceLang = project.getProjectProperties().getSourceLanguage();
        Language targetLang = project.getProjectProperties().getTargetLanguage();
        List<String> segments = Core.getSegmenter().segment(sourceLang, srcText, spaces, brules);
        if (segments.size() > 1) {
            List<String> fsrc = new ArrayList<String>(segments.size());
            List<String> ftrans = new ArrayList<String>(segments.size());
            // multiple segments
            for (short i = 0; i < segments.size(); i++) {
                String onesrc = segments.get(i);
                // find match for separate segment
                List<NearString> segmentMatch = separateSegmentMatcher.search(onesrc, requiresTranslation, false, stop);
                if (!segmentMatch.isEmpty() && segmentMatch.get(0).scores[0].score >= SUBSEGMENT_MATCH_THRESHOLD) {
                    fsrc.add(segmentMatch.get(0).source);
                    ftrans.add(segmentMatch.get(0).translation);
                } else {
                    fsrc.add("");
                    ftrans.add("");
                }
            }
            // glue found sources
            String foundSrc = Core.getSegmenter().glue(sourceLang, sourceLang, fsrc, spaces, brules);
            // glue found translations
            String foundTrans = Core.getSegmenter().glue(sourceLang, targetLang, ftrans, spaces, brules);
            processEntry(null, foundSrc, foundTrans, NearString.MATCH_SOURCE.TM, false, 0, "", "", 0, "", 0, null);
        }
    }
    if (fillSimilarityData) {
        // fill similarity data only for result
        for (NearString near : result) {
            // fix for bug 1586397
            byte[] similarityData = FuzzyMatcher.buildSimilarityData(strTokensAll, tokenizeAll(near.source));
            near.attr = similarityData;
        }
    }
    return result;
}
Also used : EntryKey(org.omegat.core.data.EntryKey) Matcher(java.util.regex.Matcher) FuzzyMatcher(org.omegat.core.matching.FuzzyMatcher) ArrayList(java.util.ArrayList) NearString(org.omegat.core.matching.NearString) NearString(org.omegat.core.matching.NearString) ITMXEntry(org.omegat.core.data.ITMXEntry) Language(org.omegat.util.Language) SourceTextEntry(org.omegat.core.data.SourceTextEntry) DefaultTranslationsIterator(org.omegat.core.data.IProject.DefaultTranslationsIterator) ExternalTMX(org.omegat.core.data.ExternalTMX) Rule(org.omegat.core.segmentation.Rule) HashMap(java.util.HashMap) Map(java.util.Map) ITMXEntry(org.omegat.core.data.ITMXEntry) TMXEntry(org.omegat.core.data.TMXEntry) MultipleTranslationsIterator(org.omegat.core.data.IProject.MultipleTranslationsIterator)

Example 20 with TMXEntry

use of org.omegat.core.data.TMXEntry in project omegat by omegat-org.

the class Main method runConsoleAlign.

public static int runConsoleAlign() throws Exception {
    Log.log("Console alignment mode");
    Log.log("");
    if (projectLocation == null) {
        System.out.println(OStrings.getString("PP_ERROR_UNABLE_TO_READ_PROJECT_FILE"));
        return 1;
    }
    String dir = PARAMS.get(CLIParameters.ALIGNDIR);
    if (dir == null) {
        System.out.println(OStrings.getString("CONSOLE_TRANSLATED_FILES_LOC_UNDEFINED"));
        return 1;
    }
    System.out.println(OStrings.getString("CONSOLE_INITIALIZING"));
    Core.initializeConsole(PARAMS);
    RealProject p = selectProjectConsoleMode(true);
    validateTagsConsoleMode();
    System.out.println(StringUtil.format(OStrings.getString("CONSOLE_ALIGN_AGAINST"), dir));
    Map<String, TMXEntry> data = p.align(p.getProjectProperties(), new File(dir));
    Map<String, PrepareTMXEntry> result = new TreeMap<>();
    for (Map.Entry<String, TMXEntry> en : data.entrySet()) {
        result.put(en.getKey(), new PrepareTMXEntry(en.getValue()));
    }
    String tmxFile = p.getProjectProperties().getProjectInternal() + "align.tmx";
    TMXWriter.buildTMXFile(tmxFile, false, false, p.getProjectProperties(), result);
    p.closeProject();
    System.out.println(OStrings.getString("CONSOLE_FINISHED"));
    return 0;
}
Also used : RealProject(org.omegat.core.data.RealProject) TreeMap(java.util.TreeMap) File(java.io.File) PrepareTMXEntry(org.omegat.core.data.PrepareTMXEntry) Map(java.util.Map) HashMap(java.util.HashMap) TreeMap(java.util.TreeMap) TMXEntry(org.omegat.core.data.TMXEntry) PrepareTMXEntry(org.omegat.core.data.PrepareTMXEntry)

Aggregations

TMXEntry (org.omegat.core.data.TMXEntry)20 SourceTextEntry (org.omegat.core.data.SourceTextEntry)13 PrepareTMXEntry (org.omegat.core.data.PrepareTMXEntry)10 Map (java.util.Map)7 HashMap (java.util.HashMap)5 Point (java.awt.Point)4 EntryKey (org.omegat.core.data.EntryKey)4 IProject (org.omegat.core.data.IProject)4 Language (org.omegat.util.Language)4 ArrayList (java.util.ArrayList)3 NearString (org.omegat.core.matching.NearString)3 File (java.io.File)2 IOException (java.io.IOException)2 HashSet (java.util.HashSet)2 ExternalTMX (org.omegat.core.data.ExternalTMX)2 FileInfo (org.omegat.core.data.IProject.FileInfo)2 ITMXEntry (org.omegat.core.data.ITMXEntry)2 ProjectTMX (org.omegat.core.data.ProjectTMX)2 ProtectedPart (org.omegat.core.data.ProtectedPart)2 SearchMatch (org.omegat.core.search.SearchMatch)2