In this app I'm building, I'm hoping for the app to take a word that is given to it and search a dictionary to see whether or not the word is real. How would I go about doing this in objective-c?
Does objective-c have anything like the 'V-Lookup' function in Excel? If so, would this be useful?
Any help is appreciated!
So what you're trying to do is this:
iPhone objective-c: detecting a 'real' word
-(BOOL)isDictionaryWord:(NSString*)word {
UITextChecker *checker = [[UITextChecker alloc] init];
NSLocale *currentLocale = [NSLocale currentLocale];
NSString *currentLanguage = [currentLocale objectForKey:NSLocaleLanguageCode];
NSRange searchRange = NSMakeRange(0, [word size]];
NSRange misspelledRange = [checker rangeOfMisspelledWordInString:word range: searchRange startingAt:0 wrap:NO language: currentLanguage ];
return misspelledRange.location == NSNotFound;
}
Related
I need my app to check if a word entered by the user is actually a real word or not. For doing this i found the UITextChecker. I am using it to check for words like this:
UITextChecker *textChecker = [[UITextChecker alloc] init];
NSLocale *locale = [NSLocale currentLocale];
NSString *language = [locale objectForKey:NSLocaleLanguageCode];
NSRange searchRange = NSMakeRange(0, [currentWord length]);
NSRange misspelledRange = [textChecker rangeOfMisspelledWordInString:currentWord range: searchRange startingAt:0 wrap:NO language:language];
if (misspelledRange.location == NSNotFound) NSLog(#"is a word");
else NSLog(#"is not a word");
This works fine until it comes to words that begin with an uppercase (and that are a lot of words in german :) ). The user is entering the text lowercase, so even if a word is actually a word it outputs "is not a word" because of it being lowercase.
I found no solution for this problem in the documentation, also i searched for it here.
So my question is if there is any better way for doing this, than transforming the first letter into an uppercase if the check failed?
Example for what i would do if there is no other way:
NSRange misspelledRange = [textChecker rangeOfMisspelledWordInString:currentWord range: searchRange startingAt:0 wrap:NO language:language];
if (misspelledRange.location == NSNotFound) NSLog(#"is a word");
else {
NSString *firstLetter = [currentWord substringToIndex:1];
NSString *restWord = [currentWord substringFromIndex:1];
currentWord = [NSString stringWithFormat:#"%#%#", [firstLetter capitalizedString], restWord];
NSRange misspelledRange = [textChecker rangeOfMisspelledWordInString:currentWord range: searchRange startingAt:0 wrap:NO language:language];
if (misspelledRange.location == NSNotFound) NSLog(#"is a word");
NSLog(#"is not a word");
}
Thanks for your help!
I'm getting the following error from the code below and I'm not sure why.
Fatal Exception NSRangeException
-[__NSCFString replaceOccurrencesOfString:withString:options:range:]: Range {0, 7} out of bounds; string length 6
I wondered if someone could explain ?
Is it just that I need to new NSRange variable to cater for the different string length ?
+(double)removeFormatPrice:(NSString *)strPrice {
NSNumberFormatter *currencyFormatter = [[NSNumberFormatter alloc] init];
[currencyFormatter setNumberStyle:NSNumberFormatterCurrencyStyle];
NSNumber* number = [currencyFormatter numberFromString:strPrice];
//cater for commas and create double to check against the number put
//through the currency formatter
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
NSMutableString *mstring = [NSMutableString stringWithString:strPrice];
NSRange wholeShebang = NSMakeRange(0, [mstring length]);
[mstring replaceOccurrencesOfString: [formatter groupingSeparator]
withString: #""
options: 0
range: wholeShebang];
//if decimal symbol is not a decimal point replace it with a decimal point
NSString *symbol = [[NSLocale currentLocale]
objectForKey:NSLocaleDecimalSeparator];
if (![symbol isEqualToString:#"."]) {
[mstring replaceOccurrencesOfString: symbol
withString: #"."
options: 0
range: wholeShebang]; // ERROR HERE
}
double newPrice = [mstring doubleValue];
if (number == nil) {
return newPrice;
} else {
return [number doubleValue];
}
}
After the first replacement operation has been done, the string is shorter than it was, when you constructed the range (you are replacing characters with nothing). The original initialisation
NSRange wholeShebang = NSMakeRange(0, [mstring length]);
gives now a range, whose length is too large. Example:
NSMutableString* str = [NSMutableString stringWithString: #"1.234,5"];
NSRange allOfStr = NSMakeRange(0, [str length]);
[str replaceOccurrencesOfString: #"."
withString: #""
options: 0
range: allOfStr];
Note, that str now looks like 1234,5, i.e., it is shorter by one character than it was at the time, the range was initialized.
For that reason, you get the index out of bounds error, if you use the range again on the now too short string. You should re-initialize the range before passing it to the second replace op.:
allOfStr = NSMakeRange(0, [str length]);
[str replaceOccurrencesOfString: #","
withString: #"."
options: 0
range: allOfStr];
NSString * currentWord;
currentWord = Text.text;
UITextChecker* checker = [[UITextChecker alloc] init];
NSString* preferredLanguage = [[UITextChecker availableLanguages] objectAtIndex:0];
NSRange range;
range = [checker rangeOfMisspelledWordInString:currentWord
range:NSMakeRange(0, [currentWord length])
startingAt:0
wrap:NO
language:preferredLanguage];
if (range.location == NSNotFound)
{
NSLog(#"Word found");
}
else
{
NSLog(#"Word not found");
}
//Here i used UITextChecker Function ,even for the wrong word, the above function shows correct word statement
eg: wrong word like abcd,
bcde,
cdef,
CAPs, kindly help me what is the reason behind this. Is there any other option to solve this?
Thanks in advance
UITextChecker *Checker = [[UITextChecker alloc] init];
NSRange range = NSMakeRange(0, inputWord.length);
NSRange misspelledRange = [Checker rangeOfMisspelledWordInString:[Sentence lowercaseString] range:range startingAt:0 wrap:NO language:#"en_IN"];
bool isValidWord = misspelledRange.location == NSNotFound;
//NSLog(#"----%i", misspelledRange.location);
if (isValidWord)
{
isValidWord = [self checkIfWordExistsInSuggestedSpellings:Sentence];
NSLog(#"++++%d", isValidWord);
}
return isValidWord;
}
else
{
NSLog(#"Invalid word");
return false;
}
NSNotFound means that whatever you asked for wasn't found. == is equality operator. Your code prints "Word found" when range.location is equal to NSNotFound.
This question already has answers here:
How to get all NSRange of a particular character in a NSString?
(4 answers)
Closed 9 years ago.
NSString *str = #" My name is Mike, I live in California and I work in Texas. Weather in California is nice but in Texas is too hot...";
How can I loop through this NSString and get NSRange for each occurrence of "California", I want the NSRange because I would like to change it's color in the NSAttributed string.
NSRange range = NSMakeRange(0, _stringLength);
while(range.location != NSNotFound)
{
range = [[attString string] rangeOfString: #"California" options:0 range:range];
if(range.location != NSNotFound)
{
range = NSMakeRange(range.location + range.length, _stringLength - (range.location + range.length));
[attString addAttribute:NSForegroundColorAttributeName value:_green range:range];
}
}
Lots of ways of solving this problem - NSScanner was mentioned; rangeOfString:options:range etc. For completeness' sake, I'll mention NSRegularExpression. This also works:
NSMutableAttributedString *mutableString = nil;
NSString *sampleText = #"I live in California, blah blah blah California.";
mutableString = [[NSMutableAttributedString alloc] initWithString:sampleText];
NSString *pattern = #"(California)";
NSRegularExpression *expression = [NSRegularExpression regularExpressionWithPattern:pattern options:0 error:nil];
// enumerate matches
NSRange range = NSMakeRange(0,[sampleText length]);
[expression enumerateMatchesInString:sampleText options:0 range:range usingBlock:^(NSTextCheckingResult *result, NSMatchingFlags flags, BOOL *stop) {
NSRange californiaRange = [result rangeAtIndex:0];
[mutableString addAttribute:NSForegroundColorAttributeName value:[NSColor greenColor] range:californiaRange];
}];
with
[str rangeOfString:#"California"]
and
[str rangeOfString:#"California" options:YOUR_OPTIONS range:rangeToSearch]
You may use rangeOfString:options:range: or NSScanner (there are other possibilities like regexps but anyway). It's easier to use first approach updating range, i.e. search for first occurrence and then depending on the result update the search range. When the search range is empty, you've found everything;
I want to extract all "daughter" of a NSarray.
For example, nsarray is made like this:
hello
hi
hello daughter
my goodness
daughter (someone)
my daughter
how are you?
etc.
And I want to extract all lines that contain "daughter". Could you point me in right direction?
My current code is:
for (NSString *mystring in self.array)
{
if ([mystring rangeOfString:searchText].location!=NSNotFound)
{
NSUInteger idx = [self.array indexOfObject:mystring];
[self.filterarray addObject: mystring];
}
}
NSArray *lines = ...;
NSArray *filteredLines = [lines filteredArrayUsingPredicate:[NSPredicate predicateWithBlock:^BOOL(NSString *string, NSDictionary *bindings) {
return ([string rangeOfString:searchText].location != NSNotFound);
}]];
(requires iOS 4.0 or later)
If you need to support earlier system versions use this:
NSArray *lines = ...;
NSArray *filteredLines = [lines filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:#"(SELF contains[c] %#)", searchText]];
(requires iOS 3.0 or later)
NSString *textViewText = #“hello hi hello daughter my goodness daughter (someone) my daughter how are you? etc.”;
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:textViewText];
NSArray *highlightWords = #[#“daughter”,#”hello”];
for(NSString *word in highlightWords)
{
NSRange range = NSMakeRange(0, textViewText.length);
do{
range = [textViewText rangeOfString:word options:NSCaseInsensitiveSearch range:range];
[attributedString addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:range];
if (range.location != NSNotFound)
range = NSMakeRange(range.location + 1, textViewText.length - (range.location + 1));
}while(range.location != NSNotFound);
}