Skip to content

Commit

Permalink
Merge branch 'dev' into changeFontSize
Browse files Browse the repository at this point in the history
  • Loading branch information
tisfeng authored Dec 22, 2023
2 parents e57b6a1 + 12a8768 commit ad4af8f
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 34 deletions.
6 changes: 1 addition & 5 deletions Easydict.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -218,8 +218,6 @@
2746AEC12AF95138005FE0A1 /* CaiyunService.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2746AEC02AF95138005FE0A1 /* CaiyunService.swift */; };
278322602B0FB0EA0026644C /* CaiyunResponse.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2783225F2B0FB0EA0026644C /* CaiyunResponse.swift */; };
278322622B0FB8EF0026644C /* CaiyunTranslateType.swift in Sources */ = {isa = PBXBuildFile; fileRef = 278322612B0FB8EF0026644C /* CaiyunTranslateType.swift */; };
27B7919E2AEC36A1006E07C6 /* Easydict.xcconfig in Resources */ = {isa = PBXBuildFile; fileRef = 27B7919C2AEC36A1006E07C6 /* Easydict.xcconfig */; };
27B7919F2AEC36A1006E07C6 /* Easydict-debug.xcconfig in Resources */ = {isa = PBXBuildFile; fileRef = 27B7919D2AEC36A1006E07C6 /* Easydict-debug.xcconfig */; };
6220AD5B2A82812300BBFB52 /* EZBingService.m in Sources */ = {isa = PBXBuildFile; fileRef = 6220AD5A2A82812300BBFB52 /* EZBingService.m */; };
6295DE312A84D82E006145F4 /* EZBingTranslateModel.m in Sources */ = {isa = PBXBuildFile; fileRef = 6295DE302A84D82E006145F4 /* EZBingTranslateModel.m */; };
6295DE342A84EF76006145F4 /* EZBingLookupModel.m in Sources */ = {isa = PBXBuildFile; fileRef = 6295DE332A84EF76006145F4 /* EZBingLookupModel.m */; };
Expand Down Expand Up @@ -2075,7 +2073,7 @@
attributes = {
BuildIndependentTargetsInParallel = YES;
CLASSPREFIX = EZ;
LastUpgradeCheck = 1500;
LastUpgradeCheck = 1510;
ORGANIZATIONNAME = izual;
TargetAttributes = {
0313F86C2AD5577400A5CFB0 = {
Expand Down Expand Up @@ -2140,13 +2138,11 @@
isa = PBXResourcesBuildPhase;
buildActionMask = 2147483647;
files = (
27B7919E2AEC36A1006E07C6 /* Easydict.xcconfig in Resources */,
0310C8272A94F5DF00B1D81E /* apple-dictionary.html in Resources */,
03BFBB802923A2FA00C48725 /* [email protected] in Resources */,
031DBD792AE01E130071CF85 /* easydict in Resources */,
03BFBB7329239E9F00C48725 /* [email protected] in Resources */,
03882F9229D95044005B5A52 /* Info.plist in Resources */,
27B7919F2AEC36A1006E07C6 /* Easydict-debug.xcconfig in Resources */,
03BFBB7C2923A1D900C48725 /* [email protected] in Resources */,
03882F9029D95044005B5A52 /* ToastWindowController.xib in Resources */,
03BFBB652923998300C48725 /* [email protected] in Resources */,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "1500"
LastUpgradeVersion = "1510"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "1500"
LastUpgradeVersion = "1510"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
Expand Down
6 changes: 3 additions & 3 deletions Easydict/Feature/EventMonitor/EZEventMonitor.m
Original file line number Diff line number Diff line change
Expand Up @@ -194,8 +194,6 @@ - (void)start {

// Monitor global events, Ref: https://blog.csdn.net/ch_soft/article/details/7371136
- (void)startMonitor {
[self monitorCGEventTap];

[NSEvent addLocalMonitorForEventsMatchingMask:NSEventMaskKeyDown handler:^NSEvent *_Nullable(NSEvent *_Nonnull event) {
if (event.keyCode == kVK_Escape) { // escape
NSLog(@"escape");
Expand Down Expand Up @@ -302,7 +300,9 @@ - (void)getSelectedText:(BOOL)checkTextFrame completion:(void (^)(NSString *_Nul
self.selectTextType = EZSelectTextTypeAccessibility;

// Monitor CGEventTap must be required after using Accessibility successfully.
[self monitorCGEventTap];
if (EZConfiguration.shared.autoSelectText) {
[self monitorCGEventTap];
}

self.selectedTextEditable = [EZSystemUtility isSelectedTextEditable];

Expand Down
16 changes: 10 additions & 6 deletions Easydict/Feature/Service/Apple/AppleDictionary/EZAppleDictionary.m
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#import "EZWindowManager.h"
#import "NSString+EZUtils.h"
#import "NSString+EZHandleInputText.h"
#import "NSString+EZChineseText.h"

@implementation EZAppleDictionary

Expand Down Expand Up @@ -605,6 +606,9 @@ - (BOOL)isValidHeadword:(NSString *)headword

// If text is Chinese
if ([EZLanguageManager.shared isChineseLanguage:language]) {
if (word.length == 1) {
return YES;
}

/**
開 --> 开
Expand All @@ -613,15 +617,15 @@ - (BOOL)isValidHeadword:(NSString *)headword
開始 --> 開始 kāishǐ
国色天香 --> 国色天香 guósè-tiānxiāng, 国色天香 guó sè tiān xiāng, 天香国色 tiān xiāng guó sè
浮云终日行 --> 浮 fú xxx
奇怪字符 --> 奇怪 qiguai xxx
*/

if (word.length == 1) {
return YES;
}
normalizedWord = [normalizedWord toSimplifiedChineseText];
normalizedHeadword = [normalizedHeadword toSimplifiedChineseText];

BOOL hasWordSubstring = [normalizedHeadword containsString:normalizedWord];
BOOL hasSameWordParts = [normalizedWord wordsInText].count == [normalizedHeadword wordsInText].count;
if (hasWordSubstring || hasSameWordParts) {
NSString *pureChineseHeadwords = [normalizedHeadword removeAlphabet].trim;
BOOL hasWordSubstring = [pureChineseHeadwords containsString:normalizedWord];
if (hasWordSubstring) {
return YES;
}

Expand Down
47 changes: 29 additions & 18 deletions Easydict/Feature/Service/OpenAI/EZOpenAIService.m
Original file line number Diff line number Diff line change
Expand Up @@ -477,7 +477,7 @@ - (NSString *)parseContentFromStreamData:(NSData *)data
NSString *dataKey = @"data:";
NSString *terminationFlag = @"[DONE]";
NSArray *jsonArray = [jsonDataString componentsSeparatedByString:dataKey];
// NSLog(@"jsonArray: %@", jsonArray);
// NSLog(@"jsonArray: %@", jsonArray);

NSMutableString *mutableString = [NSMutableString string];

Expand Down Expand Up @@ -827,19 +827,29 @@ - (NSArray *)translatioMessages:(NSString *)text from:(EZLanguage)sourceLanguage
self.result.to = answerLanguage;

NSString *prompt = @"";
NSString *literalTranslation = @"Literal Translation";
NSString *keyWords = @"Key Words";
NSString *grammarParse = @"Grammar Parsing";
NSString *inferenceTranslation = @"Inferential Translation";
NSString *freeTranslation = @"Free Translation";

if ([EZLanguageManager.shared isChineseLanguage:answerLanguage]) {
literalTranslation = @"直译";
keyWords = @"重点词汇";
grammarParse = @"语法分析";
inferenceTranslation = @"推理翻译";
freeTranslation = @"意译";
}

NSString *sentencePrompt = [NSString stringWithFormat:@"Here is a %@ sentence: \"\"\"%@\"\"\" .\n", sourceLanguage, sentence];

/**
Fuck, Google Gemini cannot input this text, no result returned.
"分析这个英语句子: \"\"\"Body cam shows man shot after attacking a police officer\"\"\""
So we need to use ``` wrap it.
*/
NSString *sentencePrompt = [NSString stringWithFormat:@"Here is a %@ sentence: ```%@```.\n", sourceLanguage, sentence];
prompt = [prompt stringByAppendingString:sentencePrompt];

NSString *directTransaltionPrompt = [NSString stringWithFormat:@"First, translate the sentence into %@ text, desired format: \" $(literal_translation) \",\n\n", targetLanguage];
NSString *directTransaltionPrompt = [NSString stringWithFormat:@"First, translate the sentence into %@ text literally, keep the original format, and don’t miss any information, desired display format: \"%@:\n {literal_translation_result} \",\n\n", targetLanguage, literalTranslation];
prompt = [prompt stringByAppendingString:directTransaltionPrompt];


Expand All @@ -858,14 +868,14 @@ - (NSArray *)translatioMessages:(NSString *)text from:(EZLanguage)sourceLanguage
Improving the country's economy is a political imperative for the new president.
I must dash off this letter before the post is collected.
*/
NSString *keyWordsPrompt = [NSString stringWithFormat:@"1. List the non-simple and key words and phrases in the sentence, no more than 6 key words, and look up all parts of speech and meanings of each key word, and point out its actual meaning in this sentence in detail, desired format: \"%@:\n xxx \", \n\n", keyWords];
NSString *keyWordsPrompt = [NSString stringWithFormat:@"1. List the non-simple and key words and phrases in the sentence, no more than 6 key words, and look up all parts of speech and meanings of each key word, and point out its actual meaning in this sentence in detail, desired display format: \"%@:\n xxx \", \n\n", keyWords];
prompt = [prompt stringByAppendingString:keyWordsPrompt];

NSString *grammarParsePrompt = [NSString stringWithFormat:@"2. Analyze the grammatical structure of this sentence, desired format: \"%@:\n xxx \", \n\n", grammarParse];
NSString *grammarParsePrompt = [NSString stringWithFormat:@"2. Analyze the grammatical structure of this sentence, desired display format: \"%@:\n xxx \", \n\n", grammarParse];
prompt = [prompt stringByAppendingString:grammarParsePrompt];

NSString *inferentialTranslationPrompt = [NSString stringWithFormat:@"3. You are a translation expert who is proficient in step-by-step analysis and reasoning. Generate an %@ $(inferential_translation) of the sentence based on the actual meaning of the keywords listed earlier as well as contextual. Note that the $(inferential_translation) is different from the previous $(literal_translation). $(inferential_translation) only contains the final translation result. Display in this format: \"%@: $(inferential_translation) \", \n\n", targetLanguage, inferenceTranslation];
prompt = [prompt stringByAppendingString:inferentialTranslationPrompt];
NSString *freeTranslationPrompt = [NSString stringWithFormat:@"3. According to the results of literal translation, find out the existing problems, including not limited to: not in line with %@ expression habits, sentence is not smooth, obscure, difficult to understand, and then re-free translation, on the basis of ensuring the original meaning of the content, make it easier to understand, more in line with the %@ expression habits, while keeping the original format unchanged, desired display format: \"%@:\n {free_translation_result} \", \n\n", targetLanguage, targetLanguage, freeTranslation];
prompt = [prompt stringByAppendingString:freeTranslationPrompt];

NSString *answerLanguagePrompt = [NSString stringWithFormat:@"Answer in %@. \n", answerLanguage];
prompt = [prompt stringByAppendingString:answerLanguagePrompt];
Expand Down Expand Up @@ -971,7 +981,8 @@ - (NSArray *)translatioMessages:(NSString *)text from:(EZLanguage)sourceLanguage
@"content" : kTranslationSystemPrompt,
},
];
NSMutableArray *messages = [NSMutableArray arrayWithArray:systemMessages];
NSMutableArray *messages = [NSMutableArray array];
[messages addObjectsFromArray:systemMessages];

if ([EZLanguageManager.shared isChineseLanguage:answerLanguage]) {
[messages addObjectsFromArray:chineseFewShot];
Expand Down Expand Up @@ -1045,7 +1056,7 @@ - (NSArray *)translatioMessages:(NSString *)text from:(EZLanguage)sourceLanguage
exampleSentence = @"例句";
}

NSString *pronunciationPrompt = [NSString stringWithFormat:@"Look up its pronunciation, desired format: \"%@: / xxx /\" \n", pronunciation];
NSString *pronunciationPrompt = [NSString stringWithFormat:@"Look up its pronunciation, desired display format: \"%@: / xxx /\" \n", pronunciation];
prompt = [prompt stringByAppendingString:pronunciationPrompt];

if (isEnglishWord) {
Expand All @@ -1059,26 +1070,26 @@ - (NSArray *)translatioMessages:(NSString *)text from:(EZLanguage)sourceLanguage
// prompt = [prompt stringByAppendingString:examPrompt];

// <tense or form>xxx: <word>xxx
NSString *tensePrompt = @"Look up its all tenses and forms, each line only display one tense or form, if has, show desired format: \" xxx \" . \n"; // 复数 looks 第三人称单数 looks 现在分词 looking 过去式 looked 过去分词 looked
NSString *tensePrompt = @"Look up its all tenses and forms, each line only display one tense or form, if has, show desired display format: \" xxx \" . \n"; // 复数 looks 第三人称单数 looks 现在分词 looking 过去式 looked 过去分词 looked
prompt = [prompt stringByAppendingString:tensePrompt];
} else {
NSString *translationPrompt = [self translationPrompt:word from:sourceLanguage to:targetLanguage];
translationPrompt = [translationPrompt stringByAppendingFormat:@", desired format: \"%@: xxx \" ", translationTitle];
translationPrompt = [translationPrompt stringByAppendingFormat:@", desired display format: \"%@: xxx \" ", translationTitle];
prompt = [prompt stringByAppendingString:translationPrompt];
}

NSString *explanationPrompt = [NSString stringWithFormat:@"\nLook up its brief <%@> explanation in clear and understandable way, desired format: \"%@: xxx \" \n", answerLanguage, explanation];
NSString *explanationPrompt = [NSString stringWithFormat:@"\nLook up its brief <%@> explanation in clear and understandable way, desired display format: \"%@: xxx \" \n", answerLanguage, explanation];
prompt = [prompt stringByAppendingString:explanationPrompt];

// !!!: This shoud use "词源学" instead of etymology when look up Chinese words.
NSString *etymologyPrompt = [NSString stringWithFormat:@"Look up its detailed %@, including but not limited to the original origin of the word, how the word's meaning has changed, and the current common meaning. Desired format: \"%@: xxx \" . \n", etymology, etymology];
NSString *etymologyPrompt = [NSString stringWithFormat:@"Look up its detailed %@, including but not limited to the original origin of the word, how the word's meaning has changed, and the current common meaning. desired display format: \"%@: xxx \" . \n", etymology, etymology];
prompt = [prompt stringByAppendingString:etymologyPrompt];

if (isEnglishWord) {
NSString *rememberWordPrompt = [NSString stringWithFormat:@"Look up disassembly and association methods to remember it, desired format: \"%@: xxx \" \n", howToRemember];
NSString *rememberWordPrompt = [NSString stringWithFormat:@"Look up disassembly and association methods to remember it, desired display format: \"%@: xxx \" \n", howToRemember];
prompt = [prompt stringByAppendingString:rememberWordPrompt];

// NSString *cognatesPrompt = [NSString stringWithFormat:@"\nLook up its most commonly used <%@> cognates, no more than 6, desired format: \"%@: xxx \" ", sourceLanguage, cognate];
// NSString *cognatesPrompt = [NSString stringWithFormat:@"\nLook up its most commonly used <%@> cognates, no more than 6, desired display format: \"%@: xxx \" ", sourceLanguage, cognate];
NSString *cognatesPrompt = [NSString stringWithFormat:@"\nLook up main <%@> words with the same root word as \"%@\", no more than 6, excluding phrases, display all parts of speech and meanings of the same root word, pos always displays its English abbreviation. If there are words with the same root, show format: \"%@: xxx \", otherwise don't display it. ", sourceLanguage, word, cognate];
prompt = [prompt stringByAppendingString:cognatesPrompt];
}
Expand Down

0 comments on commit ad4af8f

Please sign in to comment.