-4

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.

jscs
  • 63,694
  • 13
  • 151
  • 195
Hedylove
  • 1,724
  • 16
  • 26
  • 1
    See also [Replace specific words in NSString](http://stackoverflow.com/q/15997712) – jscs Jun 04 '14 at 04:49
  • This does not look like a duplicate of http://stackoverflow.com/q/15997712, more a request for help writing an app with thesaurus functionality - e.g. efficiently find lists of words with equivalent meaning. – Adam Eberbach Jun 04 '14 at 04:50
  • 3
    However, it is far too broad... – borrrden Jun 04 '14 at 04:54
  • It may seem broad, but it really is all about if a word is in the text fiend, then replace it. My problem is having the words in the text field being read to change them if they are equal to another word. – Hedylove Jun 04 '14 at 04:56
  • 1
    I read your question thoroughly, and if you're not asking how to replace substrings, then I don't understand what you're asking. Please edit to _clarify_ instead of barking at people who are trying to help you find an answer to your problem. – jscs Jun 04 '14 at 04:57
  • For word replacing you could use **stringByReplacingOccurrencesOfString** but as I understand your question you want to rephrase the sentence with new word keeping the meaning of sentence same or similar so for that you would need write down a function using regex so as to check and keep meaning of sentence similar which by the way will gonna take a lot of your time. Good Luck ! – nikhil84 Jun 04 '14 at 05:10
  • Time is not an issue for me, but thanks for the regex suggestion, I will look into it. – Hedylove Jun 04 '14 at 05:18
  • 1
    possible duplicate of [Replace specific words in NSString](http://stackoverflow.com/questions/15997712/replace-specific-words-in-nsstring) – Ryan Haining Jun 04 '14 at 06:23
  • @RyanHaining No. That one works with a pre entered statement. Mine requires the input of the user. – Hedylove Jun 04 '14 at 06:54
  • `stringByReplacingOccurrencesOfString:withString:` does _not_ require a literal string for either the arguments or the receiver. You can get the strings from wherever you like: a text field, a file, a collection in memory, or created from arbitrary characters on the spot. It sounds like you need need to cover some basics before you start writing an app. Have a look at [Good resources for learning ObjC](http://stackoverflow.com/q/1374660) – jscs Jun 05 '14 at 02:58
  • I figured it out. I used this: – Hedylove Jun 06 '14 at 00:35

3 Answers3

0

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.

Enrico Susatyo
  • 19,372
  • 18
  • 95
  • 156
  • Unfortunately, this was already suggested in the proposed duplicate, and OP has said that it's not what he is looking for. – jscs Jun 04 '14 at 05:02
  • This requires a pre-made statement. What I'm looking for is dependent on what the user types in. – Hedylove Jun 04 '14 at 05:15
0

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;
        }
    }
}
Adam Eberbach
  • 12,309
  • 6
  • 62
  • 114
  • Okay this sounds like the correct solution. How would I go about implementing the componentSeperateByString: into the code? For example. The first text field is called input. How do I implement it into the input part? After pressing the convert button, I thought it had to do with _input.text = .... but I'm not sure what? – Hedylove Jun 04 '14 at 05:12
  • word separation code added to the answer. – Adam Eberbach Jun 04 '14 at 05:17
  • Thank you for this. I am currently trying to implement this. Would the "NSMutableDictionary..." go under the [super viewDidLoad]? I have this when the convert button is pressed: - (IBAction)convert:(id)sender { NSString *input = _input.text; NSArray *words = [input componentsSeparatedByString:@" "]; //////How do I implement the for here? It tells me that "words" is not found. } – Hedylove Jun 04 '14 at 05:47
  • If it is in a method of its own then it can be anywhere in the implementation of a module. [super viewDidLoad] would normally only be called from a view controller's viewDidLoad method, and unless you only want to convert once when the view controller's view is loaded that's probably the wrong place to put it. – Adam Eberbach Jun 04 '14 at 05:52
  • So how do I make it that if the word "smart" is found, set it equal to intellectual? Nevermind, im using the stringByAppendingString function. – Hedylove Jun 04 '14 at 06:01
  • Imagine that arrayOfArraysOfSimilarWords = @[@[@"smart", @"intellectual"]]; That's an array containing an array containing two similar words. Build the masterDictionary with that input to the code shown, then follow the method described in the original answer to find the alternative word. – Adam Eberbach Jun 04 '14 at 06:05
  • Okay I'm sorry that you're doing all the work, but I'm getting confused how exactly to show after the "for..." statement to make happy change to intelligent. – Hedylove Jun 04 '14 at 06:45
  • 1
    Don't be too discouraged but what you're asking indicates that what you're trying is a little ambitious right now. Have you taken a look at some of the programming tutorials on raywenderlich.com? The forums there might also be a better place for the more fundamental questions you're asking. – Adam Eberbach Jun 04 '14 at 07:00
0

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.

orj
  • 13,234
  • 14
  • 63
  • 73
  • This might help. It actually replaces is it after the user clicks the "convert" button, not while typing. – Hedylove Jun 04 '14 at 06:52