Swift: how to censor/filter text entered for swear

2020-07-29 17:59发布

问题:

I just want to see whether there is an established way to do this, or how one would go about this.

I have a text field that essentially acts as a form in my iOs app where a user can post something. I can't have users posting swear words/inappropriate crap, so I want to filter out and display an error if the string they enter contains one of these words.

How do other apps in Swift do this? Do they just search through the string to see if it contains the word (obviously not within other words, but standing alone) or is there another method?

How can I accurately filter out the swear words from my user post in Swift?

回答1:

Construct a list of words you consider to be swear words, and simply check the user entered string whether any of these words are contained within the string.

Swift 3:

import Foundation

func containsSwearWord(text: String, swearWords: [String]) -> Bool {
    return swearWords
        .reduce(false) { $0 || text.contains($1.lowercased()) }
}

// example usage
let listOfSwearWords = ["darn", "crap", "newb"] 
    /* list as lower case */

let userEnteredText1 = "This darn didelo thread is a no no."
let userEnteredText2 = "This fine didelo thread is a go."

print(containsSwearWord(text: userEnteredText1, swearWords: listOfSwearWords)) // true
print(containsSwearWord(text: userEnteredText2, swearWords: listOfSwearWords)) // false

Swift 2.2:

import Foundation

func containsSwearWord(text: String, swearWords: [String]) -> Bool {
    return swearWords
        .reduce(false) { $0 || text.containsString($1.lowercaseString) }
}

// example usage
let listOfSwearWords = ["darn", "crap", "newb"]
/* list as lower case */

let userEnteredText1 = "This darn didelo thread is a no no."
let userEnteredText2 = "This fine didelo thread is a go."

print(containsSwearWord(userEnteredText1, swearWords: listOfSwearWords)) // true
print(containsSwearWord(userEnteredText2, swearWords: listOfSwearWords)) // false


回答2:

I created a class that enables you to feed a string in and remove profanity. Here's a link to the repo.

Here's the code:

class ProfanityFilter: NSObject {

    static let sharedInstance = ProfanityFilter()
    private override init() {}

    // Customize as needed
    private let dirtyWords = "\\b(ducker|mother ducker|motherducker|shot|bad word|another bad word|)\\b"

    // Courtesy of Martin R
    // https://stackoverflow.com/users/1187415/martin-r
    private func matches(for regex: String, in text: String) -> [String] {

        do {
            let regex = try NSRegularExpression(pattern: regex, options: [.caseInsensitive])
            let nsString = text as NSString
            let results = regex.matches(in: text, range: NSRange(location: 0, length: nsString.length))
            return results.map { nsString.substring(with: $0.range)}
        } catch let error {
            print("invalid regex: \(error.localizedDescription)")
            return []
        }
    }

    public func cleanUp(_ string: String) -> String {
        let dirtyWords = matches(for: self.dirtyWords, in: string)

        if dirtyWords.count == 0 {
            return string
        } else {
            var newString = string

            dirtyWords.forEach({ dirtyWord in
                let newWord = String(repeating: "              
                            
标签: ios swift