Corrected the implementation of some of the methods in NSSpellChecker and NSSpellServer. I also added a working spell checker which uses ispell.

git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/gui/trunk@10541 72102866-910b-0410-8b05-ffd578937521
This commit is contained in:
Gregory John Casamento 2001-07-24 04:31:06 +00:00
parent d46026370e
commit 2543b6418b
5 changed files with 387 additions and 83 deletions

View file

@ -46,6 +46,8 @@
id _delegate;
BOOL _caseSensitive;
NSMutableDictionary *_userDictionaries;
NSString *_currentLanguage;
NSArray *_ignoredWords;
}
// Checking in Your Service

View file

@ -42,6 +42,7 @@
#include <AppKit/NSSpellChecker.h>
#include <AppKit/NSSpellServer.h>
#include <AppKit/NSTextField.h>
#include <AppKit/NSTextView.h>
#include <AppKit/NSPopUpButton.h>
#include <AppKit/IMLoading.h>
#include <AppKit/GSServicesManager.h>
@ -60,7 +61,7 @@ NSString *GSSpellServerName(NSString *checkerDictionary, NSString *language);
@protocol NSSpellServerPrivateProtocol
- (NSRange) _findMisspelledWordInString: (NSString *)stringToCheck
language: (NSString *)language
learnedDictionaries: (NSArray *)dictionaries
ignoredWords: (NSArray *)ignoredWords
wordCount: (int *)wordCount
countOnly: (BOOL)countOnly;
@ -180,7 +181,6 @@ static int __documentTag = 0;
//
+ (int)uniqueSpellDocumentTag
{
NSLog(@"returning unique spell document tag");
return ++__documentTag;
}
@ -191,23 +191,26 @@ static int __documentTag = 0;
- (id)_startServerForLanguage: (NSString *)language
{
id proxy = nil;
// Start the service for this language
proxy = [[NSApp _listener] _launchSpellCheckerForLanguage: language];
if(proxy == nil)
{
NSLog(@"Failed to get the spellserver");
return nil;
}
// Retain the proxy, if we got the connection.
// Also make sure that we handle the death of the server
// correctly.
[[NSNotificationCenter defaultCenter]
addObserver: self
selector: @selector(_handleServerDeath:)
name: NSConnectionDidDieNotification
object: [(NSDistantObject *)proxy connectionForProxy]];
else
{
// remove any previous notifications we are observing.
[[NSNotificationCenter defaultCenter] removeObserver: self];
// Make sure that we handle the death of the server correctly.
[[NSNotificationCenter defaultCenter]
addObserver: self
selector: @selector(_handleServerDeath:)
name: NSConnectionDidDieNotification
object: [(NSDistantObject *)proxy connectionForProxy]];
}
return proxy;
}
@ -317,7 +320,7 @@ static int __documentTag = 0;
NSRange r = NSMakeRange(0,0);
r = [[self _serverProxy] _findMisspelledWordInString: aString
language: _language
learnedDictionaries: nil
ignoredWords: nil
wordCount: &count
countOnly: YES];
@ -329,12 +332,14 @@ static int __documentTag = 0;
{
int wordCount = 0;
NSRange r = NSMakeRange(0,0);
id responder = [[[[NSApplication sharedApplication] mainWindow] contentView] documentView];
_currentTag = [responder spellCheckerDocumentTag];
r = [self checkSpellingOfString: stringToCheck
startingAt: startingOffset
language: _language
wrap: NO
inSpellDocumentWithTag: 0
inSpellDocumentWithTag: _currentTag
wordCount: &wordCount];
return r;
@ -349,8 +354,7 @@ static int __documentTag = 0;
{
NSRange r = NSMakeRange(0,0);
NSString *misspelledWord = nil;
NSArray *dictForTag = [self ignoredWordsInSpellDocumentWithTag: tag],
*suggestedWords = nil;
NSArray *dictForTag = [self ignoredWordsInSpellDocumentWithTag: tag];
_currentTag = tag;
// We have no string to work with
@ -373,7 +377,7 @@ static int __documentTag = 0;
NSString *substringToCheck = [stringToCheck substringFromIndex: startingOffset];
r = [[self _serverProxy] _findMisspelledWordInString: substringToCheck
language: _language
learnedDictionaries: dictForTag
ignoredWords: dictForTag
wordCount: wordCount
countOnly: NO];
@ -391,15 +395,13 @@ static int __documentTag = 0;
substringToIndex: startingOffset];
r = [[self _serverProxy] _findMisspelledWordInString: firstHalfOfString
language: _language
learnedDictionaries: dictForTag
ignoredWords: dictForTag
wordCount: wordCount
countOnly: NO];
}
}
misspelledWord = [stringToCheck substringFromRange: r];
//suggestedWords = [[self _serverProxy] _suggestGuessesForWord: misspelledWord
// inLanguage: _language];
}
NS_HANDLER
{
@ -455,7 +457,6 @@ inSpellDocumentWithTag:(int)tag
NSNumber *key = [NSNumber numberWithInt: tag];
NSMutableSet *words = [_ignoredWords objectForKey: key];
NSLog(@"Ignore: %@",wordToIgnore);
if(![wordToIgnore isEqualToString: @""])
{
// If there is a dictionary add to it, if not create one.
@ -469,8 +470,6 @@ inSpellDocumentWithTag:(int)tag
[words addObject: wordToIgnore];
}
}
NSLog(@"Words to ignore %@ for doc# %d", words, tag);
}
// get the list of ignored words.
@ -502,6 +501,21 @@ inSpellDocumentWithTag:(int)tag
[self setWordFieldStringValue: word];
}
- _findNext: (id)sender
{
BOOL processed = NO;
id responder = [[[[NSApplication sharedApplication] mainWindow] contentView] documentView];
processed = [responder tryToPerform: @selector(checkSpelling:)
with: _spellPanel];
if(!processed)
{
NSLog(@"No responder found");
}
return self;
}
- _learn: (id)sender
{
NSString *word = [_wordField stringValue];
@ -519,6 +533,8 @@ inSpellDocumentWithTag:(int)tag
}
NS_ENDHANDLER
[self _findNext: sender];
return self;
}
@ -540,6 +556,8 @@ inSpellDocumentWithTag:(int)tag
}
NS_ENDHANDLER
[self _findNext: sender];
return self;
}
@ -555,6 +573,8 @@ inSpellDocumentWithTag:(int)tag
NSLog(@"_ignore: No responder found");
}
[self _findNext: sender];
return self;
}
@ -565,21 +585,6 @@ inSpellDocumentWithTag:(int)tag
return self;
}
- _findNext: (id)sender
{
BOOL processed = NO;
id responder = [[[[NSApplication sharedApplication] mainWindow] contentView] documentView];
processed = [responder tryToPerform: @selector(checkSpelling:)
with: _spellPanel];
if(!processed)
{
NSLog(@"Call to checkSpelling failed. No responder found");
}
return self;
}
- _correct: (id)sender
{
BOOL processed = NO;
@ -589,8 +594,9 @@ inSpellDocumentWithTag:(int)tag
with: _wordField];
if(!processed)
{
NSLog(@"Call to changeSpelling failed. No responder found");
NSLog(@"No responder found");
}
[self _findNext: sender];
return self;
}
@ -708,12 +714,10 @@ inSpellDocumentWithTag:(int)tag
atRow: (int)row
column: (int)column
{
NSLog(@"reached 1....");
}
- (BOOL) browser: (NSBrowser *)sender isColumnValid: (int)column
{
NSLog(@"reached 3....");
return NO;
}
@end

View file

@ -77,14 +77,29 @@ GSSpellServerName(NSString *vendor, NSString *language)
// Non-private Instance methods
- init
{
NSArray *userLanguages = [NSUserDefaults userLanguages];
NSString *currentLanguage = [userLanguages objectAtIndex: 0];
[super init];
_delegate = nil;
_userDictionaries = [NSMutableDictionary dictionary];
_ignoredWords = nil;
ASSIGN(_userDictionaries, [NSMutableDictionary dictionary]);
ASSIGN(_currentLanguage, currentLanguage);
RETAIN(_userDictionaries);
RETAIN(_currentLanguage);
return self;
}
// Cleanup when deallocated
- (void)dealloc
{
RELEASE(_userDictionaries);
RELEASE(_currentLanguage);
}
// Checking in Your Service
- (BOOL)registerLanguage:(NSString *)language
byVendor:(NSString *)vendor
@ -183,8 +198,6 @@ GSSpellServerName(NSString *vendor, NSString *language)
}
}
}
NSLog(@"Dictionary path = %@", path);
return path;
}
@ -216,11 +229,6 @@ GSSpellServerName(NSString *vendor, NSString *language)
NSLog(@"Unable to find user dictionary at: %@", path);
}
}
else
{
NSLog(@"User dictionary for language %@ already opened.",
language);
}
// successful in opening the desired dictionary..
return words;
@ -233,9 +241,8 @@ GSSpellServerName(NSString *vendor, NSString *language)
{
BOOL result = NO;
NSString *dictWord = nil;
NSEnumerator *setEnumerator = nil, *dictEnumerator = nil;
NSEnumerator *setEnumerator = nil;
NSLog(@"Searching user dictionary");
// Catch the odd cases before they start trouble later on...
if(word == nil || dict == nil)
{
@ -248,7 +255,7 @@ GSSpellServerName(NSString *vendor, NSString *language)
}
// Check the dictionary for the word...
dictEnumerator = [dict objectEnumerator];
setEnumerator = [dict objectEnumerator];
while((dictWord = [setEnumerator nextObject]) && result == NO)
{
// If the case is important then uppercase both strings
@ -265,6 +272,29 @@ GSSpellServerName(NSString *vendor, NSString *language)
result = [word isEqualToString: dictWord];
}
}
if(result == NO && _ignoredWords)
{
NSEnumerator *arrayEnumerator = [_ignoredWords objectEnumerator];
NSString *iword = nil;
while((iword = [arrayEnumerator nextObject]) && result == NO)
{
// If the case is important then uppercase both strings
// and compare, otherwise do the comparison.
if(flag == NO)
{
NSString *upperWord = [word uppercaseString];
NSString *upperIWord = [iword uppercaseString];
result = [upperWord isEqualToString: upperIWord];
}
else
{
result = [word isEqualToString: iword];
}
}
}
return result;
}
@ -273,9 +303,7 @@ GSSpellServerName(NSString *vendor, NSString *language)
- (BOOL)isWordInUserDictionaries:(NSString *)word
caseSensitive:(BOOL)flag
{
NSArray *userLanguages = [NSUserDefaults userLanguages];
NSString *currentLanguage = [userLanguages objectAtIndex: 0];
NSSet *userDict = [self _openUserDictionary: currentLanguage];
NSSet *userDict = [self _openUserDictionary: _currentLanguage];
BOOL result = NO;
if(userDict)
@ -352,7 +380,7 @@ GSSpellServerName(NSString *vendor, NSString *language)
NSLog(@"Call to delegate caused following exception: %@",
[localException reason]);
}
NS_ENDHANDLER
NS_ENDHANDLER
return [self _saveUserDictionary: language];
}
@ -360,29 +388,22 @@ GSSpellServerName(NSString *vendor, NSString *language)
// Find a misspelled word
- (NSRange)_findMisspelledWordInString: (NSString *)stringToCheck
language: (NSString *)language
learnedDictionaries: (NSArray *)dictionaries
ignoredWords: (NSArray *)ignoredWords
wordCount: (int *)wordCount
countOnly: (BOOL)countOnly
{
NSRange r = NSMakeRange(0,0);
if(dictionaries != nil)
{
// Will put code here to check the user dictionary.
}
else
{
NSLog(@"No user dictionary to check");
}
// Forward to delegate
NS_DURING
{
ASSIGN(_ignoredWords,ignoredWords);
r = [_delegate spellServer: self
findMisspelledWordInString: stringToCheck
language: language
wordCount: wordCount
countOnly: countOnly];
findMisspelledWordInString: stringToCheck
language: language
wordCount: wordCount
countOnly: countOnly];
_ignoredWords = nil;
}
NS_HANDLER
{

View file

@ -5,7 +5,7 @@
Copyright (C) 2001 Free Software Foundation, Inc.
Author: Gregory John Casamento <borgheron@yahoo.com>
Author: Gregory John Casamento <greg_casamento@yahoo.com>
Date: May 2001
This file is part of the GNUstep Project
@ -23,11 +23,109 @@
*/
#include <AppKit/AppKit.h>
#include <Foundation/Foundation.h>
#include <Foundation/NSString.h>
// A minor category for NSData so that we can convert NSStrings
// into data.
@interface NSData (MethodsForSpellChecker)
+ (id)dataWithString: (NSString *)string;
@end
@implementation NSData (MethodsForSpellChecker)
+ (id)dataWithString: (NSString *)string
{
NSData *data = [NSData dataWithBytes: (char *)[string cString]
length: [string length]];
return data;
}
@end
@interface GNUSpellChecker : NSObject
{
NSTask *_spellTask;
}
@end
@implementation GNUSpellChecker
// Find the ispell executable in the user's path.
- (NSString *)_locateIspell
{
NSDictionary *env = [[NSProcessInfo processInfo] environment];
NSString *path = [env objectForKey: @"PATH"], *fullPath = nil;
NSScanner *pathScanner = nil;
BOOL found = NO;
NSFileManager *fm = [NSFileManager defaultManager];
pathScanner = [NSScanner scannerWithString: path];
while(![pathScanner isAtEnd] && !found)
{
NSString *directory = nil;
BOOL scanned = NO;
scanned = [pathScanner scanUpToString: @":" intoString: &directory];
[pathScanner scanString: @":" intoString: NULL];
fullPath = [directory stringByAppendingString: @"/ispell"];
found = [fm fileExistsAtPath: fullPath];
}
if(!found)
{
fullPath = nil;
}
return fullPath;
}
// Start the ispell command.
// The -a option allows the program to accept commands
// from stdin and put output on stdout.
- (id)_runIspell
{
NSArray *args = [NSArray arrayWithObject: @"-a"];
if(_spellTask == nil)
{
NSString *pathToIspell = [self _locateIspell];
NSPipe
*inputPipe = [NSPipe pipe],
*outputPipe = [NSPipe pipe];
_spellTask = [[NSTask alloc] init];
[_spellTask setLaunchPath: pathToIspell];
[_spellTask setArguments: args];
[_spellTask setStandardInput: inputPipe];
[_spellTask setStandardOutput: outputPipe];
[_spellTask launch];
if(![_spellTask isRunning])
{
NSLog(@"ispell failed to launch");
}
else
{
// Enter terse mode immediately upon startup.
NSString *terseCommand = @"!", *outstring = nil;
NSData
*data = [NSData dataWithString: terseCommand],
*output = nil;
// Sleep until ispell starts
[NSThread sleepUntilDate: [NSDate dateWithTimeIntervalSinceNow: 1]];
[[[_spellTask standardInput] fileHandleForWriting] writeData: data];
output = [[[_spellTask standardOutput] fileHandleForReading] availableData];
outstring = [NSString stringWithCString: (char *)[output bytes]];
}
}
return _spellTask;
}
- (void)_handleTaskTermination: (NSNotification *)notification
{
NSLog(@"ispell process died");
_spellTask = nil;
}
- (NSRange)spellServer:(NSSpellServer *)sender
findMisspelledWordInString:(NSString *)stringToCheck
language:(NSString *)language
@ -36,19 +134,103 @@ findMisspelledWordInString:(NSString *)stringToCheck
{
int length = 0;
NSRange r = NSMakeRange(0,0);
char *p = 0;
NSLog(@"Stubbed out - Finding misspelled word");
length = [stringToCheck length];
if(length < 10)
[self _runIspell];
if(_spellTask != nil)
{
r.length = length;
NSCharacterSet *newlineSet = nil;
NSRange newlineRange;
NSString
*checkCommand = @"^", *outputString = nil;
NSData
*inputData = nil,
*outputData = nil;
NSScanner
*inputScanner = nil;
NSFileHandle
*standardInput = [[_spellTask standardInput] fileHandleForWriting],
*standardOutput = [[_spellTask standardOutput] fileHandleForReading];
unsigned int i = 0;
newlineRange.location = (unsigned char)'\n';
newlineRange.length = 1;
newlineSet = [NSCharacterSet characterSetWithRange: newlineRange];
inputScanner = [NSScanner scannerWithString: stringToCheck];
[inputScanner setCharactersToBeSkipped: [NSCharacterSet whitespaceAndNewlineCharacterSet]];
*wordCount = 0;
while(![inputScanner isAtEnd])
{
NSScanner *outputScanner = nil;
NSString *outputString = nil, *fullCommand = nil, *inputString = nil;
NSData *returnChar = [NSData dataWithBytes: "\n"
length: 1];
[inputScanner scanUpToCharactersFromSet: [NSCharacterSet whitespaceAndNewlineCharacterSet]
intoString: &inputString];
fullCommand = [checkCommand stringByAppendingString: inputString];
fullCommand = [fullCommand stringByAppendingString: @"\n"];
inputData = [NSData dataWithString: fullCommand];
[standardInput writeData: inputData];
[standardInput writeData: returnChar];
outputData = [standardOutput availableData];
p = (char *)[outputData bytes];
outputString = [NSString stringWithCString: p];
// Put everything back into a string for scanning
outputScanner = [NSScanner scannerWithString: outputString];
// Check to see if the first character in the byte stream is
// a carriage return. If so, no spelling errors were found.
if(p[0] != '\n' && !countOnly)
{
NSString *indicator = nil;
NSString *word = nil,
*count = nil,
*offset = nil;
int start = 0;
[outputScanner scanUpToString: @" " intoString: &indicator];
if([indicator isEqualToString: @"&"]
|| [indicator isEqualToString: @"?"])
{
BOOL found = NO;
[outputScanner scanUpToString: @" " intoString: &word];
[outputScanner scanUpToString: @" " intoString: &count];
[outputScanner scanUpToString: @":" intoString: &offset];
found = [sender isWordInUserDictionaries: word caseSensitive: NO];
if(!found)
{
start = [inputScanner scanLocation] - [word length];
r = NSMakeRange(start, [word length]);
break;
}
}
else
if([indicator isEqualToString: @"#"])
{
BOOL found = NO;
[outputScanner scanUpToString: @" " intoString: &word];
[outputScanner scanUpToString: @" " intoString: &offset];
found = [sender isWordInUserDictionaries: word caseSensitive: NO];
if(!found)
{
start = [inputScanner scanLocation] - [word length];
r = NSMakeRange(start, [word length]);
break;
}
}
}
*wordCount++;
}
}
else
{
r.length = 10;
}
return r;
}
@ -56,9 +238,88 @@ findMisspelledWordInString:(NSString *)stringToCheck
suggestGuessesForWord:(NSString *)word
inLanguage:(NSString *)language
{
NSArray *array = [NSArray arrayWithObjects: word, @"test", nil];
NSMutableArray *array = [NSMutableArray array];
NSString *checkCommand = @"^", *fullCommand = nil, *outputString = nil;
NSScanner *inputScanner = nil;
NSData *inputData = nil, *outputData = nil;
NSData *returnChar = [NSData dataWithBytes: "\n"
length: 1];
NSFileHandle
*standardInput = [[[self _runIspell] standardInput] fileHandleForWriting],
*standardOutput = [[[self _runIspell] standardOutput] fileHandleForReading];
char *p = 0;
unsigned int i = 0;
BOOL stop = NO;
NSLog(@"Stubbed out - returning test guess results: %@", array);
fullCommand = [checkCommand stringByAppendingString: word];
inputData = [NSData dataWithString: fullCommand];
[standardInput writeData: inputData];
[standardInput writeData: returnChar];
// Check to see if the first character in the byte stream is
// a carriage return. If so, no spelling errors were found.
outputData = [standardOutput availableData];
// look at the results
p = (char *)[outputData bytes];
if(p[0] != '\n')
{
NSString *indicator = nil;
NSString *word = nil;
NSScanner *outputScanner = nil;
int i = 0;
// replace the first carriage return with a NULL to prevent the
// NSString from being constructed incorrectly
while(p[i] != '\n')
{
i++;
}
p[i] = 0;
// Put everything back into a string for scanning
outputString = [NSString stringWithCString: p];
outputScanner = [NSScanner scannerWithString: outputString];
[outputScanner setCharactersToBeSkipped: [NSCharacterSet whitespaceAndNewlineCharacterSet]];
// Scan the string for the indicator. Either & or #.
// The "&" and "?" symbols indicates a misspelled word for which ispell can suggest corrections
// whereas the "#" symbol represents a word for which ispell cannot suggest corrections.
[outputScanner scanUpToString: @" " intoString: &indicator];
if([indicator isEqualToString: @"&"]
|| [indicator isEqualToString: @"?"])
{
// get past the ": " on ispell's output line
[outputScanner scanUpToString: @": " intoString: NULL];
[outputScanner scanString: @": " intoString: NULL];
while(![outputScanner isAtEnd])
{
NSString *guessWord = nil;
if([outputScanner scanUpToString: @", " intoString: &guessWord])
{
[outputScanner scanString: @", " intoString: NULL];
}
else
if(![outputScanner scanUpToCharactersFromSet:
[NSCharacterSet whitespaceAndNewlineCharacterSet]
intoString: &guessWord])
{
break;
}
if(guessWord != nil)
{
[array addObject: guessWord];
}
}
}
else
if([indicator isEqualToString: @"#"])
{
[outputScanner scanUpToString: @" " intoString: &word];
}
}
return array;
}
@ -67,6 +328,7 @@ findMisspelledWordInString:(NSString *)stringToCheck
didLearnWord:(NSString *)word
inLanguage:(NSString *)language
{
NSLog(@"Stubbed out -- Learning word: %@", word);
}
@ -76,6 +338,19 @@ findMisspelledWordInString:(NSString *)stringToCheck
{
NSLog(@"Stubbed out -- Forgetting word: %@", word);
}
- init
{
[super init];
[[NSNotificationCenter defaultCenter]
addObserver: self
selector: @selector(_handleTaskTermination:)
name: NSTaskDidTerminateNotification
object: nil];
[self _runIspell];
return self;
}
@end
#ifdef GNUSTEP

View file

@ -1,3 +1,5 @@
NSExtensions = {};
NSPrincipalClass = NSApplication;
NSServices =
(
{