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();
}
}
}
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();
}
}
}
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();
}
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));
}
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;
}
Aggregations