How do I replace words in a text field to change into other words in another text field?

This is for Xcode 5 and for iOS apps; I'm working with Objective-C code.

Okay so for example, if I put into the first text field "I was born in 1995." and when I click a convert button, I want it to say "I was conceived circa 1995." How can I go about doing that? The problem I'm having is being able to replace a word with another word.

Like, how do I return whats in the text field to be replace the possible words into whatever the person types in? Sort of like a translator app, the way it replaced words.

My question concerns that if the user were to type anything into the text field, then it would rephrase it for him with words that have synonyms to be other words.

Yes, it is about replacing substrings, but it will replace whatever the user types into it.

I understand that stringByReplacingOccurrencesOfString:withString: makes sense, but what would go after that to apply to whatever the user typed in?

Basically, let's say a translator app. If I type in: "I am very smart." it would rephrase to: "I am very intellectual." It has to deal with whatever the user types in.


If the string is in NSString, you can use the stringByReplacingOccurrencesOfString:withString: method.

NSString *str = @"I was born in 1995.";

str = [str stringByReplacingOccurrencesOfString:@"born in" withString:@"conceived circa"];

Not sure though if this is what you mean.

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
            // 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;

If you're trying to do in place replacement of strings while the user types you will need probably to use the UITextFieldDelegate method:

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string

This is called on every character insertion (or text paste) in a UITextField.

Pseudo code (because I'm writing this off the top of my head):

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
    NSMutableString *proposedText = [textField.text mutableCopy];
    [proposedText replaceCharactersInRange:range withString:string];

    if (proposedText contains word I want to replace)
       replace word in proposedText

       textField.text = proposedText;
       textField.selectedTextRange = new selection

       return NO;

    return YES; 

Hope this helps.

Need Your Help

binary comparation

c++ binary

Is there any function in c++ to convert decimal number to binary number without using divide algorithm?

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.