Search in sources :

Example 1 with PrepareTMXEntry

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

the class Main method runCreatePseudoTranslateTMX.

/**
 * Execute in console mode for translate.
 */
protected static int runCreatePseudoTranslateTMX() throws Exception {
    Log.log("Console pseudo-translate mode");
    Log.log("");
    System.out.println(OStrings.getString("CONSOLE_INITIALIZING"));
    Core.initializeConsole(PARAMS);
    RealProject p = selectProjectConsoleMode(true);
    validateTagsConsoleMode();
    System.out.println(OStrings.getString("CONSOLE_CREATE_PSEUDOTMX"));
    ProjectProperties config = p.getProjectProperties();
    List<SourceTextEntry> entries = p.getAllEntries();
    String pseudoTranslateTMXFilename = PARAMS.get(CLIParameters.PSEUDOTRANSLATETMX);
    PSEUDO_TRANSLATE_TYPE pseudoTranslateType = PSEUDO_TRANSLATE_TYPE.parse(PARAMS.get(CLIParameters.PSEUDOTRANSLATETYPE));
    String fname;
    if (!StringUtil.isEmpty(pseudoTranslateTMXFilename)) {
        if (!pseudoTranslateTMXFilename.endsWith(OConsts.TMX_EXTENSION)) {
            fname = pseudoTranslateTMXFilename + "." + OConsts.TMX_EXTENSION;
        } else {
            fname = pseudoTranslateTMXFilename;
        }
    } else {
        fname = "";
    }
    // prepare tmx
    Map<String, PrepareTMXEntry> data = new HashMap<>();
    for (SourceTextEntry ste : entries) {
        PrepareTMXEntry entry = new PrepareTMXEntry();
        entry.source = ste.getSrcText();
        switch(pseudoTranslateType) {
            case EQUAL:
                entry.translation = ste.getSrcText();
                break;
            case EMPTY:
                entry.translation = "";
                break;
        }
        data.put(ste.getSrcText(), entry);
    }
    try {
        // Write OmegaT-project-compatible TMX:
        TMXWriter.buildTMXFile(fname, false, false, config, data);
    } catch (IOException e) {
        Log.logErrorRB("CT_ERROR_CREATING_TMX");
        Log.log(e);
        throw new IOException(OStrings.getString("CT_ERROR_CREATING_TMX") + "\n" + e.getMessage());
    }
    p.closeProject();
    System.out.println(OStrings.getString("CONSOLE_FINISHED"));
    return 0;
}
Also used : SourceTextEntry(org.omegat.core.data.SourceTextEntry) HashMap(java.util.HashMap) RealProject(org.omegat.core.data.RealProject) ProjectProperties(org.omegat.core.data.ProjectProperties) IOException(java.io.IOException) PSEUDO_TRANSLATE_TYPE(org.omegat.CLIParameters.PSEUDO_TRANSLATE_TYPE) PrepareTMXEntry(org.omegat.core.data.PrepareTMXEntry)

Example 2 with PrepareTMXEntry

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

the class Searcher method searchEntries.

/**
 * Loops over collection of TMXEntries and checks every entry.
 * If max nr of hits have been reached or serach has been stopped,
 * the function stops and returns false. Else it finishes and returns true;
 *
 * @param tmEn collection of TMX Entries to check.
 * @param tmxID identifier of the TMX. E.g. the filename or language code
 * @return true when finished and all entries checked,
 *         false when search has stopped before all entries have been checked.
 */
private boolean searchEntries(Collection<PrepareTMXEntry> tmEn, final String tmxID) {
    for (PrepareTMXEntry tm : tmEn) {
        // stop searching if the max. nr of hits has been reached
        if (m_numFinds >= expression.numberOfResults) {
            return false;
        }
        // for alternative translations:
        // - it is not feasible to get the sourcetextentry that matches the tm.source, so we cannot get the entryNum
        // and real translation
        // - although the 'translation' is used as 'source', we search it as translation, else we cannot show to
        // which real source it belongs
        checkEntry(tm.source, tm.translation, tm.note, null, null, ENTRY_ORIGIN_TRANSLATION_MEMORY, tmxID);
        checkStop.checkInterrupted();
    }
    return true;
}
Also used : PrepareTMXEntry(org.omegat.core.data.PrepareTMXEntry)

Example 3 with PrepareTMXEntry

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

the class EditorController method commitAndDeactivate.

void commitAndDeactivate(ForceTranslation forceTranslation, String newTrans) {
    UIThreadsUtil.mustBeSwingThread();
    Document3 doc = editor.getOmDocument();
    doc.stopEditMode();
    // segment was active
    SegmentBuilder sb = m_docSegList[displayedEntryIndex];
    SourceTextEntry entry = sb.ste;
    TMXEntry oldTE = Core.getProject().getTranslationInfo(entry);
    PrepareTMXEntry newen = new PrepareTMXEntry();
    newen.source = sb.ste.getSrcText();
    newen.note = Core.getNotes().getNoteText();
    if (forceTranslation != null) {
        // there is force translation
        switch(forceTranslation) {
            case UNTRANSLATED:
                newen.translation = null;
                break;
            case EMPTY:
                newen.translation = "";
                break;
            case EQUALS_TO_SOURCE:
                newen.translation = newen.source;
                break;
        }
    } else {
        // translation from editor
        if (newTrans.isEmpty()) {
            // empty translation
            if (oldTE.isTranslated() && "".equals(oldTE.translation)) {
                // It's an empty translation which should remain empty
                newen.translation = "";
            } else {
                // will be untranslated
                newen.translation = null;
            }
        } else if (newTrans.equals(newen.source)) {
            // equals to source
            if (Preferences.isPreference(Preferences.ALLOW_TRANS_EQUAL_TO_SRC)) {
                // translation can be equals to source
                newen.translation = newTrans;
            } else {
                // translation can't be equals to source
                if (oldTE.source.equals(oldTE.translation)) {
                    // but it was equals to source before
                    newen.translation = oldTE.translation;
                } else {
                    // set untranslated
                    newen.translation = null;
                }
            }
        } else {
            // new translation is not empty and not equals to source - just change
            newen.translation = newTrans;
        }
    }
    boolean defaultTranslation = sb.isDefaultTranslation();
    boolean isNewAltTrans = !defaultTranslation && oldTE.defaultTranslation;
    boolean translationChanged = !Objects.equals(oldTE.translation, newen.translation);
    boolean noteChanged = !StringUtil.nvl(oldTE.note, "").equals(StringUtil.nvl(newen.note, ""));
    if (!isNewAltTrans && !translationChanged && noteChanged) {
        // Only note was changed, and we are not making a new alt translation.
        Core.getProject().setNote(entry, oldTE, newen.note);
    } else if (translationChanged || noteChanged) {
        while (true) {
            // iterate before optimistic locking will be resolved
            try {
                Core.getProject().setTranslation(entry, newen, defaultTranslation, null, previousTranslations);
                break;
            } catch (OptimisticLockingFail ex) {
                String result = new ConflictDialogController().show(ex.getOldTranslationText(), ex.getNewTranslationText(), newen.translation);
                if (result == newen.translation) {
                    // next iteration
                    previousTranslations = ex.getPrevious();
                } else {
                    // use remote - don't save user's translation
                    break;
                }
            }
        }
    }
    m_docSegList[displayedEntryIndex].createSegmentElement(false, Core.getProject().getTranslationInfo(m_docSegList[displayedEntryIndex].ste));
    // find all identical sources and redraw them
    for (int i = 0; i < m_docSegList.length; i++) {
        if (i == displayedEntryIndex) {
            // current entry, skip
            continue;
        }
        SegmentBuilder builder = m_docSegList[i];
        if (!builder.hasBeenCreated()) {
            // Skip because segment has not been drawn yet
            continue;
        }
        if (builder.ste.getSrcText().equals(entry.getSrcText())) {
            // the same source text - need to update
            builder.createSegmentElement(false, Core.getProject().getTranslationInfo(builder.ste));
            // then add new marks
            markerController.reprocessImmediately(builder);
        }
    }
    Core.getNotes().clear();
    // then add new marks
    markerController.reprocessImmediately(m_docSegList[displayedEntryIndex]);
    editor.undoManager.reset();
    // validate tags if required
    if (entry != null && Preferences.isPreference(Preferences.TAG_VALIDATE_ON_LEAVE)) {
        String file = getCurrentFile();
        new SwingWorker<Boolean, Void>() {

            protected Boolean doInBackground() throws Exception {
                return Core.getTagValidation().checkInvalidTags(entry);
            }

            @Override
            protected void done() {
                try {
                    if (!get()) {
                        Core.getIssues().showForFiles(Pattern.quote(file), entry.entryNum());
                    }
                } catch (InterruptedException | ExecutionException e) {
                    LOGGER.log(Level.SEVERE, "Exception when validating tags on leave", e);
                }
            }
        }.execute();
    }
    // team sync for save thread
    if (Core.getProject().isTeamSyncPrepared()) {
        try {
            Core.executeExclusively(false, Core.getProject()::teamSync);
        } catch (InterruptedException ex) {
        } catch (TimeoutException ex) {
        }
    }
}
Also used : Point(java.awt.Point) TimeoutException(java.util.concurrent.TimeoutException) BadLocationException(javax.swing.text.BadLocationException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) ConflictDialogController(org.omegat.gui.dialogs.ConflictDialogController) SourceTextEntry(org.omegat.core.data.SourceTextEntry) PrepareTMXEntry(org.omegat.core.data.PrepareTMXEntry) OptimisticLockingFail(org.omegat.core.data.IProject.OptimisticLockingFail) TMXEntry(org.omegat.core.data.TMXEntry) PrepareTMXEntry(org.omegat.core.data.PrepareTMXEntry) TimeoutException(java.util.concurrent.TimeoutException)

Example 4 with PrepareTMXEntry

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

the class FalseFriendsTest method setUp.

@Before
public final void setUp() {
    final ProjectProperties props = new ProjectProperties() {

        public Language getSourceLanguage() {
            return new Language("en");
        }

        public Language getTargetLanguage() {
            return new Language("pl");
        }
    };
    Core.setProject(new IProject() {

        public void setTranslation(SourceTextEntry entry, PrepareTMXEntry trans, boolean defaultTranslation, TMXEntry.ExternalLinked externalLinked) {
        }

        public void setTranslation(SourceTextEntry entry, PrepareTMXEntry trans, boolean defaultTranslation, ExternalLinked externalLinked, AllTranslations previousTranslations) throws OptimisticLockingFail {
        }

        public void setNote(SourceTextEntry entry, TMXEntry oldTrans, String note) {
        }

        public void saveProjectProperties() throws Exception {
        }

        public void saveProject(boolean doTeamSync) {
        }

        public void iterateByMultipleTranslations(MultipleTranslationsIterator it) {
        }

        public void iterateByDefaultTranslations(DefaultTranslationsIterator it) {
        }

        public boolean isProjectModified() {
            return false;
        }

        public boolean isProjectLoaded() {
            return true;
        }

        public boolean isOrphaned(EntryKey entry) {
            return false;
        }

        public boolean isOrphaned(String source) {
            return false;
        }

        public TMXEntry getTranslationInfo(SourceTextEntry ste) {
            return null;
        }

        public AllTranslations getAllTranslations(SourceTextEntry ste) {
            return null;
        }

        public Map<String, ExternalTMX> getTransMemories() {
            return null;
        }

        public ITokenizer getTargetTokenizer() {
            return null;
        }

        public StatisticsInfo getStatistics() {
            return null;
        }

        public ITokenizer getSourceTokenizer() {
            return null;
        }

        public ProjectProperties getProjectProperties() {
            return props;
        }

        public List<FileInfo> getProjectFiles() {
            return null;
        }

        public Map<Language, ProjectTMX> getOtherTargetLanguageTMs() {
            return null;
        }

        public List<SourceTextEntry> getAllEntries() {
            return null;
        }

        public void compileProject(String sourcePattern) throws Exception {
        }

        public void closeProject() {
        }

        public List<String> getSourceFilesOrder() {
            return null;
        }

        public void setSourceFilesOrder(List<String> filesList) {
        }

        @Override
        public String getTargetPathForSourceFile(String sourceFile) {
            return null;
        }

        @Override
        public boolean isTeamSyncPrepared() {
            return false;
        }

        @Override
        public void teamSync() {
        }

        @Override
        public void teamSyncPrepare() throws Exception {
        }

        @Override
        public boolean isRemoteProject() {
            return false;
        }

        @Override
        public void commitSourceFiles() throws Exception {
        }

        @Override
        public void compileProjectAndCommit(String sourcePattern, boolean doPostProcessing, boolean commitTargetFiles) throws Exception {
        }
    });
    LanguageToolWrapper.setBridgeFromCurrentProject();
}
Also used : ExternalLinked(org.omegat.core.data.TMXEntry.ExternalLinked) EntryKey(org.omegat.core.data.EntryKey) StatisticsInfo(org.omegat.core.statistics.StatisticsInfo) ProjectProperties(org.omegat.core.data.ProjectProperties) IProject(org.omegat.core.data.IProject) ExternalLinked(org.omegat.core.data.TMXEntry.ExternalLinked) ITokenizer(org.omegat.tokenizer.ITokenizer) Language(org.omegat.util.Language) SourceTextEntry(org.omegat.core.data.SourceTextEntry) List(java.util.List) PrepareTMXEntry(org.omegat.core.data.PrepareTMXEntry) Map(java.util.Map) PrepareTMXEntry(org.omegat.core.data.PrepareTMXEntry) TMXEntry(org.omegat.core.data.TMXEntry) Before(org.junit.Before)

Example 5 with PrepareTMXEntry

use of org.omegat.core.data.PrepareTMXEntry 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);
        }
    });
    // 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 (PrepareTMXEntry tmen : en.getValue().getEntries()) {
            checkStopped(stop);
            if (tmen.source == null) {
                // Not all TMX entries have a source; in that case there can be no meaningful match, so skip.
                continue;
            }
            if (requiresTranslation && tmen.translation == null) {
                continue;
            }
            processEntry(null, tmen.source, tmen.translation, NearString.MATCH_SOURCE.TM, false, penalty, en.getKey(), tmen.creator, tmen.creationDate, tmen.changer, tmen.changeDate, tmen.otherProperties);
        }
    }
    // 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) 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) PrepareTMXEntry(org.omegat.core.data.PrepareTMXEntry) PrepareTMXEntry(org.omegat.core.data.PrepareTMXEntry) TMXEntry(org.omegat.core.data.TMXEntry) MultipleTranslationsIterator(org.omegat.core.data.IProject.MultipleTranslationsIterator)

Aggregations

PrepareTMXEntry (org.omegat.core.data.PrepareTMXEntry)9 SourceTextEntry (org.omegat.core.data.SourceTextEntry)5 TMXEntry (org.omegat.core.data.TMXEntry)5 Map (java.util.Map)4 HashMap (java.util.HashMap)3 IOException (java.io.IOException)2 TreeMap (java.util.TreeMap)2 EntryKey (org.omegat.core.data.EntryKey)2 ExternalTMX (org.omegat.core.data.ExternalTMX)2 ProjectProperties (org.omegat.core.data.ProjectProperties)2 RealProject (org.omegat.core.data.RealProject)2 Language (org.omegat.util.Language)2 Point (java.awt.Point)1 File (java.io.File)1 FileOutputStream (java.io.FileOutputStream)1 OutputStreamWriter (java.io.OutputStreamWriter)1 PrintWriter (java.io.PrintWriter)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 ExecutionException (java.util.concurrent.ExecutionException)1