String Matching for Language App

Hello everyone! I am about to finish my language app!!!

I am comparing typed input strings with the correct answer text strings and if they are the same it is a match - and user gets “CORRECT!”

How can I make it so that for a match the typed input string essentially ignores “?” and “!” ?!

IE: I want a user to be able to type “HELLO” OR “HELLO!” and still get the correct match response if the correct answer string is “HELLO!”.

Same thing with question mark - how to accept “HOW ARE YOU?” AND “HOW ARE YOU” to still get a match?

Thank you!!! You all have helped me so much, I really appreciate this community.

You can strip punctuation from the user’s guess using a CharacterSet.

let str = "HELLO!"
let cleanStr = str.trimmingCharacters(in: .punctuationCharacters)
    //parameter is a CharacterSet
print(cleanStr)
//prints HELLO

Note that CharacterSet.punctuationCharacters covers far more than just ? and ! By my count, Unicode General Category P* contains nearly 800 characters!

when doing voice to voice matching — I think it would be a great idea - no? I am afraid that the system is not going to recognize someone using exclamatory or question in their voice…is stripping the punctuation via CharacterSet the best way you would do it??

Thank you!!!

I thought when using an input like Siri with a textBox, it’ll only put an exclamation mark when someone literally says “exclamation mark” while talking

I mean like if the correct string is “what is your name?” but when the user speaks “what is your name” if the phone does not recognize the “?” it will say it is wrong - so i would just like to remove that possibility from happening!!

what is best way you think?!

I think @roosterboy ’s solution would be fine

1 Like

Does @roosterboy 's solution also work for ignoring commas and periods?!?! I am having trouble getting it to match ! hmm

Yes, I think so.

let str = "HELLO!,."
let cleanStr = str.trimmingCharacters(in: .punctuationCharacters)
    //parameter is a CharacterSet
print(cleanStr)

https://developer.apple.com/documentation/foundation/nscharacterset/1411415-punctuationcharacters

## Discussion

Informally, this set is the set of all non-whitespace characters used to separate linguistic units in scripts, such as periods, dashes, parentheses, and so on.

Unfortunately, the code I posted above only strips punctuation from the beginning and end of a string. Since commas usually appear internally, they won’t get removed.

You can use this instead:


import Foundation 
let s = "Hello, my name is Simon!"
print(s.trimmingCharacters(in: .punctuationCharacters))
    //Hello, my name is Simon
extension String {
    var withoutPunctuation: String {
        self.components(separatedBy: .punctuationCharacters).joined(separator: "")
    }
}
print(s.withoutPunctuation)
    //Hello my name is Simon

oooh, that’s better. i mean, that works.