Skip to content

Commit

Permalink
Refactor: Ensure plugin's glossary search logic aligns with OmegaT's …
Browse files Browse the repository at this point in the history
…native implementation

- Refactored `OpenAITranslate` to use OmegaT's `GlossarySearcher`, ensuring consistency with OmegaT's native glossary search logic.
- Added minimal stub implementations for `SourceTextEntry` and `GlossarySearcher` to support the refactoring.
- Removed custom tokenization and glossary search methods, opting for OmegaT's built-in functionality to maintain consistency and reliability.
  • Loading branch information
ychoi-kr committed Aug 12, 2024
1 parent b1e89a3 commit 5392dba
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 41 deletions.
4 changes: 4 additions & 0 deletions src-stub/org/omegat/core/data/IProject.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.omegat.core.data;

import java.util.List;

import org.omegat.tokenizer.ITokenizer;

public interface IProject {
Expand All @@ -8,4 +10,6 @@ public interface IProject {

ProjectProperties getProjectProperties();

List<SourceTextEntry> getAllEntries();

}
10 changes: 10 additions & 0 deletions src-stub/org/omegat/core/data/SourceTextEntry.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package org.omegat.core.data;

public class SourceTextEntry {

public String getSrcText() {
// TODO Auto-generated method stub
return null;
}

}
20 changes: 20 additions & 0 deletions src-stub/org/omegat/gui/glossary/GlossarySearcher.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package org.omegat.gui.glossary;

import java.util.List;

import org.omegat.core.data.SourceTextEntry;
import org.omegat.tokenizer.ITokenizer;
import org.omegat.util.Language;

public class GlossarySearcher {

public GlossarySearcher(ITokenizer tok, Language lang, boolean mergeAltDefinitions) {
// TODO Auto-generated constructor stub
}

public List<GlossaryEntry> searchSourceMatches(SourceTextEntry ste, List<GlossaryEntry> entries) {
// TODO Auto-generated method stub
return null;
}

}
58 changes: 17 additions & 41 deletions src/kr/ychoi/otplugin/OpenAITranslate.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@
import org.omegat.util.WikiGet;
import org.json.*;
import org.omegat.gui.glossary.GlossaryEntry;
import org.omegat.gui.glossary.GlossaryManager;
import org.omegat.gui.glossary.GlossarySearcher;
import org.omegat.core.Core;
import org.omegat.tokenizer.ITokenizer;
import org.omegat.util.Token;
import org.omegat.core.data.SourceTextEntry;

/*
* OpenAI Translate plugin for OmegaT
Expand Down Expand Up @@ -44,20 +43,23 @@ public String getName() {

@Override
protected String translate(Language sLang, Language tLang, String text) throws Exception {
if (API_KEY == null) {
return "API key not found";
// 프로젝트에서 SourceTextEntry를 찾음
List<SourceTextEntry> entries = Core.getProject().getAllEntries();
SourceTextEntry matchingEntry = null;

for (SourceTextEntry entry : entries) {
if (entry.getSrcText().equals(text)) {
matchingEntry = entry;
break;
}
}

String cachedResult = getCachedTranslation(sLang, tLang, text);
if (cachedResult != null) {
return cachedResult;
List<GlossaryEntry> glossaryEntries = new ArrayList<>();
if (matchingEntry != null) {
// GlossarySearcher를 사용하여 용어집 검색 수행
GlossarySearcher glossarySearcher = new GlossarySearcher(Core.getProject().getSourceTokenizer(), sLang, true);
glossaryEntries = glossarySearcher.searchSourceMatches(matchingEntry, Core.getGlossaryManager().getGlossaryEntries(text));
}

// 텍스트 토큰화
Set<String> wordsInText = tokenizeText(text);

// 용어 검색
List<GlossaryEntry> glossaryEntries = searchGlossary(wordsInText);

// 시스템 프롬프트 및 사용자 프롬프트 작성
String systemPrompt = createSystemPrompt(sLang, tLang, glossaryEntries);
Expand All @@ -66,35 +68,9 @@ protected String translate(Language sLang, Language tLang, String text) throws E
System.out.println(userPrompt);

// OpenAI API 요청
String translatedText = requestTranslation(systemPrompt, userPrompt);

// 캐시에 저장
putToCache(sLang, tLang, text, translatedText);

return translatedText;
}

private Set<String> tokenizeText(String text) throws Exception {
ITokenizer tokenizer = Core.getProject().getSourceTokenizer();
Token[] tokens = tokenizer.tokenizeWords(text, ITokenizer.StemmingMode.NONE);
Set<String> words = new HashSet<>();
for (Token token : tokens) {
words.add(token.getTextFromString(text).toLowerCase());
}
return words;
return requestTranslation(systemPrompt, userPrompt);
}

private List<GlossaryEntry> searchGlossary(Set<String> wordsInText) throws Exception {
GlossaryManager glossaryManager = Core.getGlossaryManager();
List<GlossaryEntry> glossaryEntries = glossaryManager.getGlossaryEntries(String.join(" ", wordsInText));
List<GlossaryEntry> relevantEntries = new ArrayList<>();
for (GlossaryEntry entry : glossaryEntries) {
if (wordsInText.contains(entry.getSrcText().toLowerCase())) {
relevantEntries.add(entry);
}
}
return relevantEntries;
}

private String createSystemPrompt(Language sLang, Language tLang, List<GlossaryEntry> glossaryEntries) {
StringBuilder promptBuilder = new StringBuilder();
Expand Down

0 comments on commit 5392dba

Please sign in to comment.