Notifies the users not to add duplicated phrases.

This commit is contained in:
zonble 2022-02-04 05:31:47 +08:00
parent 472b149020
commit dc24de2ccb
6 changed files with 47 additions and 6 deletions

View File

@ -90,3 +90,5 @@
"Cursor is after \"%@\"." = "Cursor is after \"%@\"."; "Cursor is after \"%@\"." = "Cursor is after \"%@\".";
"Cursor is between \"%@\" and \"%@\"." = "Cursor is between \"%@\" and \"%@\"."; "Cursor is between \"%@\" and \"%@\"." = "Cursor is between \"%@\" and \"%@\".";
"You are now selecting \"%@\". The phrase already exists." = "You are now selecting \"%@\". The phrase already exists.";

View File

@ -185,6 +185,16 @@ class InputState: NSObject {
} else if (markedRange.length > kMaxMarkRangeLength) { } else if (markedRange.length > kMaxMarkRangeLength) {
return String(format: NSLocalizedString("You are now selecting \"%@\". A phrase cannot be longer than %d characters.", comment: ""), text, kMaxMarkRangeLength) return String(format: NSLocalizedString("You are now selecting \"%@\". A phrase cannot be longer than %d characters.", comment: ""), text, kMaxMarkRangeLength)
} }
let (exactBegin, _) = (composingBuffer as NSString).characterIndex(from: markedRange.location)
let (exactEnd, _) = (composingBuffer as NSString).characterIndex(from: markedRange.location + markedRange.length)
let selectedReadings = readings[exactBegin..<exactEnd]
let joined = selectedReadings.joined(separator: "-")
let exist = LanguageModelManager.checkIfExist(userPhrase: text, key: joined)
if exist {
return String(format: NSLocalizedString("You are now selecting \"%@\". The phrase already exists.", comment: ""), text)
}
return String(format: NSLocalizedString("You are now selecting \"%@\". Press enter to add a new phrase.", comment: ""), text) return String(format: NSLocalizedString("You are now selecting \"%@\". Press enter to add a new phrase.", comment: ""), text)
} }
@ -238,9 +248,18 @@ class InputState: NSObject {
if composingBuffer.count != readings.count { if composingBuffer.count != readings.count {
return false return false
} }
if markedRange.length < kMinMarkRangeLength {
return markedRange.length >= kMinMarkRangeLength && return false
markedRange.length <= kMaxMarkRangeLength }
if markedRange.length > kMaxMarkRangeLength {
return false
}
let text = (composingBuffer as NSString).substring(with: markedRange)
let (exactBegin, _) = (composingBuffer as NSString).characterIndex(from: markedRange.location)
let (exactEnd, _) = (composingBuffer as NSString).characterIndex(from: markedRange.location + markedRange.length)
let selectedReadings = readings[exactBegin..<exactEnd]
let joined = selectedReadings.joined(separator: "-")
return LanguageModelManager.checkIfExist(userPhrase: text, key: joined) == false
} }
@objc var userPhrase: String { @objc var userPhrase: String {

View File

@ -33,6 +33,8 @@ NS_ASSUME_NONNULL_BEGIN
+ (void)loadUserPhraseReplacement; + (void)loadUserPhraseReplacement;
+ (void)setupDataModelValueConverter; + (void)setupDataModelValueConverter;
+ (BOOL)checkIfUserLanguageModelFilesExist; + (BOOL)checkIfUserLanguageModelFilesExist;
+ (BOOL)checkIfUserPhraseExist:(NSString *)userPhrase key:(NSString *)key NS_SWIFT_NAME(checkIfExist(userPhrase:key:));
+ (BOOL)writeUserPhrase:(NSString *)userPhrase; + (BOOL)writeUserPhrase:(NSString *)userPhrase;
@property (class, readonly, nonatomic) NSString *dataFolderPath; @property (class, readonly, nonatomic) NSString *dataFolderPath;

View File

@ -193,6 +193,19 @@ static void LTLoadAssociatedPhrases(McBopomofoLM &lm)
return YES; return YES;
} }
+ (BOOL)checkIfUserPhraseExist:(NSString *)userPhrase key:(NSString *)key NS_SWIFT_NAME(checkIfExist(userPhrase:key:))
{
string unigramKey = string(key.UTF8String);
vector<Unigram> unigrams = gLanguageModelMcBopomofo.unigramsForKey(unigramKey);
string userPhraseString = string(userPhrase.UTF8String);
for (auto unigram: unigrams) {
if (unigram.keyValue.value == userPhraseString) {
return YES;
}
}
return NO;
}
+ (BOOL)writeUserPhrase:(NSString *)userPhrase + (BOOL)writeUserPhrase:(NSString *)userPhrase
{ {
if (![self checkIfUserLanguageModelFilesExist]) { if (![self checkIfUserLanguageModelFilesExist]) {

View File

@ -90,3 +90,6 @@
"Cursor is after \"%@\"." = "Cursor is after \"%@\"."; "Cursor is after \"%@\"." = "Cursor is after \"%@\".";
"Cursor is between \"%@\" and \"%@\"." = "Cursor is between \"%@\" and \"%@\"."; "Cursor is between \"%@\" and \"%@\"." = "Cursor is between \"%@\" and \"%@\".";
"You are now selecting \"%@\". The phrase already exists." = "You are now selecting \"%@\". The phrase already exists.";

View File

@ -47,11 +47,11 @@
"Use Half-Width Punctuations" = "使用半型標點符號"; "Use Half-Width Punctuations" = "使用半型標點符號";
"You are now selecting \"%@\". You can add a phrase with two or more characters." = "您目前選擇了 \"%@\"。請選擇兩個字以上,才能加入使用者詞彙。"; "You are now selecting \"%@\". You can add a phrase with two or more characters." = "您目前選擇了「%@」。請選擇兩個字以上,才能加入使用者詞彙。";
"You are now selecting \"%@\". Press enter to add a new phrase." = "您目前選擇了 \"%@\"。按下 Enter 就可以加入到使用者詞彙中。"; "You are now selecting \"%@\". Press enter to add a new phrase." = "您目前選擇了「%@」。按下 Enter 就可以加入到使用者詞彙中。";
"You are now selecting \"%@\". A phrase cannot be longer than %d characters." = "您目前選擇了 \"%@\"。自訂詞彙不能超過 %d 個字元。"; "You are now selecting \"%@\". A phrase cannot be longer than %d characters." = "您目前選擇了「%@」。自訂詞彙不能超過 %d 個字元。";
"Chinese conversion on" = "已經切換到簡體中文模式"; "Chinese conversion on" = "已經切換到簡體中文模式";
@ -90,3 +90,5 @@
"Cursor is after \"%@\"." = "游標正在「%@」後方"; "Cursor is after \"%@\"." = "游標正在「%@」後方";
"Cursor is between \"%@\" and \"%@\"." = "游標正在「%@」與「%@」之間"; "Cursor is between \"%@\" and \"%@\"." = "游標正在「%@」與「%@」之間";
"You are now selecting \"%@\". The phrase already exists." = "您目前選擇了「%@」,這個詞彙已經存在了";