-1

entities = ({confidence = "<null>"; end = 113; entity = DATE; extractor = "ner_spacy";start = 103;value = "five years"; }, {confidence = "<null>"; end = 177;entity = ORG; extractor = "ner_spacy";start = 163; value = "xyz Company"; } );

This is the backend data, I need to display in string with remove and add the new values in a string text:

Example: "In your {{years_of_experience}} of experience at {{ORG}}, what kind of process improvements or standards setup?"

Answer : Array of 0 ---> five years and Array of 1 ---> xyz Company Instead of open and closed curly brackets, I need to show this text of array 0 and 1.

In your five years of experience at xyz company, what kind of process improvements or standards setup?

2 Answers2

0

I've tried to get a solution for your problem,

This is the JSON response that I've used as an example,

[
    {
        "confidence": "<null>",
        "end": 113,
        "entity": "DATE",
        "extractor": "ner_spacy",
        "start": 103,
        "value": "five years"
    },
    {
        "confidence": "<null>",
        "end": 177,
        "entity": "ORG",
        "extractor": "ner_spacy",
        "start": 163,
        "value": "xyz Company"
    }
]

Parse the JSON response using Codable into an array of Entity objects, i.e.

struct Entity: Codable {
    var confidence: String?
    var end: Int?
    var entity: String?
    var extractor: String?
    var start: Int?
    var value: String?
}

I've used entity key in the response to identify which value to replace, i.e.

if let data = str.data(using: .utf8) { //You'll get this data from API response
    let entities = try? JSONDecoder().decode([Entity].self, from: data)

    var sentence = "In your {{DATE}} of experience at {{ORG}}, what kind of process improvements or standards setup?"
    entities?.forEach({
        if let entity = $0.entity, let value = $0.value {
            sentence = sentence.replacingOccurrences(of: "{{\(entity)}}", with: value)
        }
    })
    print(sentence) //In your five years of experience at xyz Company, what kind of process improvements or standards setup?
}

In the above code, I've traversed the entities array and replaced each occurrence of the {{entity}} with the respective value, i.e.

"{{DATE}}" is replaced with "five years"
"{{ORG}}" is replaced with "xyz Company"

Let me know in case you still face any issues or if I haven't understood the problem statement well.

PGDev
  • 23,751
  • 6
  • 34
  • 88
0

It is not working for dynamic data, In some text doesn't contain any key values and {{}}, In that case how we will write this.

I need to display an tableview with this type of data and play the voice message.

Example: q1) Can you please explain to me about yourself, highlighting the number of years of experience relevant to Project Manager and different domains you worked on

Answer: User says the answer, sends the backend and stored the response in a dictionary.

Q2) In your {{years_of_experience}} of experience at {{ORG}}, what kind of process improvements or standards setup? Note: 1) I need to replace the text value inside of the {{ }} 2) For some question text there is no entity key and value. 3) We need to store {{ORG}} value and Whenever question text inside {{ORG}} we should replace the value of entity.

Q3) Could you tell me a few Software development methodologies and what you have used and are comfortable with?

q4) Great. Can you name a few clients you were supporting in {{industry}} domain and {{years_of_experience}}?

------------ and Soon.

I stored the entity key and value response whenever says the answers with respective text