Given a word, you want to find words with similar meaning. You will need a lot of data - arrays of words grouped by meaning. A given word may appear in several of these arrays depending on context (one reason why machine interpretation of language is difficult...).
The easy way to separate the string into words is to use componentsSeparatedByString:
with a string of @" "
. After that you need to identify which strings you want to try and replace. Whether or not they return anything useful using the dictionary described below is one way to decide.
A specific word can be used as a key into a dictionary returning an array of arrays. Let's say the word is "iron". When you use that as a key in your master dictionary it returns an array containing three arrays. In one are all the words (as NSStrings) that mean "elemental iron", in another are all the words that mean "smooth clothes with a hot tool" such as "press" or "smooth", in another are tool-like uses such as "shooting iron", "branding iron" etc.
The hardest thing you have to do is identify the context and choose the right array, or else you end up generating nonsense.
Once you have identified context you can choose any other word from the selected array, substitute it in the sentence, and then process other words you want to substitute.
To separate a string such as
NSString *string = @"I am very smart."
you would use
NSArray *words = [string componentsSeparatedByString:@" "];
you can iterate over the words with
for(NSString *word in words) {
// do something with NSString *word here
}
Here's a quick look at building the master dictionary - it hasn't been run but I'm sure someone will spot if there's a mistake...
// NSArray *arrayOfArraysOfSimilarWords; <- populated elsewhere, as shown below
// array
// |
// - "cat", "feline", "pussycat", "gato"
// - "shoe", "pump", "clog", "trainer"
NSMutableDictionary *masterDictionary = [NSMutableDictionary dictionary];
for(NSArray *wordArray in arrayOfArraysOfSimilarWords) {
for(NSString *word in wordArray) {
NSArray *arrayOfWordArrays = masterDictionary[word];
if(arrayOfWordArrays == nil) {
masterDictionary[word] = @[wordArray];
} else {
// entry exists
NSSet *wordArraySet = [NSSet setWithArray:arrayOfWordArrays];
if([wordArraySet containsObject:wordArray]) {
// this word array is already returned as a result for this word
continue;
}
// there is no entry for this word array yet - add an array containing the current word array
NSMutableArray *newArrayOfWordArrays = [NSMutableArray arrayWithArray:arrayOfWordArrays];
[newArrayOfWordArrays addObject:wordArray];
masterDictionary[word] = newArrayOfWordArrays;
}
}
}