Search in sources :

Example 1 with GlossaryEntry

use of org.omegat.gui.glossary.GlossaryEntry in project omegat by omegat-org.

the class Searcher method searchProject.

private void searchProject() {
    // reset the number of search hits
    m_numFinds = 0;
    // search the Memory, if requested
    if (m_searchExpression.memory) {
        // search through all project entries
        IProject dataEngine = m_project;
        for (int i = 0; i < m_project.getAllEntries().size(); i++) {
            // stop searching if the max. nr of hits has been reached
            if (m_numFinds >= expression.numberOfResults) {
                return;
            }
            // get the source and translation of the next entry
            SourceTextEntry ste = dataEngine.getAllEntries().get(i);
            TMXEntry te = m_project.getTranslationInfo(ste);
            checkEntry(ste.getSrcText(), te.translation, te.note, ste.getComment(), te, i, null);
            checkStop.checkInterrupted();
        }
        // search in orphaned
        if (!m_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 >= expression.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 >= expression.numberOfResults) {
                        return;
                    }
                    checkStop.checkInterrupted();
                    if (m_project.isOrphaned(source)) {
                        checkEntry(en.source, en.translation, en.note, null, en, ENTRY_ORIGIN_ORPHAN, file);
                    }
                }
            });
        }
    }
    // search the TM, if requested
    if (m_searchExpression.tm) {
        // that case.
        if (!expression.searchAuthor && !expression.searchDateAfter && !expression.searchDateBefore) {
            for (Map.Entry<String, ExternalTMX> tmEn : m_project.getTransMemories().entrySet()) {
                final String fileTM = tmEn.getKey();
                if (!searchEntries(tmEn.getValue().getEntries(), fileTM)) {
                    return;
                }
                checkStop.checkInterrupted();
            }
            for (Map.Entry<Language, ProjectTMX> tmEn : m_project.getOtherTargetLanguageTMs().entrySet()) {
                final Language langTM = tmEn.getKey();
                if (!searchEntriesAlternative(tmEn.getValue().getDefaults(), langTM.getLanguage())) {
                    return;
                }
                if (!searchEntriesAlternative(tmEn.getValue().getAlternatives(), langTM.getLanguage())) {
                    return;
                }
                checkStop.checkInterrupted();
            }
        }
    }
    // search the glossary, if requested
    if (m_searchExpression.glossary) {
        String intro = OStrings.getString("SW_GLOSSARY_RESULT");
        List<GlossaryEntry> entries = Core.getGlossaryManager().getLocalEntries();
        for (GlossaryEntry en : entries) {
            checkEntry(en.getSrcText(), en.getLocText(), null, null, null, ENTRY_ORIGIN_GLOSSARY, intro);
            // stop searching if the max. nr of hits has been reached
            if (m_numFinds >= expression.numberOfResults) {
                return;
            }
            checkStop.checkInterrupted();
        }
    }
}
Also used : ProjectTMX(org.omegat.core.data.ProjectTMX) EntryKey(org.omegat.core.data.EntryKey) IProject(org.omegat.core.data.IProject) Language(org.omegat.util.Language) GlossaryEntry(org.omegat.gui.glossary.GlossaryEntry) SourceTextEntry(org.omegat.core.data.SourceTextEntry) ExternalTMX(org.omegat.core.data.ExternalTMX) HashMap(java.util.HashMap) Map(java.util.Map) PrepareTMXEntry(org.omegat.core.data.PrepareTMXEntry) TMXEntry(org.omegat.core.data.TMXEntry)

Example 2 with GlossaryEntry

use of org.omegat.gui.glossary.GlossaryEntry in project omegat by omegat-org.

the class Searcher method searchGlossary.

private void searchGlossary() throws SearchLimitReachedException {
    // search the glossary, if requested
    if (searchExpression.glossary) {
        String intro = OStrings.getString("SW_GLOSSARY_RESULT");
        List<GlossaryEntry> entries = Core.getGlossaryManager().getLocalEntries();
        for (GlossaryEntry en : entries) {
            checkEntry(en.getSrcText(), en.getLocText(), null, null, null, ENTRY_ORIGIN_GLOSSARY, intro);
            // stop searching if the max. nr of hits has been reached
            if (m_numFinds >= searchExpression.numberOfResults) {
                throw new SearchLimitReachedException();
            }
            checkStop.checkInterrupted();
        }
    }
}
Also used : GlossaryEntry(org.omegat.gui.glossary.GlossaryEntry)

Example 3 with GlossaryEntry

use of org.omegat.gui.glossary.GlossaryEntry in project omegat by omegat-org.

the class EditorUtils method replaceGlossaryEntries.

/**
 * Given a list of glossary entries, replace any instances of the source term appearing in the given text with the
 * target term. When there are multiple target terms, the first one is used.
 *
 * @param text
 *            Text in which to replace glossary hits (assumed to be in the project's source language)
 * @param entries
 *            List of glossary entries
 * @param locale
 *            Locale with which to perform capitalization matching (assumed to be source locale)
 * @param tokenizer
 *            Tokenizer with which to split text (assumed to be project's source tokenizer)
 * @return Text with source glossary terms replaced with target terms
 */
public static String replaceGlossaryEntries(String text, List<GlossaryEntry> entries, Locale locale, ITokenizer tokenizer) {
    if (StringUtil.isEmpty(text) || entries == null || entries.isEmpty()) {
        return text;
    }
    StringBuilder sb = new StringBuilder();
    String[] haystack = tokenizer.tokenizeVerbatimToStrings(text);
    for (int i = 0; i < haystack.length; i++) {
        String tok = haystack[i];
        boolean replaced = false;
        for (GlossaryEntry e : entries) {
            String[] needle = tokenizer.tokenizeVerbatimToStrings(e.getSrcText());
            if (tokensPresentAt(needle, haystack, i, true)) {
                String toAppend = e.getLocText();
                if (!tokensPresentAt(needle, haystack, i, false)) {
                    // If the source tokens don't have matching case, fix the replacement's case to match
                    toAppend = StringUtil.matchCapitalization(toAppend, tok, locale);
                }
                sb.append(toAppend);
                replaced = true;
                i += needle.length - 1;
                break;
            }
        }
        if (!replaced) {
            sb.append(tok);
        }
    }
    return sb.toString();
}
Also used : GlossaryEntry(org.omegat.gui.glossary.GlossaryEntry)

Example 4 with GlossaryEntry

use of org.omegat.gui.glossary.GlossaryEntry in project omegat by omegat-org.

the class EditorUtilsTest method testReplaceGlossaryEntries.

@Test
public void testReplaceGlossaryEntries() {
    List<GlossaryEntry> entries = new ArrayList<GlossaryEntry>();
    entries.add(new GlossaryEntry("snowman", "sneeuwpop", "", false, null));
    entries.add(new GlossaryEntry("Bob", "Blub", "", false, null));
    ITokenizer tokenizer = new LuceneEnglishTokenizer();
    Locale locale = Locale.ENGLISH;
    String srcText = "Snowman Bob went to the snowman party. SnOwMaN!";
    String expected = "Sneeuwpop Blub went to the sneeuwpop party. sneeuwpop!";
    assertEquals(expected, EditorUtils.replaceGlossaryEntries(srcText, entries, locale, tokenizer));
    // Empty cases
    assertNull(EditorUtils.replaceGlossaryEntries(null, entries, locale, tokenizer));
    assertEquals("", EditorUtils.replaceGlossaryEntries("", entries, locale, tokenizer));
    assertSame(srcText, EditorUtils.replaceGlossaryEntries(srcText, null, locale, tokenizer));
    assertSame(srcText, EditorUtils.replaceGlossaryEntries(srcText, new ArrayList<GlossaryEntry>(), locale, tokenizer));
    try {
        EditorUtils.replaceGlossaryEntries(srcText, entries, null, tokenizer);
        fail("Should give NPE when given null locale");
    } catch (NullPointerException ex) {
    }
    try {
        EditorUtils.replaceGlossaryEntries(srcText, entries, locale, null);
        fail("Should give NPE when given null tokenizer");
    } catch (NullPointerException ex) {
    }
    // Multiword entry
    entries.add(0, new GlossaryEntry("snowman party", "sneeuwpop parti", "", false, null));
    srcText = "Snowman Bob went to the snowman party. SnOwMaN!";
    expected = "Sneeuwpop Blub went to the sneeuwpop parti. sneeuwpop!";
    assertEquals(expected, EditorUtils.replaceGlossaryEntries(srcText, entries, locale, tokenizer));
    // Replace final token (see https://sourceforge.net/p/omegat/bugs/819/)
    srcText = "Snowman Bob went to the snowman party. SnOwMaN";
    expected = "Sneeuwpop Blub went to the sneeuwpop parti. sneeuwpop";
    assertEquals(expected, EditorUtils.replaceGlossaryEntries(srcText, entries, locale, tokenizer));
}
Also used : Locale(java.util.Locale) ITokenizer(org.omegat.tokenizer.ITokenizer) GlossaryEntry(org.omegat.gui.glossary.GlossaryEntry) ArrayList(java.util.ArrayList) LuceneEnglishTokenizer(org.omegat.tokenizer.LuceneEnglishTokenizer) Test(org.junit.Test)

Example 5 with GlossaryEntry

use of org.omegat.gui.glossary.GlossaryEntry in project omegat by omegat-org.

the class TaaSGlossary method search.

@Override
public List<GlossaryEntry> search(Language sLang, Language tLang, String srcText) throws Exception {
    if (!Preferences.isPreferenceDefault(Preferences.TAAS_LOOKUP, false) || !TaaSPlugin.getClient().isAllowed()) {
        return Collections.emptyList();
    }
    TaasExtractionResult res = TaaSPlugin.getClient().termExtraction(sLang, tLang, srcText, Preferences.getPreference(Preferences.TAAS_DOMAIN));
    String data = TaaSPlugin.filterTaasResult(res.getTerms());
    List<GlossaryEntry> entries = GlossaryReaderTBX.read(data, false, OStrings.getString("TAAS_GLOSSARY_NAME"));
    Log.logDebug(LOGGER, "TaaS returns {0} glossary entries", entries.size());
    return entries;
}
Also used : GlossaryEntry(org.omegat.gui.glossary.GlossaryEntry) TaasExtractionResult(gen.taas.TaasExtractionResult)

Aggregations

GlossaryEntry (org.omegat.gui.glossary.GlossaryEntry)6 ArrayList (java.util.ArrayList)2 TaasExtractionResult (gen.taas.TaasExtractionResult)1 File (java.io.File)1 IOException (java.io.IOException)1 RandomAccessFile (java.io.RandomAccessFile)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Locale (java.util.Locale)1 Map (java.util.Map)1 Test (org.junit.Test)1 KnownException (org.omegat.core.KnownException)1 EntryKey (org.omegat.core.data.EntryKey)1 ExternalTMX (org.omegat.core.data.ExternalTMX)1 IProject (org.omegat.core.data.IProject)1 PrepareTMXEntry (org.omegat.core.data.PrepareTMXEntry)1 ProjectTMX (org.omegat.core.data.ProjectTMX)1 SourceTextEntry (org.omegat.core.data.SourceTextEntry)1 TMXEntry (org.omegat.core.data.TMXEntry)1 RebaseAndCommit (org.omegat.core.team2.RebaseAndCommit)1