1

I am working on Swift 4.

The requirement: I have a string:

var inputString = "Today is very bright day"

I have a file containing key value pairs for strings.

{
    "Birthday": ["brand day", "burn day", "bright day", "brick daya"],
    "Temporary": ["tempora", "temporar", "tempura"]
}

The math needed: In the app, if the inputString is "Today is very bright day", I need to check if this string contains any of the values from the key-value pair file and replace the occurrence with the key.

e.g: In above example: inputString = "Today is very bright day" match it with the file. replace bight day with birthday so that the inout string becomes inputString = "Today is very birthday".

Is there any way to do this? The key-value file could be big.

2
  • Wouldn't it make more sense to invert the Dictionary? Map the words you're searching for, to the words you're replacing them with Commented Jun 15, 2018 at 17:14
  • Actually, since some of these terms are multiple words, you wouldn't get that much benefit from doing what I just mentioned above. I was thinking you could split on spaces/punctuation to get words, and query the dict for each word. That wouldn't work for doing multi-word matches. Commented Jun 15, 2018 at 17:24

3 Answers 3

1

You can use brute force solution it will be 2 for loop , i work on way to enhance it

  var inputString = "Today is very bright day"

        let file  = ["Birthday":["brand day", "burn day", "bright day", "brick daya"],"Temporary": ["tempora", "temporar", "tempura"]]

        file.forEach { (key,value) in
            value.forEach({ (item) in
               inputString =  inputString.replacingOccurrences(of: item, with: key)
            })
        }

        print(inputString)
Sign up to request clarification or add additional context in comments.

1 Comment

This one looks more feasible, but time consuming with iterating over each values. Lets integrate this and I will see. Thank you. @Abdelahad
0

I think you will have to iterate through each list of each key value pairing no matter what, but here is a basic way to do replace substrings.

import Foundation

let replacer = "Birthday"
let replacees = ["brand day", "burn day", "bright day", "brick daya"]

var inputString = "Today is very bright day"

for phrase in replacees {
    inputString = inputString.replacingOccurrences(of: phrase, with: replacer)
}

print(inputString) //should print "Today is very birthday"

4 Comments

This looks achievable. Let me implement this and see. The solution here works fine for just one pair of replacer and replacees. As I mentioned in the question. I will have multiple replacers and multiple replacees. How to go about that?
I'm not exactly sure how you are storing your [replacer, replacees] pairings, but what you can do is just put translate the above code to a function and then just use a for-loop to apply each one of the replacer-replacees pairings. func f(replacer: String, replacees: [String], inputString: String) -> String { // insert the above code into here }
Oh wait I didn't see that you were storing it as a dictionary. So you will need a nested for loop (or equivalent), where you do: for key in keyValuePairings {// above code} and then you use key as replacer, and use keyValuePairings[key] as replacees.
It is essentially is just Abdelahad Darwish's solution
0

I cant't figure anyway to achieve what you want without iterating through your key-value list, but here is a solution. I'm not sure if it will be efficient, but you may try it.

        if let path = Bundle.main.path(forResource: "fileName", ofType: "json") {
            do {
                let data = try Data(contentsOf: URL(fileURLWithPath: path), options: .mappedIfSafe)
                let jsonResult = try JSONSerialization.jsonObject(with: data, options: .allowFragments)

                if let keyValueArray = jsonResult as? [String: [String]]{
                    var inputString = "Today is very bright day"

                    for (key, value) in keyValueArray {
                        let regex = "\\b" + value.joined(separator: "|\\b")

                        inputString = inputString.replacingOccurrences(of: regex, with: key, options: .regularExpression, range: nil)
                    }

                    print(inputString)
                }
            } catch {
                return
            }
        }

1 Comment

Thanks. I will definitely try it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.