parent
8fd9808f56
commit
3760d24350
|
@ -76,7 +76,6 @@ static NSString *const kCandidateListTextSizeKey = @"CandidateListTextSize";
|
|||
static NSString *const kSelectPhraseAfterCursorAsCandidatePreferenceKey = @"SelectPhraseAfterCursorAsCandidate";
|
||||
static NSString *const kUseHorizontalCandidateListPreferenceKey = @"UseHorizontalCandidateList";
|
||||
static NSString *const kComposingBufferSizePreferenceKey = @"ComposingBufferSize";
|
||||
static NSString *const kDisableUserCandidateSelectionLearning = @"DisableUserCandidateSelectionLearning";
|
||||
static NSString *const kChooseCandidateUsingSpaceKey = @"ChooseCandidateUsingSpaceKey";
|
||||
static NSString *const kChineseConversionEnabledKey = @"ChineseConversionEnabledKey";
|
||||
static NSString *const kEscToCleanInputBufferKey = @"EscToCleanInputBufferKey";
|
||||
|
@ -104,9 +103,6 @@ enum {
|
|||
kDeleteKeyCode = 117
|
||||
};
|
||||
|
||||
// a global object for saving the "learned" user candidate selections
|
||||
NSMutableDictionary *gCandidateLearningDictionary = nil;
|
||||
NSString *gUserCandidatesDictionaryPath = nil;
|
||||
VTCandidateController *gCurrentCandidateController = nil;
|
||||
|
||||
// if DEBUG is defined, a DOT file (GraphViz format) will be written to the
|
||||
|
@ -133,10 +129,7 @@ static inline NSString *LocalizationNotNeeded(NSString *s) {
|
|||
- (void)collectCandidates;
|
||||
|
||||
- (size_t)actualCandidateCursorIndex;
|
||||
- (NSString *)neighborTrigramString;
|
||||
|
||||
- (void)_performDeferredSaveUserCandidatesDictionary;
|
||||
- (void)saveUserCandidatesDictionary;
|
||||
- (void)_showCandidateWindowUsingVerticalMode:(BOOL)useVerticalMode client:(id)client;
|
||||
|
||||
- (void)beep;
|
||||
|
@ -190,11 +183,6 @@ public:
|
|||
// create the composing buffer
|
||||
_composingBuffer = [[NSMutableString alloc] init];
|
||||
|
||||
// populate the settings, by default, DISABLE user candidate learning
|
||||
if (![[NSUserDefaults standardUserDefaults] objectForKey:kDisableUserCandidateSelectionLearning]) {
|
||||
[[NSUserDefaults standardUserDefaults] setObject:(id)kCFBooleanTrue forKey:kDisableUserCandidateSelectionLearning];
|
||||
}
|
||||
|
||||
_inputMode = kBopomofoModeIdentifier;
|
||||
_chineseConversionEnabled = [[NSUserDefaults standardUserDefaults] boolForKey:kChineseConversionEnabledKey];
|
||||
}
|
||||
|
@ -209,30 +197,6 @@ public:
|
|||
NSMenuItem *preferenceMenuItem = [[NSMenuItem alloc] initWithTitle:NSLocalizedString(@"McBopomofo Preferences", @"") action:@selector(showPreferences:) keyEquivalent:@""];
|
||||
[menu addItem:preferenceMenuItem];
|
||||
|
||||
// If Option key is pressed, show the learning-related menu
|
||||
|
||||
#if DEBUG
|
||||
//I think the following line is 10.6+ specific
|
||||
if ([[NSEvent class] respondsToSelector:@selector(modifierFlags)] && ([NSEvent modifierFlags] & NSAlternateKeyMask)) {
|
||||
|
||||
BOOL learningEnabled = ![[NSUserDefaults standardUserDefaults] boolForKey:kDisableUserCandidateSelectionLearning];
|
||||
|
||||
NSMenuItem *learnMenuItem = [[NSMenuItem alloc] initWithTitle:NSLocalizedString(@"Enable Selection Learning", @"") action:@selector(toggleLearning:) keyEquivalent:@""];
|
||||
learnMenuItem.state = learningEnabled ? NSControlStateValueOn : NSControlStateValueOff;
|
||||
[menu addItem:learnMenuItem];
|
||||
|
||||
if (learningEnabled) {
|
||||
NSString *clearMenuItemTitle = [NSString stringWithFormat:NSLocalizedString(@"Clear Learning Dictionary (%ju Items)", @""), (uintmax_t)[gCandidateLearningDictionary count]];
|
||||
NSMenuItem *clearMenuItem = [[NSMenuItem alloc] initWithTitle:clearMenuItemTitle action:@selector(clearLearningDictionary:) keyEquivalent:@""];
|
||||
[menu addItem:clearMenuItem];
|
||||
|
||||
|
||||
NSMenuItem *dumpMenuItem = [[NSMenuItem alloc] initWithTitle:NSLocalizedString(@"Dump Learning Data to Console", @"") action:@selector(dumpLearningDictionary:) keyEquivalent:@""];
|
||||
[menu addItem:dumpMenuItem];
|
||||
}
|
||||
}
|
||||
#endif //DEBUG
|
||||
|
||||
NSMenuItem *chineseConversionMenuItem = [[NSMenuItem alloc] initWithTitle:NSLocalizedString(@"Chinese Conversion", @"") action:@selector(toggleChineseConverter:) keyEquivalent:@"G"];
|
||||
chineseConversionMenuItem.keyEquivalentModifierMask = NSEventModifierFlagCommand | NSEventModifierFlagControl;
|
||||
chineseConversionMenuItem.state = _chineseConversionEnabled ? NSControlStateValueOn : NSControlStateValueOff;
|
||||
|
@ -695,17 +659,6 @@ public:
|
|||
// then walk the lattice
|
||||
[self popOverflowComposingTextAndWalk:client];
|
||||
|
||||
// see if we need to override the selection if a learned one exists
|
||||
if (![[NSUserDefaults standardUserDefaults] boolForKey:kDisableUserCandidateSelectionLearning]) {
|
||||
NSString *trigram = [self neighborTrigramString];
|
||||
|
||||
// Lookup from the user dict to see if the trigram fit or not
|
||||
NSString *overrideCandidateString = [gCandidateLearningDictionary objectForKey:trigram];
|
||||
if (overrideCandidateString) {
|
||||
[self candidateSelected:(NSAttributedString *)overrideCandidateString];
|
||||
}
|
||||
}
|
||||
|
||||
// then update the text
|
||||
_bpmfReadingBuffer->clear();
|
||||
[self updateClientComposingBuffer:client];
|
||||
|
@ -1292,78 +1245,6 @@ public:
|
|||
return cursorIndex;
|
||||
}
|
||||
|
||||
- (NSString *)neighborTrigramString
|
||||
{
|
||||
// gather the "trigram" for user candidate selection learning
|
||||
|
||||
NSMutableArray *termArray = [NSMutableArray array];
|
||||
|
||||
size_t cursorIndex = [self actualCandidateCursorIndex];
|
||||
vector<NodeAnchor> nodes = _builder->grid().nodesCrossingOrEndingAt(cursorIndex);
|
||||
|
||||
const Node* prev = 0;
|
||||
const Node* current = 0;
|
||||
const Node* next = 0;
|
||||
|
||||
size_t wni = 0;
|
||||
size_t wnc = _walkedNodes.size();
|
||||
size_t accuSpanningLength = 0;
|
||||
for (wni = 0; wni < wnc; wni++) {
|
||||
NodeAnchor& anchor = _walkedNodes[wni];
|
||||
if (!anchor.node) {
|
||||
continue;
|
||||
}
|
||||
|
||||
accuSpanningLength += anchor.spanningLength;
|
||||
if (accuSpanningLength >= cursorIndex) {
|
||||
prev = current;
|
||||
current = anchor.node;
|
||||
break;
|
||||
}
|
||||
|
||||
current = anchor.node;
|
||||
}
|
||||
|
||||
if (wni + 1 < wnc) {
|
||||
next = _walkedNodes[wni + 1].node;
|
||||
}
|
||||
|
||||
string term;
|
||||
if (prev) {
|
||||
term = prev->currentKeyValue().key;
|
||||
[termArray addObject:[NSString stringWithUTF8String:term.c_str()]];
|
||||
}
|
||||
|
||||
if (current) {
|
||||
term = current->currentKeyValue().key;
|
||||
[termArray addObject:[NSString stringWithUTF8String:term.c_str()]];
|
||||
}
|
||||
|
||||
if (next) {
|
||||
term = next->currentKeyValue().key;
|
||||
[termArray addObject:[NSString stringWithUTF8String:term.c_str()]];
|
||||
}
|
||||
|
||||
return [termArray componentsJoinedByString:@"-"];
|
||||
}
|
||||
|
||||
- (void)_performDeferredSaveUserCandidatesDictionary
|
||||
{
|
||||
BOOL __unused success = [gCandidateLearningDictionary writeToFile:gUserCandidatesDictionaryPath atomically:YES];
|
||||
}
|
||||
|
||||
- (void)saveUserCandidatesDictionary
|
||||
{
|
||||
if (!gUserCandidatesDictionaryPath) {
|
||||
return;
|
||||
}
|
||||
|
||||
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(_performDeferredSaveUserCandidatesDictionary) object:nil];
|
||||
|
||||
// TODO: Const-ize the delay
|
||||
[self performSelector:@selector(_performDeferredSaveUserCandidatesDictionary) withObject:nil afterDelay:5.0];
|
||||
}
|
||||
|
||||
- (void)_showCandidateWindowUsingVerticalMode:(BOOL)useVerticalMode client:(id)client
|
||||
{
|
||||
// set the candidate panel style
|
||||
|
@ -1467,30 +1348,12 @@ public:
|
|||
[[NSApplication sharedApplication] activateIgnoringOtherApps:YES];
|
||||
}
|
||||
|
||||
- (void)toggleLearning:(id)sender
|
||||
{
|
||||
BOOL toggle = ![[NSUserDefaults standardUserDefaults] boolForKey:kDisableUserCandidateSelectionLearning];
|
||||
|
||||
[[NSUserDefaults standardUserDefaults] setBool:toggle forKey:kDisableUserCandidateSelectionLearning];
|
||||
}
|
||||
|
||||
- (void)toggleChineseConverter:(id)sender
|
||||
{
|
||||
_chineseConversionEnabled = !_chineseConversionEnabled;
|
||||
[[NSUserDefaults standardUserDefaults] setBool:_chineseConversionEnabled forKey:kChineseConversionEnabledKey];
|
||||
}
|
||||
|
||||
- (void)clearLearningDictionary:(id)sender
|
||||
{
|
||||
[gCandidateLearningDictionary removeAllObjects];
|
||||
[self _performDeferredSaveUserCandidatesDictionary];
|
||||
}
|
||||
|
||||
- (void)dumpLearningDictionary:(id)sender
|
||||
{
|
||||
NSLog(@"%@", gCandidateLearningDictionary);
|
||||
}
|
||||
|
||||
- (NSUInteger)candidateCountForController:(VTCandidateController *)controller
|
||||
{
|
||||
return [_candidates count];
|
||||
|
@ -1508,13 +1371,6 @@ public:
|
|||
// candidate selected, override the node with selection
|
||||
string selectedValue = [[_candidates objectAtIndex:index] UTF8String];
|
||||
|
||||
if (![[NSUserDefaults standardUserDefaults] boolForKey:kDisableUserCandidateSelectionLearning]) {
|
||||
NSString *trigram = [self neighborTrigramString];
|
||||
NSString *selectedNSString = [NSString stringWithUTF8String:selectedValue.c_str()];
|
||||
[gCandidateLearningDictionary setObject:selectedNSString forKey:trigram];
|
||||
[self saveUserCandidatesDictionary];
|
||||
}
|
||||
|
||||
size_t cursorIndex = [self actualCandidateCursorIndex];
|
||||
_builder->grid().fixNodeSelectedCandidate(cursorIndex, selectedValue);
|
||||
|
||||
|
@ -1545,57 +1401,4 @@ void LTLoadLanguageModel()
|
|||
{
|
||||
LTLoadLanguageModelFile(@"data", gLanguageModel);
|
||||
LTLoadLanguageModelFile(@"data-plain-bpmf", gLanguageModelPlainBopomofo);
|
||||
|
||||
|
||||
// initialize the singleton learning dictionary
|
||||
// putting singleton in @synchronized is the standard way in Objective-C
|
||||
// to avoid race condition
|
||||
gCandidateLearningDictionary = [[NSMutableDictionary alloc] init];
|
||||
|
||||
// the first instance is also responsible for loading the dictionary
|
||||
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDirectory, YES);
|
||||
if (![paths count]) {
|
||||
NSLog(@"Fatal error: cannot find Applicaiton Support directory.");
|
||||
return;
|
||||
}
|
||||
|
||||
NSString *appSupportPath = [paths objectAtIndex:0];
|
||||
NSString *userDictPath = [appSupportPath stringByAppendingPathComponent:@"McBopomofo"];
|
||||
|
||||
BOOL isDir = NO;
|
||||
BOOL exists = [[NSFileManager defaultManager] fileExistsAtPath:userDictPath isDirectory:&isDir];
|
||||
|
||||
if (exists) {
|
||||
if (!isDir) {
|
||||
NSLog(@"Fatal error: Path '%@' is not a directory", userDictPath);
|
||||
return;
|
||||
}
|
||||
}
|
||||
else {
|
||||
NSError *error = nil;
|
||||
BOOL success = [[NSFileManager defaultManager] createDirectoryAtPath:userDictPath withIntermediateDirectories:YES attributes:nil error:&error];
|
||||
if (!success) {
|
||||
NSLog(@"Failed to create directory '%@', error: %@", userDictPath, error);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: Change this
|
||||
NSString *userDictFile = [userDictPath stringByAppendingPathComponent:@"UserCandidatesCache.plist"];
|
||||
gUserCandidatesDictionaryPath = userDictFile;
|
||||
|
||||
exists = [[NSFileManager defaultManager] fileExistsAtPath:userDictFile isDirectory:&isDir];
|
||||
if (exists && !isDir) {
|
||||
NSData *data = [NSData dataWithContentsOfFile:userDictFile];
|
||||
if (!data) {
|
||||
return;
|
||||
}
|
||||
|
||||
id plist = [NSPropertyListSerialization propertyListWithData:data options:NSPropertyListImmutable format:NULL error:NULL];
|
||||
if (plist && [plist isKindOfClass:[NSDictionary class]]) {
|
||||
[gCandidateLearningDictionary setDictionary:(NSDictionary *)plist];
|
||||
NSLog(@"User dictionary read, item count: %ju", (uintmax_t)[gCandidateLearningDictionary count]);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue