Skip to content

Commit

Permalink
[WIP] predict next words, step3
Browse files Browse the repository at this point in the history
  • Loading branch information
dongyuwei committed May 2, 2024
1 parent d97997f commit c085bc9
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 32 deletions.
2 changes: 1 addition & 1 deletion package/PackageInfo
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
<?xml version="1.0" encoding="utf-8" standalone="no"?>
<pkg-info postinstall-action="logout"/>
<pkg-info postinstall-action="none"/>
104 changes: 73 additions & 31 deletions src/InputController.mm
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ - (BOOL)onKeyEvent:(NSEvent *)event client:(id)sender {
NSString *bufferedSentence = [self sentenceBuffer];
bool hasBufferedText = bufferedText && bufferedText.length > 0;
bool hasSentence = bufferedSentence && bufferedSentence.length > 0;

BOOL isCandidatesVisible = [sharedCandidates isVisible];

if (keyCode == KEY_DELETE) {
if (hasBufferedText) {
Expand All @@ -99,15 +99,25 @@ - (BOOL)onKeyEvent:(NSEvent *)event client:(id)sender {
}

if (keyCode == KEY_SPACE) {
if (hasBufferedText || hasSentence) {
if (hasBufferedText) {
[self commitComposition:sender];
return YES;
}

if (hasSentence && isCandidatesVisible) {
[self commitComposition:sender];
return YES;
}

return NO;
}

if (keyCode == KEY_RETURN) {
if (hasBufferedText || hasSentence) {
if (hasBufferedText) {
[self commitCompositionWithoutSpace:sender];
return YES;
}
if (hasSentence && isCandidatesVisible) {
[self commitCompositionWithoutSpace:sender];
return YES;
}
Expand All @@ -117,6 +127,7 @@ - (BOOL)onKeyEvent:(NSEvent *)event client:(id)sender {
if (keyCode == KEY_ESC) {
[self cancelComposition];
[sender insertText:@""];
[self setSentenceBuffer:@""];
[self reset];
return YES;
}
Expand All @@ -131,7 +142,6 @@ - (BOOL)onKeyEvent:(NSEvent *)event client:(id)sender {
}

if ([self isMojaveAndLaterSystem]) {
BOOL isCandidatesVisible = [sharedCandidates isVisible];
if (isCandidatesVisible) {
if (keyCode == KEY_ARROW_DOWN) {
[sharedCandidates moveDown:self];
Expand All @@ -147,9 +157,10 @@ - (BOOL)onKeyEvent:(NSEvent *)event client:(id)sender {
}

if ([[NSCharacterSet decimalDigitCharacterSet] characterIsMember:ch]) {
if (!(hasBufferedText || hasSentence)) {
if (!hasBufferedText || !hasSentence) {
[self appendToComposedBuffer:characters];
[self commitCompositionWithoutSpace:sender];
[self setSentenceBuffer:@""];
return YES;
}

Expand Down Expand Up @@ -191,15 +202,11 @@ - (BOOL)isMojaveAndLaterSystem {

- (BOOL)deleteBackward:(id)sender {
NSMutableString *originalText = [self originalBuffer];
NSMutableString *sensence = [self sentenceBuffer];

if (_insertionIndex > 0) {
--_insertionIndex;

NSString *convertedString = [originalText substringToIndex:originalText.length - 1];
NSString *convertedSentence = [sensence substringToIndex:sensence.length - 1];
[self setSentenceBuffer: convertedSentence];

[self setComposedBuffer:convertedString];
[self setOriginalBuffer:convertedString];

Expand All @@ -216,12 +223,32 @@ - (BOOL)deleteBackward:(id)sender {
return NO;
}

- (BOOL)lastCharIsSymbol:(NSString *)text {
if (text.length > 0) {
unichar lastChar = [text characterAtIndex:text.length - 1];
return [[NSCharacterSet decimalDigitCharacterSet] characterIsMember:lastChar] ||
[[NSCharacterSet symbolCharacterSet] characterIsMember:lastChar] ||
[[NSCharacterSet punctuationCharacterSet] characterIsMember:lastChar];
}
return NO;
}

- (void)commitComposition:(id)sender {
NSString *text = [self composedBuffer];

if (text == nil || text.length == 0) {
text = [self originalBuffer];
}
BOOL isCandidatesVisible = [sharedCandidates isVisible];
if ((text == nil || text.length == 0) && isCandidatesVisible && _candidates.count > 0) {
NSInteger selectedCandidateIndex = [sharedCandidates selectedCandidate];
if (selectedCandidateIndex != NSNotFound) {
text = [_candidates objectAtIndex: selectedCandidateIndex];
}
}

[self appendSentenceBuffer: text];

BOOL commitWordWithSpace = [preference boolForKey:@"commitWordWithSpace"];

if (commitWordWithSpace && text.length > 0) {
Expand All @@ -236,8 +263,37 @@ - (void)commitComposition:(id)sender {

[self reset];

[self getPredictions];

}

- (void)commitCompositionWithoutSpace:(id)sender {
NSString *text = [self composedBuffer];

if (text == nil || text.length == 0) {
text = [self originalBuffer];
}

BOOL isCandidatesVisible = [sharedCandidates isVisible];
if ((text == nil || text.length == 0) && isCandidatesVisible && _candidates.count > 0) {
NSInteger selectedCandidateIndex = [sharedCandidates selectedCandidate];
if (selectedCandidateIndex != NSNotFound) {
text = [_candidates objectAtIndex: selectedCandidateIndex];
}
}

[self appendSentenceBuffer: text];

[sender insertText:text replacementRange:NSMakeRange(NSNotFound, NSNotFound)];

[self reset];

[self getPredictions];
}

- (void) getPredictions {
NSLog(@"Current Sentence Buffer: %@", self.sentenceBuffer);
if ([self doesSentenceBufferIncludeSpace]) {
if (self.sentenceBuffer && self.sentenceBuffer.length > 0 && ![self lastCharIsSymbol: self.sentenceBuffer]) {
[self fetchPredictionsForText:self.sentenceBuffer completion:^(NSDictionary *responseDict, NSArray *bertArray, NSError *error) {
if (error) {
NSLog(@"Error: %@", error.localizedDescription);
Expand All @@ -250,24 +306,7 @@ - (void)commitComposition:(id)sender {
}
}];
}

}

- (BOOL)doesSentenceBufferIncludeSpace {
NSRange range = [self.sentenceBuffer rangeOfString:@" "];
return range.location != NSNotFound;
}

- (void)commitCompositionWithoutSpace:(id)sender {
NSString *text = [self composedBuffer];

if (text == nil || text.length == 0) {
text = [self originalBuffer];
}

[sender insertText:text replacementRange:NSMakeRange(NSNotFound, NSNotFound)];

[self reset];
}

- (NSString *) fetchAPIURL {
Expand Down Expand Up @@ -352,10 +391,6 @@ - (NSMutableString *)composedBuffer {

- (void)setComposedBuffer:(NSString *)string {
NSMutableString *buffer = [self composedBuffer];
if (string && string.length > 0) {
NSString * sentence = self.sentenceBuffer;
[self setSentenceBuffer: [NSString stringWithFormat:@"%@ %@", sentence, string]];
}
[buffer setString:string];
}

Expand All @@ -366,6 +401,13 @@ - (NSMutableString *)sentenceBuffer {
return _sentenceBuffer;
}

- (void)appendSentenceBuffer:(NSString *)input {
NSMutableString *buffer = [self sentenceBuffer];
[buffer appendString: @" "];
[buffer appendString:input];
[buffer appendString: @" "];
}

- (void)setSentenceBuffer:(NSString *)string {
NSMutableString *buffer = [self sentenceBuffer];
[buffer setString:string];
Expand Down

0 comments on commit c085bc9

Please sign in to comment.